Open zkamvar opened 8 years ago
Thx for reporting! I will look into at the end of this week
Best,
Edwin
Seconded; please fix this bug. The MWE that @zkamvar produced above was almost exactly my first stab at using docopt.R
, which made me think that I was doing something wrong. Glad I thought to check the issue tracker.
+1, I would love to have this working in my scripts
Bump.
Thx for reminding: will look into this before weekend
bump
Hi everybody! Just run into the same issue here, when taking the first steps with Docopt. Any fix yet?
@martabe I only had this issue when trying to run in rstudio - if I call my script from the command line, it seems to work
FWIW, I got the error running on a cluster. I don't think RStudio has anything to do with it. @rmwthorne, what version are you using?
library("docopt")
'usage: my_program.R [-a -r -m <msg>] <file>
options:
-a Add
-r Remote
-m <msg> Message' -> doc
docopt(doc)
#> Error:
#> usage: my_program.R [-a -r -m <msg>] <file>
Sorry to intrude, but isn't this behavior correct? In the example, we have an argument <file>
which is mandatory, so should always be provided.
When doing docopt(doc)
with no arguments, it looks at commandArgs(TRUE)
, and hence gives the error because there is no <file>
argument provided. If we do docopt(doc, "foo")
we get the right info:
List of 8
$ -a : logi FALSE
$ -r : logi FALSE
$ -m : NULL
$ <file>: chr "foo"
$ a : logi FALSE
$ r : logi FALSE
$ m : NULL
$ file : chr "foo"
NULL
So the point is that docopt
receives both the definition and the actual arguments received. The error you get is not in the definition of the arguments, but in the values received (which are none).
@rmwthorne
I only had this issue when trying to run in rstudio
I see the same behaviour in Rstudio and running the script in the terminal
edit: at least with my script. I cannot replicate the same behaviour with the very simple script from @zkamvar:
'usage: my_program.R <file>' -> doc docopt(doc)
I finally ended up using optparse
...
Session Information:
> devtools::session_info()
Session info -----------------------------------------------------------------------
setting value
version R version 3.4.3 (2017-11-30)
system x86_64, linux-gnu
ui RStudio (1.1.423)
language en_US:en
collate en_US.UTF-8
tz Europe/Zurich
date 2018-02-20
Packages --------------------------------------------------------------------------
package * version date source
base * 3.4.3 2017-11-30 local
colorspace 1.3-2 2016-12-14 CRAN (R 3.4.3)
compiler 3.4.3 2017-11-30 local
datasets * 3.4.3 2017-11-30 local
devtools 1.13.5 2018-02-18 CRAN (R 3.4.3)
digest 0.6.15 2018-01-28 CRAN (R 3.4.3)
docopt * 0.4.5 2016-06-13 CRAN (R 3.4.3)
getopt 1.20.2 2018-02-16 CRAN (R 3.4.3)
ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.3)
graphics * 3.4.3 2017-11-30 local
grDevices * 3.4.3 2017-11-30 local
grid 3.4.3 2017-11-30 local
gtable 0.2.0 2016-02-26 CRAN (R 3.4.3)
labeling 0.3 2014-08-23 CRAN (R 3.4.3)
lazyeval 0.2.1 2017-10-29 CRAN (R 3.4.3)
magrittr 1.5 2014-11-22 CRAN (R 3.4.3)
memoise 1.1.0 2017-04-21 CRAN (R 3.4.3)
methods * 3.4.3 2017-11-30 local
munsell 0.4.3 2016-02-13 CRAN (R 3.4.3)
optparse * 1.4.4 2017-07-18 CRAN (R 3.4.3)
pillar 1.1.0 2018-01-14 CRAN (R 3.4.3)
plyr 1.8.4 2016-06-08 CRAN (R 3.4.3)
Rcpp 0.12.15 2018-01-20 CRAN (R 3.4.3)
rlang 0.1.6 2017-12-21 CRAN (R 3.4.3)
rstudioapi 0.7 2017-09-07 CRAN (R 3.4.3)
scales 0.5.0 2017-08-24 CRAN (R 3.4.3)
stats * 3.4.3 2017-11-30 local
stringi 1.1.6 2017-11-17 CRAN (R 3.4.3)
stringr 1.2.0 2017-02-18 CRAN (R 3.4.3)
tibble 1.4.2 2018-01-22 CRAN (R 3.4.3)
tools 3.4.3 2017-11-30 local
utils * 3.4.3 2017-11-30 local
withr 2.1.1 2017-12-19 CRAN (R 3.4.3)
@julian-urbano
Good point! I honestly cannot remember why I thought this was an error and I honestly don't know what I was expecting. I was working at a fever-pitch on finishing my dissertation, so I may not have been thinking clearly.
I thought maybe that it was a behavior only seen when run from Rscript, but when I ran this script:
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library("docopt"))
'usage: my_program.R [-a -r -m <msg>] <file>
options:
-a Add
-r Remote
-m <msg> Message' -> doc
x <- docopt(doc)
print(x)
I get a sensible result.
$ Rscript test.R file.txt
List of 7
$ -a : logi FALSE
$ -r : logi FALSE
$ -m : NULL
$ file : chr "file.txt"
$ a : logi FALSE
$ r : logi FALSE
$ m : NULL
NULL
The only thing I can think of is that I may have been expecting the full help message to show up, but 🤷♂️
All in all, I guess this was a PBKC-type error.
I remember now what I was expecting!
The full help message does not print if the input is not correctly specified:
'usage: my_program.R [-a -r -m <msg>] <file>
options:
-a Add
-r Remote
-m <msg> Message' -> doc
docopt(doc)
## Error:
## usage: my_program.R [-a -r -m <msg>] <file>
One would expect the error message to be:
usage: my_program.R [-a -r -m <msg>] <file>
options:
-a Add
-r Remote
-m <msg> Message
Agreed and good one! However in conflict with staying close to the docopt.py implementation (have to check that)
I found some strange behavior where required options results in an error:
It's fixed if you place it as an optional argument:
Here's the most MWE:
Session Information: