Flags do not work in v 0.3
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)
-
repo owner -
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.
-
Maybe it is not the behaviour you imply, but by analogy with the pythonic
argparse
flags and bashoptarg
, 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
-
repo owner The
flag
parameter toadd_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 setflag = 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 callRscript argtest.R
, thenargv$permute
will beFALSE
, due to the default value. -
repo owner If you want to use a flag, so that
Rscript argtest.R
runs withargv$permute
asFALSE
, andRscript argtest.R -p
runs withargv$permute
asTRUE
.
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 withpermute
asTRUE
, andRscript argtest.R --nopermute
runs withpermute
asFALSE
.
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, thenprogram --flag
causesprogram
to run with flag asTRUE
program
(without--flag
) causesprogram
to run with flag asFALSE
Flags in Python's
argparse
are more flexible, but I didn't see the need for more complexity. -
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.
-
repo owner - changed status to resolved
- Log in to comment
Hello,
Thanks for your bug report. In order for me to diagnose this, can you provide some code to demonstrate the bug?
Thanks, David