Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded space in parameter values #11

Closed
jennybc opened this issue Jun 8, 2015 · 20 comments
Closed

Embedded space in parameter values #11

jennybc opened this issue Jun 8, 2015 · 20 comments

Comments

@jennybc
Copy link

jennybc commented Jun 8, 2015

Thanks for providing this docopt implementation for R!

I was pleased to see it can handle quoted arguments. I'm a little surprised that the example below -- where I have spaces embedded in a parameter value -- doesn't work.

Homework & context:

  • I note the docopt R package matches the behaviour I get here (http://try.docopt.org), so your package definitely matches the reference implementation in this case update: I did not actually compare against the reference, so will just limit my comment to doctopt.org. But even lots of unix command line utilities allow for embedded space, possibly escaped, in parameter values. So why not docopt?
  • I did read through the thread and commit re: Cannot parse quoted arguments #4 but that discussion focused on a different situation, i.e. where the quotes enclose the combination of the option (short or long form) and its value.

Sample R script foo.R:

#!/usr/bin/Rscript

suppressPackageStartupMessages(library(docopt))

"docopt practice script

Usage: foo.R [-i <integers>]

Options:
     -i <integers>, --integers=<integers>  Integers [default: 1]
" -> doc

"%||%" <- function(a, b) if (!is.null(a)) a else b
opts <- docopt(doc)
my_ints <- opts$integers %||% opts$i
my_ints <- as.integer(eval(parse(text = my_ints)))
cat(sprintf("integers = %s\n", paste(my_ints, collapse = ", ")))

Basically I want to pass an R expression that will generate an atomic vector of integers. Some successful usage and some less successful:

Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R
integers = 1
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -i 4
integers = 4
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -i 2:4
integers = 2, 3, 4
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -i 'c(1,8)'
integers = 1, 8
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -i 'c(1, 8)'
Error in docopt(doc) : usage: foo.R [-i <integers>]
Execution halted
Jennifers-MacBook-Pro-3:scripts jenny$ ./foo.R -i 'c(1,\ 8)'
Error in docopt(doc) : usage: foo.R [-i <integers>]
Execution halted

Obviously I can just avoid using spaces! But it got me wondering about the general question. Any thoughts?

BTW you might find it interesting that something special and bad happens if you try to use an option with short form -g: a question I asked on stackoverflow.

@keleshev
Copy link
Member

keleshev commented Jun 9, 2015

The try.docopt.org doesn't handle shell escaping rules—it just splits the string on whitespace unfortunately.

However, the reference implementation does what you expect:

"""docopt practice script

Usage: foo.R [-i <integers>]

Options:
     -i <integers>, --integers=<integers>  Integers [default: 1]

"""
from docopt import docopt

print(docopt(__doc__))

Given '-i c(1, 8)' this prints {'--integers': ' c(1, 8)'}. -i 'c(1, 8)' also works.

So this seems to be an error in R port.

@edwindj
Copy link
Member

edwindj commented Jun 9, 2015

Thanks for reporting! I will look into it tommorow.

Best regards,

Edwin

@edwindj
Copy link
Member

edwindj commented Jun 10, 2015

@jennybc: I have added a test with your example and it seems to works with the latest github version.

The problem was that base::commandArgs which is used by docopt.R removes quotes.

Question: I would like to change the methods "Depends" into an "Imports". Before R 3.2 this was a problem (aka R bug). Would it be a problem for you if the CRAN update is (R >= 3.2)? Otherwise I save that change for later.

Best,

Edwin

@edwindj edwindj closed this as completed Jun 10, 2015
@jennybc
Copy link
Author

jennybc commented Jun 10, 2015

Thanks @edwindj for the fix! I would be fine if the CRAN update requires R >= 3.2, so go right ahead as far as I'm concerned.

@malcook
Copy link

malcook commented Jul 9, 2015

Hi @edwindj,

I'm not seeing this as fixed (or not understanding the intention of quoted_args)

My script, qtest, is as follows:

#!/usr/bin/env Rscript
'usage: qtest <s> [<more>...]
' -> doc

str(commandArgs(TRUE))
suppressPackageStartupMessages({
    ## load the docopt library
    library(docopt)
})

opt <- docopt(doc, quoted_args=TRUE)
str(opt)  

I would expect that opt$s would be "hello world" when called as below, but, alas it is not.

./qtest "hello world"
 chr "hello world"
List of 4
 $ <s>   : chr "hello"
 $ <more>: chr "world"
 $ s     : chr "hello"
 $ more  : chr "world"
 - attr(*, "class")= chr [1:2] "docopt" "list"

Can you advise?

Note the script shows that commandArgs is seeing "hello world" as a single value.

I am using docopt_0.4.4 on R version 3.2.1 (2015-06-18)

@edwindj edwindj reopened this Jul 10, 2015
@edwindj
Copy link
Member

edwindj commented Jul 10, 2015

@malcook Thanks for reporting: it is a bug, that only happens when one argument is given:

../qtest "hello world" more does work...

I will look into it tomorrow

Best,

Edwin

@edwindj
Copy link
Member

edwindj commented Jul 10, 2015

@malcook: should be fixed...

@cysouw
Copy link

cysouw commented Nov 11, 2015

It still does not work here, even after installing the latest updates. Try this really minimal example:

#!/usr/bin/env Rscript
library(docopt)
DOC <- "USAGE: foo ARG"
attach(docopt(DOC))
print(ARG)

It will work without spaces, but not with spaces, e.g. foo bla works, but not foo 'bla bla'

Also note that passing a star does not work (which is useful to mimic typical bash usage to execute something on a whole directory): foo * does not work, you have to use foo '*', which is rather un-bash-like...

Great work though, and please keep up this great project!

@randomee
Copy link

This seems to be still broken.

I'm working around it for now by embedding _ where I'd like a space, then using gsub inside R to clean it up. Not ideal.

Any chance this could get another look?
Thanks

@edwindj
Copy link
Member

edwindj commented Mar 29, 2016

I will be working on it the end of this week

@randomee
Copy link

Any luck on this?

Thanks

@ajwnewkirk
Copy link

I'm also curious about the status of this. Just taught a class on docopt to 40+ people in my agency yesterday, and this question generated a lot of interest when it came up.

@edwindj
Copy link
Member

edwindj commented May 2, 2016

I may have solved this a long time ago, but did not upload it yet to CRAN.
Could you please check if your problem is solved with the current version on github

devtools::install_github('docopt/docopt.R')

If fixed, I will uploaded it to CRAN asap, so it will be official.

Best,
Edwin

@ajwnewkirk
Copy link

Thanks for the response, Edwin.

I installed the GitHub version and ran a minimal example similar to the one cysouw used above:

#!/usr/bin/Rscript

library(docopt)
doc <- "USAGE: foo ARG"
args <- docopt(doc = doc, quoted_args = TRUE)
print(sessionInfo())
print(args$ARG)

I get the following output on my system:

GIGte_programmaticR$ ./test.R Jo
Loading required package: methods
R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] methods stats graphics grDevices utils datasets base

