defineOptions-package {defineOptions}R Documentation

Define and Parse Command Line Options

Description

Parses command line arguments and supplies values to scripts. Users can specify names to which parsed inputs are assigned, value types into which inputs are cast, long options or short options, input splitters and callbacks that define how options should be specified and how input values are supplied.

Details

Definitions are consturcted by calling define_option method for ParserDef object, which is instantiated by new_parser_def function. The second argument of define_option takes a list that has defition about how to parse and store its option value. The definition also holds information about how to behave when the option is not specified. Finally, parse_with_defs function takes command line arguments and ParserDef object and returns parsing result.

Author(s)

NA Maintainer: Toshihiro Umehara <toshi@niceume.com>

See Also

ParserDef new_parser_def define_option parse_with_defs callbacks

Examples

library(defineOptions)
parser_def = new_parser_def() |>
    define_option(
        list(
            def_name = "target_range",
            def_type = "integer",
            long_option = "--target-range",
            short_option = "-r",
            input_splitter = ",",
            callback = opt_optional_input_required( input_when_omitted = "70,180" )
        )
    ) |>
    define_option(
        list(
            def_name = "exclude_weekend",
            def_type = "logical",
            long_option = "--exclude-weekend",
            callback = opt_optional_input_disallowed( input_when_specified = "TRUE",
                                                      input_when_omitted = "FALSE" )
        )
    )|>
    define_option(
        list(
            def_name = "output_path",
            def_type = "character",
            long_option = "--output",
            callback = opt_required_input_required()
        )
    )

# In practice, command line arguments can be obtained by commandArgs() function
# with trailingOnly option TRUE.
# command_arguments = commandArgs(trailingOnly = TRUE)

example_string = "input1.txt input2.txt --target-range 60,140 --exclude-weekend --output log.data"
command_arguments = strsplit( example_string, " ")[[1]]

parsed_args = parse_with_defs( parser_def, command_arguments)
print(parsed_args)

[Package defineOptions version 0.9 Index]