docopt.R
docopt.R copied to clipboard
can't make example from docopt.org work
Newbie here. Here is example from http://docopt.org/:
Naval Fate.
Usage:
naval_fate ship new <name>...
naval_fate ship <name> move <x> <y> [--speed=<kn>]
naval_fate ship shoot <x> <y>
naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
naval_fate -h | --help
naval_fate --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
Trying to make it work from R:
library(docopt)
doc ='
Naval Fate.
Usage:
naval_fate ship new <name>...
naval_fate ship <name> move <x> <y> [--speed=<kn>]
naval_fate ship shoot <x> <y>
naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
naval_fate -h | --help
naval_fate --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
'
opt = docopt(doc)
Getting uninformative error:
Error:
usage: naval_fate ship new <name>...
usage: naval_fate ship <name> move <x> <y> [--speed=<kn>]
usage: naval_fate ship shoot <x> <y>
usage: naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
usage: naval_fate -h | --help
usage: naval_fate --version
Thanks for reporting, I'll look into it tomorrow!
Best
I think the error you're seeing is because you're using it in a context where the command line args to the R session are not a valid "usage". To simulate command line args, try
docopt(doc, "-h")
# or
docopt(doc, c("ship", "new", "Boaty McBoatface"))
I think the error you're seeing is because you're using it in a context where the command line args to the R session are not a valid "usage". To simulate command line args, try
docopt(doc, "-h") # or docopt(doc, c("ship", "new", "Boaty McBoatface"))
I actually tried to put it to test in real command-line environment. i.e. I have a rscript file (where we have that typical line of #!/usr/bin/Rscript
, with correct permission (in linux sudo chmod +755
). I ran this file from my terminal, and I got the same error.
Ok so I think I somehow get the idea now. We do need to run the actual script in a real terminal to get it to work correctly but that is a little bit implicit in documentation.... (too implicit to actually see it anywhere.... :P )
Firstly, it is right if you run your script from RStudio (for example) you'll have to provide dummy values for docopt to understand you are simulating the real command-line environment and to feed you back with correct simulated output.
The same logic applies in terminal experiment as well. The example on the front page is supposed to be run with actual input value for each argument for docopt to understand.
i.e. let's call our front-page example file 'test.R' and it has correct permission,
./test.R ship new something
./test.R ship something move 10 50 --speed=20
to test incorrect usage as suggested,
./test.R ship mine
all output on terminal should match the results demonstrated on the front page.