other attached packages:
[1] docopt_0.4.5

loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.2.5 stringi_1.0-1 stringr_1.0.0
[1] "Jo"

GIGte_programmaticR$ ./test.R "Jo Williams"
Loading required package: methods
Error: usage: foo ARG
Execution halted
GIGte_programmaticR$ ./test.R 'Jo Williams'
Loading required package: methods
Error: usage: foo ARG
Execution halted

Am I missing something? Thank you for your help.

@edwindj
Copy link
Member

edwindj commented May 2, 2016

Thanks for your detailed report!
I think I found the problem; it was a subtle difference between Windows and Ubuntu.

I have made some changes:
Could you check again if your problem is solved? (your code is now running fine on my Ubuntu 15.10).

@cysouw
Copy link

cysouw commented May 3, 2016

YES! works here now
spaces work. I'll make a new issue about stars :-)
best
michael

R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.4 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] methods stats graphics grDevices utils datasets base

other attached packages:
[1] docopt_0.4.5

loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.2.2 stringi_1.0-1 stringr_1.0.0

@ajwnewkirk
Copy link

The minimal example I posted is now working on my system as well - thank you!

@burgerga
Copy link

burgerga commented Jun 8, 2016

Any idea on when this will make it to CRAN?

@edwindj
Copy link
Member

edwindj commented Jun 8, 2016

Thx for reminding :-), I will send it to CRAN within two days.

@edwindj edwindj closed this as completed Jun 8, 2016
@edwindj edwindj reopened this Jun 8, 2016
@edwindj
Copy link
Member

edwindj commented Jun 13, 2016

on its way to CRAN (will close it when accepted)

@edwindj edwindj closed this as completed Jun 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants