Skip to content

Support custom function 'type' in 'add_argument()' #37

Description

@ellendejong

Hi,

Is it possible to provide a custom function to type argument in add_argument?
For example, something similar to:

non_empty_existing_file <- function(inputfile) {
  if(!file.exists(inputfile)) { 
    stop("Non existing input path:", inputfile)
  } else if(file.info(inputfile)$size == 0) {  # check empty
    stop("Input file is empty:", inputfile)
  }
  return(inputfile)
}
parser = ArgumentParser(prog='PROG')
parser$add_argument('input_file', type=non_empty_existing_file(), help="Input file.")
args <- parser$parse_args(commandArgs(TRUE))

Currently, this example does not work, because input_file value is not passed to non_empty_existing_file.
I haven't seen any examples using custom types and it would be really useful.
If it is supported already, could you add some examples to the documentation?

Thanks in advance.

Ellen

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions