docopt/docopt.R

Mishandling of spaces in arguments' values

Closed this issue · 8 comments

Hi guys,
I cannot seem to have doctopt to properly handle argument values that contain spaces. Here is a reproducible example:

#!/usr/bin/env Rscript --vanilla

library("docopt")

"usage: test.R <A> [options]

 <A>            Dummy positional argument
 -B <value>...  Dummy optional argument
" -> syntax

print(docopt::docopt(syntax))

If I call this script with ./test.R foo -B bar, everything is fine. But the following are considered errors:

  • ./test.R foo -B "bar baz"
  • ./test.R foo -B 'bar bar'
  • ./test.R foo -b bar baz (this last one to be expected)

Best,
Aurélien

Same issue here. Might be related, see

http://stackoverflow.com/questions/26130724/how-to-pass-a-docopt-argument-where-the-argument-is-a-string-with-spaces-in-pyth.

Edit: Actually, issue on my code is with use of the =. For instance, error occurs when I use --arg= 'some string', but works when I do --arg 'some string'.

Two years have passed, and the OPs script above seems to work for me. I have other space-related problems but this issue might warrant closing.

agree with @vforget. Here's a reprex:

#!/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

arguments <- docopt::docopt(doc)
print(arguments)
saveRDS(arguments, 'argument.rds')

No problem here:

(base) lorenz precommit $ ~/short-term/docopt-error --arg='ab' f1 f2                                √ 22:57:33
List of 4
 $ --arg  : chr "ab"
 $ <files>: chr [1:2] "f1" "f2"
 $ arg    : chr "ab"
 $ files  : chr [1:2] "f1" "f2"
NULL

But this does not work:

(base) lorenz precommit $ ~/short-term/docopt-error --arg='a b' f1 f2                              ×1 22:56:34
List of 4
 $ --arg  : chr "Arg1"
 $ <files>: chr [1:3] "--arg=a b" "f1" "f2"
 $ arg    : chr "Arg1"
 $ files  : chr [1:3] "--arg=a b" "f1" "f2"
NULL

Thanks for reporting!
I am looking into it! FWIW:

~/short-term/docopt-error --arg 'a b' f1 f2       

(without the assignment sign), does work.

Jup, true. Thanks @edwindj, it would be awesome to see this fixed. If it motivates you: this edge case will be used in {precommit} to allow people to select the style guide for formatting, as described in lorenzwalthert/precommit#178 (comment).

I seem to have fixed the issue.
Please reopen if necessary :-)
I will upload this version to CRAN on Wednesday. So please let me know if this fix is breaking other stuff.

Perfect, I'll take a look.

Unfortunately not -.-, I filed #40 to describe exactly what does not work as expected.