Skip to contents

A Subparsers R6 object wraps the Python argparse object used to add sub-commands. Objects of this class are returned by the add_subparsers() method of Parser objects.

Methods


Subparsers$new()

Create a new Subparsers object. Users should not call this directly but instead use the add_subparsers() method of a Parser object.

Usage

Subparsers$new(python_code, name)

Arguments

python_code

An internal object that accumulates the Python code used to build the parser.

name

Name of the Python variable holding this object.


Subparsers$add_parser()

Add a sub-command parser.

Usage

Subparsers$add_parser(...)

Arguments

...

Arguments cleaned and passed to Python's add_parser() method. The first argument is usually the name of the sub-command. Accepts the same arguments as ArgumentParser() as well as help and aliases.

Returns

A Parser object.

Examples

if (argparse:::detects_python()) {
  parser <- ArgumentParser(prog = "PROG")
  parser$add_argument("--foo", action = "store_true", help = "foo help")
  subparsers <- parser$add_subparsers(help = "sub-command help")
  parser_a <- subparsers$add_parser("a", help = "a help")
  parser_a$add_argument("bar", type = "integer", help = "bar help")
  parser$parse_args(c("a", "12"))
}
#> $bar
#> [1] 12
#> 
#> $foo
#> [1] FALSE
#>