Flags do not work in v 0.3

Issue #3 resolved
Former user created an issue

after updating to 0.3 (and changing nothing else), for my flag arguments, I get the error

Insufficient number of arguments supplied for --offline

which worked in previous versions with identical call.

Comments (7)

  1. David Shih repo owner

    Hello,

    Thanks for your bug report. In order for me to diagnose this, can you provide some code to demonstrate the bug?

    Thanks, David

  2. David Shih repo owner

    I just tested flags using the example given in add_argument(). Flags seem to be working as expected.

    library(argparser)
    
    p <- arg_parser("A text file modifying program")
    
    p <- add_argument(p, "input", help="input file")
    p <- add_argument(p, "--output", help="output file", default="output.txt")
    p <- add_argument(p, "--append", help="append to file", flag=TRUE)
    
    argv <- parse_args(p, "input.txt");
    stopifnot(argv$append == FALSE);
    stopifnot(argv$input == "input.txt");
    
    argv <- parse_args(p, c("--append", "input.txt"));
    stopifnot(argv$append == TRUE);
    
    argv <- parse_args(p, c("-a", "input.txt"));
    stopifnot(argv$append == TRUE);
    
    # this should throw an error as expected, because <input> is not specified
    argv <- parse_args(p, c("--append"));
    

    You might be using a special combination of flags, positional arguments, and optional arguments, and I cannot diagnose your problem without some example code. Alternatively, the error checking is more stringent in v0.3. If you can provide some minimal code, I could help.

  3. Dmytro Lituiev

    Maybe it is not the behaviour you imply, but by analogy with the pythonic argparse flags and bash optarg, I expected that one can use flags to switch between TRUE/FALSE:

    ###############################################################################################
    library(argparser)
    # Create a parser
    p <- arg_parser("plot p-value QQ-plot")
    
    # Add command line arguments
    p <- add_argument(p,'--permute', flag = FALSE,
                       help='')
    
    # Parse the command line arguments
    argv <- parse_args(p)
    
    ################################################################################################
    
    library(methods)
    
    if (argv$permute){
        write("permuting", file=stderr() )
    } else {
        write(" - ", file=stderr() )
    }
    

    calling: Rscript argtest.R -p

    results in:

    Error in preprocess_argv(argv, parser) :
      Insufficient number of arguments supplied for --permute
    Calls: parse_args -> preprocess_argv
    Execution halted
    
  4. David Shih repo owner

    The flag parameter to add_argument simply indicates whether an argument is a flag or not.

    In your code,

    # Add command line arguments
    p <- add_argument(p,'--permute', flag = FALSE, help='')
    

    --permute is treated as an optional argument, because you set flag = FALSE Therefore, it would expect an argument.

    If you want to set a default value for --permute, then you would do

    # Add command line arguments
    p <- add_argument(p,'--permute', default = FALSE, help='')
    

    You have to call Rscript argtest.R -p TRUE. If you call Rscript argtest.R, then argv$permute will be FALSE, due to the default value.

  5. David Shih repo owner

    If you want to use a flag, so that

    • Rscript argtest.R runs with argv$permute as FALSE, and
    • Rscript argtest.R -p runs with argv$permute as TRUE.

    Then you would do:

    ###############################################################################################
    library(argparser)
    # Create a parser
    p <- arg_parser("plot p-value QQ-plot")
    
    # Add command line arguments
    p <- add_argument(p, '--permute', flag = TRUE, help='')
    
    # Parse the command line arguments
    argv <- parse_args(p)
    
    ################################################################################################
    
    library(methods)
    
    if (argv$permute){
        write("permuting", file=stderr() )
    } else {
        write(" - ", file=stderr() )
    }
    

    By default, all flag arguments take on a value of FALSE, unless the flag parameter is passed in the command line.

    So if you want the program to permute by default,

    • Rscript argtest.R runs with permute as TRUE, and
    • Rscript argtest.R --nopermute runs with permute as FALSE.

    Then you would write:

    ###############################################################################################
    library(argparser)
    # Create a parser
    p <- arg_parser("plot p-value QQ-plot")
    
    # Add command line arguments
    p <- add_argument(p, '--nopermute', flag = TRUE, help='')
    
    # Parse the command line arguments
    argv <- parse_args(p)
    
    ################################################################################################
    
    library(methods)
    permute <- ! argv$nopermute
    
    if (permute){
        write("permuting", file=stderr() )
    } else {
        write(" - ", file=stderr() )
    }
    

    Notice that you set permute manually. You could also work with argv$nopermute directly if you want.

    I implemented flags the way they behave in most programs I know. Suppose --flag is a flag, then

    • program --flag causes program to run with flag as TRUE
    • program (without --flag) causes program to run with flag as FALSE

    Flags in Python's argparse are more flexible, but I didn't see the need for more complexity.

  6. Dmytro Lituiev

    yes, i meant pythonic behaviour of argparse, where True and False are whether default or action values:

    parser.add_argument('--foo', action='store_true')
    parser.add_argument('--bar', action='store_false')
    

    which is a nice-to-have feature seemingly important for mutually exclusive flags. It was just confusing after the Python.

  7. Log in to comment