braces are being stripped from text
Issue #31
resolved
I’ve been using argparser very happily, but I just tracked down a bug in my code, and it’s caused by argparser stripping braces from the text of a character argument:
library(argparse)
parser <- ArgumentParser("test") %>% add_argument("--query", help = "query string", type = "character", nargs = Inf)
argv <- parse_args(parser)
print(argv$query)
Result:
$./myscript -q “A AND (B OR C)”
[1] "A AND B OR C"
No amount of escaping the braces makes any difference, and I noticed there is explicit brace stripping code in the parser, but apparently no way to disable it. Is this an unsupported use case, or am I missing something obvious?
Comments (3)
-
repo owner -
reporter Oh yes of course! I should have realised Thanks for the quick explanation!!
-
reporter - changed status to resolved
User error
- Log in to comment
It looks like
query
should be accepting a single argument that is quoted.The quotes in
$./myscript -q “A AND (B OR C)”
is simply not optional because that is handled by the shell. The only way to protect whitespace is to quote the argument as you have done.Therefore, your program should be:
The key here is
nargs = 1
. You are accepting a single argument that is an arbitrary expression.When you specify
nargs = Inf
,argparser
needs to parse the ensuing arguments for,
and()
, because it s expecting a tuple of some sort:'(1, 2, 3, 4)'
'(1,2,3,4
)''(1 2 3 4)'
are all supported quoted expression, and they all get turned into
1 2 3 4
That’s why the brackets are stripped.