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

spaces in --arg stye are not treated as expected

Open lorenzwalthert opened this issue 4 years ago • 4 comments

Unfortunately, #19 does not seem resolved, in fact made things worse for my use case. I caught the command line args with commandArgs(TRUE) so I you should be able to reproduce this easily:

With the CRAN version

#!/usr/bin/env Rscript

"style files.
Usage:
  style_files [--arg=<arg1>] <files>...

Options:
  --arg=<arg1>  Package where the style guide is stored [default: Arg1].

" -> doc
saveRDS(commandArgs(TRUE), "cmd.rds")

# expected behavior
docopt::docopt(doc, c("--arg=tidyverse_style(scope=\"none\")", "R/test.R"))
#> List of 4
#>  $ --arg  : chr "tidyverse_style(scope=\"none\")"
#>  $ <files>: chr "R/test.R"
#>  $ arg    : chr "tidyverse_style(scope=\"none\")"
#>  $ files  : chr "R/test.R"
#> NULL

# unexpected: I add a space, it does not work anymore
docopt::docopt(doc, c("--arg=tidyverse_style(scope = \"none\")", "R/test.R"))
#> List of 4
#>  $ --arg  : chr "tidyverse_style(scope"
#>  $ <files>: chr [1:3] "=" "none)" "R/test.R"
#>  $ arg    : chr "tidyverse_style(scope"
#>  $ files  : chr [1:3] "=" "none)" "R/test.R"
#> NULL

Created on 2020-06-15 by the reprex package (v0.3.0)

With the GitHub version

#!/usr/bin/env Rscript

"style files.
Usage:
  style_files [--arg=<arg1>] <files>...

Options:
  --arg=<arg1>  Package where the style guide is stored [default: Arg1].

" -> doc
saveRDS(commandArgs(TRUE), "cmd.rds")

# unexpected: Seems like spaces are removed around 'none'.
docopt::docopt(doc, c("--arg=tidyverse_style(scope=\"none\")", "R/test.R"))
#> List of 4
#>  $ --arg  : chr "tidyverse_style(scope=none)"
#>  $ <files>: chr "R/test.R"
#>  $ arg    : chr "tidyverse_style(scope=none)"
#>  $ files  : chr "R/test.R"
#> NULL

# unexpected: something else went wrong here
docopt::docopt(doc, c("--arg=tidyverse_style(scope = \"none\")", "R/test.R"))
#> List of 4
#>  $ --arg  : chr "tidyverse_style(scope"
#>  $ <files>: chr [1:3] "=" "none)" "R/test.R"
#>  $ arg    : chr "tidyverse_style(scope"
#>  $ files  : chr [1:3] "=" "none)" "R/test.R"
#> NULL

Created on 2020-06-15 by the reprex package (v0.3.0)

lorenzwalthert avatar Jun 15 '20 20:06 lorenzwalthert

Thanks for the quick feedback! I will look into it today

edwindj avatar Jun 16 '20 06:06 edwindj

Fixed code does this:

#!/usr/bin/env Rscript

"style files.
Usage:
  style_files [--arg=<arg1>] <files>...

Options:
  --arg=<arg1>  Package where the style guide is stored [default: Arg1].

" -> doc

# expected behavior
docopt::docopt(doc, c("--arg=tidyverse_style(scope= \"none\")", "R/test.R"))
## List of 4
##  $ --arg  : chr "tidyverse_style(scope= \"none\")"
##  $ <files>: chr "R/test.R"
##  $ arg    : chr "tidyverse_style(scope= \"none\")"
##  $ files  : chr "R/test.R"
## NULL

edwindj avatar Jun 16 '20 15:06 edwindj

Looks great, will try in {precommit} tonight. Thanks a ton @edwindj.

lorenzwalthert avatar Jun 16 '20 15:06 lorenzwalthert

I did some experimentation, here's what I found:

# works
"--style_transformers=tidyverse_style(scope = \"none\")"
'--style_transformers=tidyverse_style(scope = "none")'
--style_transformers=tidyverse_style(scope = "none")


# does not
'--style_transformers=tidyverse_style(scope = \'none\')'
"--style_transformers=tidyverse_style(scope = 'none')"

So there are more options that work than options that don't work. 😄 Even though things could be further improved, I appreciate all the work @edwindj already put into this and we can call it a day form my side. Will just tell people to the right quotes in the right place.

lorenzwalthert avatar Jun 17 '20 19:06 lorenzwalthert