docopt.R icon indicating copy to clipboard operation
docopt.R copied to clipboard

Required options resulting in error

Open zkamvar opened this issue 7 years ago • 14 comments

I found some strange behavior where required options results in an error:

'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>

It's fixed if you place it as an optional argument:

'usage: my_program.R [-a -r -m <msg>] [<file>]

options:
 -a         Add
 -r         Remote
 -m <msg>   Message' -> doc

docopt(doc)

## List of 8
##  $ -a    : logi FALSE
##  $ -r    : logi FALSE
##  $ -m    : NULL
##  $ <file>: NULL
##  $ a     : logi FALSE
##  $ r     : logi FALSE
##  $ m     : NULL
##  $ file  : NULL
## NULL

Here's the most MWE:

'usage: my_program.R <file>' -> doc
docopt(doc)
## Error: 
##  usage: my_program.R <file>

Session Information:

> devtools::session_info()
Session info -------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.1 (2016-06-21)
 system   x86_64, darwin13.4.0        
 ui       RStudio (1.0.8)             
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2016-09-09                  

Packages -----------------------------------------------------------------------------------------
 package  * version date       source        
 devtools   1.12.0  2016-06-24 CRAN (R 3.3.0)
 digest     0.6.10  2016-08-02 CRAN (R 3.3.0)
 docopt   * 0.4.5   2016-06-13 CRAN (R 3.3.0)
 magrittr   1.5     2014-11-22 CRAN (R 3.2.0)
 memoise    1.0.0   2016-01-29 CRAN (R 3.2.3)
 stringi    1.1.1   2016-05-27 CRAN (R 3.3.0)
 stringr    1.0.0   2015-04-30 CRAN (R 3.2.0)
 withr      1.0.2   2016-06-20 cran (@1.0.2) 

zkamvar avatar Sep 09 '16 17:09 zkamvar

Thx for reporting! I will look into at the end of this week

Best,

Edwin

edwindj avatar Sep 28 '16 12:09 edwindj

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.

dbarowy avatar Dec 06 '16 20:12 dbarowy

+1, I would love to have this working in my scripts

rmwthorne avatar Dec 21 '16 16:12 rmwthorne

Bump.

zkamvar avatar Sep 20 '17 19:09 zkamvar

Thx for reminding: will look into this before weekend

edwindj avatar Sep 20 '17 19:09 edwindj

bump

zkamvar avatar Dec 11 '17 15:12 zkamvar

Hi everybody! Just run into the same issue here, when taking the first steps with Docopt. Any fix yet?

martabe avatar Feb 20 '18 09:02 martabe

@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

rmwthorne avatar Feb 20 '18 09:02 rmwthorne

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>
Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, darwin15.6.0
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  tz       America/Chicago
#>  date     2018-02-20
#> Packages -----------------------------------------------------------------
#>  package   * version    date       source
#>  backports   1.1.2      2017-12-13 CRAN (R 3.4.3)
#>  base      * 3.4.3      2017-12-07 local
#>  compiler    3.4.3      2017-12-07 local
#>  datasets  * 3.4.3      2017-12-07 local
#>  devtools    1.13.4     2017-11-09 CRAN (R 3.4.2)
#>  digest      0.6.15     2018-01-28 cran (@0.6.15)
#>  docopt    * 0.4.5      2016-06-13 CRAN (R 3.4.0)
#>  evaluate    0.10.1     2017-06-24 CRAN (R 3.4.1)
#>  formatR     1.5        2017-04-25 CRAN (R 3.4.0)
#>  graphics  * 3.4.3      2017-12-07 local
#>  grDevices * 3.4.3      2017-12-07 local
#>  htmltools   0.3.6      2017-04-28 CRAN (R 3.4.0)
#>  knitr       1.19       2018-01-29 CRAN (R 3.4.3)
#>  magrittr    1.5        2014-11-22 CRAN (R 3.4.0)
#>  memoise     1.1.0      2017-04-21 CRAN (R 3.4.0)
#>  methods   * 3.4.3      2017-12-07 local
#>  Rcpp        0.12.15    2018-01-20 cran (@0.12.15)
#>  rmarkdown   1.8        2017-11-17 cran (@1.8)
#>  rprojroot   1.3-2      2018-01-03 CRAN (R 3.4.3)
#>  stats     * 3.4.3      2017-12-07 local
#>  stringi     1.1.6      2017-11-17 CRAN (R 3.4.2)
#>  stringr     1.2.0      2017-02-18 CRAN (R 3.4.0)
#>  tools       3.4.3      2017-12-07 local
#>  utils     * 3.4.3      2017-12-07 local
#>  withr       2.1.1.9000 2018-01-09 Github (jimhester/withr@df18523)
#>  yaml        2.1.16     2017-12-12 CRAN (R 3.4.3)

zkamvar avatar Feb 20 '18 12:02 zkamvar

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).

julian-urbano avatar Feb 20 '18 13:02 julian-urbano

@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)

martabe avatar Feb 20 '18 14:02 martabe

@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.

zkamvar avatar Feb 20 '18 16:02 zkamvar

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

zkamvar avatar Dec 04 '18 16:12 zkamvar

Agreed and good one! However in conflict with staying close to the docopt.py implementation (have to check that)

edwindj avatar Dec 05 '18 16:12 edwindj