- edited description
Flag with default value as TRUE
Hi, I found that the add_argument function doesn't work with default value as TRUE.
An minimum example flag_error.R is attached. It looks like:
#!/usr/bin/env Rscript
library(argparser, quietly = TRUE)
p <- arg_parser("Hi!")
p <- add_argument(p, arg = "--flag", short = "-f", flag = TRUE, default = TRUE, help = "a flag")
args <- parse_args(p)
print(args)
Without -f:
./flag_error.R
1 [1] FALSE
$help [1] FALSE
$flag [1] TRUE
$opts [1] NA
With -f:
./flag_error.R -f
Error in preprocess_argv(argv, parser) :
Insufficient number of arguments supplied for --flag
Calls: parse_args -> preprocess_argv
Execution halted
Is there any way to set a flag with default value as TRUE?
Comments (5)
-
reporter -
repo owner As mentioned in
?add_argument
, flags do not consume a value.Flags are always in the off (
FALSE
) state by default. This is the common behaviour of most Linux programs.If you really want to use a Boolean option, don't set the option as a flag. Just specify a default value of
TRUE
orFALSE
.Beware that the definition of
TRUE
andFALSE
vary among programming/scripting languages, and some users may expect1
to be converted toTRUE
and0
to be converted toFALSE
. This is not the case in R.library(argparser, quietly=TRUE); pr <- arg_parser("Test binary") pr <- add_argument(pr, "--binary", default = TRUE, help = "a binary option") parse_args(pr); parse_args(pr, c("-b", "FALSE")); # FALSE parse_args(pr, c("-b", "False")); # FALSE parse_args(pr, c("-b", "false")); # FALSE parse_args(pr, c("-b", "F")); # FALSE parse_args(pr, c("-b", "f")); # NA parse_args(pr, c("-b", "0")); # NA parse_args(pr, c("-b", "TRUE")); # TRUE parse_args(pr, c("-b", "True")); # TRUE parse_args(pr, c("-b", "true")); # TRUE parse_args(pr, c("-b", "T")); # TRUE parse_args(pr, c("-b", "t")); # NA parse_args(pr, c("-b", "1")); # NA
-
repo owner - changed status to invalid
Not a bug.
-
Hi David,
Thanks for your explanation!!! This library save me a lot of time.
In Python, it is possible to add an argument with action = store_true or store_false to adjust the default value of a logical argument. But I think your point of view is also good and reasonable.
Thanks for your great work!Sincerely,
Mansun -
repo owner The similarity of
argparser
to Python's argparse has caused some confusion. I added some clarification to?add_argument
in the latest commit. I'll add some examples at some point. - Log in to comment