Feature request: enable default positional arguments

Issue #4 resolved
Dmytro Lituiev created an issue

Hi, Thank you very much for this package, it is really very useful and practical.

I wonder if one could add support of defaults for positional arguments? So far it does not complain if I provide a default, but if I run it says:

     Missing required arguments

Example:

##################################
library(argparser)
# Create a parser
p <- arg_parser("plot p-value QQ-plot")

# Add command line arguments
p <- add_argument(p, "intact", help="number to round", type="character", 
                  default= "results/intact.RData")

p <- add_argument(p, "permuted", help="number to round", type="character",
                  default = "results/permuted.RData") 

# Parse the command line arguments
argv <- parse_args(p)
#############################

Comments (3)

  1. David Shih repo owner

    Thank you for your feature request. I think you should be using optional arguments if you want to set a default value. I agree that it is strange that add_argument does not complain when you try to set a default value for positional arguments. (I'll fix this.)

    For clarity, it might be best to designate arguments as either positional or optional. When there are more than one positional argument that is optional, how do you know which supplied position argument corresponds to which required position argument? You might be able to handle this ambiguity for special cases, but it would be difficult to handle this problem in general.

    Try this instead:

    ##################################
    library(argparser)
    # Create a parser
    p <- arg_parser("plot p-value QQ-plot")
    
    # Add command line arguments
    p <- add_argument(p, "--intact", help="number to round", type="character", 
                      default= "results/intact.RData")
    
    p <- add_argument(p, "--permuted", help="number to round", type="character",
                      default = "results/permuted.RData") 
    
    # Parse the command line arguments
    argv <- parse_args(p, c("--permuted", "path/to/permuted.RData"))
    
    # argv$intact == "results/permuted.RData"
    # argv$permuted == "path/to/permuted.RData"
    

    If you want to supply an arbitrary number of values for a position arguments, use the nargs parameter. (I just implemented this in the last version, mainly for optional arguments... I'll need to do more testing to ensure that it works properly for positional arguments.)

  2. David Shih repo owner

    In your example, suppose you allow both intact and permuted positional arguments to be optional, and you call the program with:

    script.R  file.RData
    

    How would you know whether file.RData is the input value to intact or to permuted? Of course, you can define a rule where the supplied value is always consumed by the first or the last positional argument, but this can introduce confusion and silent bugs.

    To summarize,

    • If you want an argument to be optional, use --arg and supply a default value.
    • If you want an argument to be required, use arg.
    • If you want a flag, use --arg and flag = TRUE.
  3. Log in to comment