1

I generally use argparse in Python and docopt in R. One of the features I miss from argparse that I have not yet figured out in docopt is the ability to specify the required data type for each argument. For example, in argparse I would require an integer input using

parser.add_argument("square", help="display a square of a given number",
                type=int)

In docopt / R, I can't find anything in the documentation about requiring a specific data type.

-s <square>, --square=<square>   display a square of a given number #additional option to require integer input?

There's a closed issue on the Python version of docopt GitHub repo that seems to indicate that this is not a part of base docopt and offers a solution for Python, but this isn't directly applicable to R. Can anyone offer any suggestions / more elegant way to validate argument inputs using docopt in R?

AJW
  • 11
  • 2

2 Answers2

1

not sure if this is elegant enough as it involves setting defaults and then using utils::type.convert determine the class/typeof

"Usage: my_program.R [-hson FILE] [--quiet | --verbose] [INPUT ...]

-h --help        show this 
-s --sorted      sorted output
--coefficient=K  [default: 2.95] The K coefficient 
--numSim=K       [default: 200] number of simulations 
--output=FILE    [default: test.txt] Output file 
--directory=DIR  [default: ./] Some directory 
-o FILE          specify output file [default: ./test.txt]
--quiet          print less text
--verbose        print more text" -> doc
opts <- docopt(doc, "-s --quiet")
str(opts)

newopts <- lapply(opts, function(x) utils::type.convert(as.character(x),as.is=T))
(definedClasses <- unlist(lapply(newopts, typeof)))

When you are running the program, you can test the inputs against this definedClasses.

You might also want to check out the getopt and optparse/argparse packages and also this SO post Parsing command line arguments in R scripts

References:

http://docopt.org

http://rgrannell1.github.io/blog/2014/08/04/command-line-interfaces-in-r

http://docopt.readthedocs.org/en/0.2.0/

Community
  • 1
  • 1
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
  • Thank you, that's interesting information. I may play around with this approach some in the future. I ended up using the testthat package to write tests to check that user input conformed to expectations. – AJW May 10 '16 at 16:20
-2

I'm a little curious why you don't use docopt with python or why you don't you use argparse (or optparse) in R?

In case you were wondering this is how to do your requested feature with the optparse R package:

> library("optparse")
> parser = OptionParser()
> parser = add_option(parser, c("-s", "--square"), 
+    type="integer", 
+    help="display a square of a given number")
> typeof(parse_args(parser, "--square=5")$square)
[1] "integer"

How to do this with the argparse package (this latter one has a python dependency):

> parser = argparse::ArgumentParser()
> parser$add_argument("-s", "--square", 
+    type="integer",
+    help="display a square of a given number")
> typeof(parser$parse_args("--square=5")$square)
[1] "integer"
Trevor
  • 434
  • 4
  • 8