braces are being stripped from text

Issue #31 resolved
Mike Potts created an issue

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)

  1. David Shih repo owner

    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:

    #!/usr/bin/env Rscript
    
    library(argparser)
    library(magrittr)
    
    parser <- arg_parser("test") %>% add_argument("--query", help = "query string", type = "character", nargs = 1)
    argv <- parse_args(parser)
    
    print(argv$query)
    

    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.

  2. Log in to comment