class Comandante::OptParser
inherits Reference

Included modules

Comandante::OptParserTypes

Constants

COLOR_OFF_OPT = OptionConfig.new(name: "no-color", short: "C", label: "color mode off.", action: ColorOffAction.new)

DEBUG_OPT = OptionConfig.new(name: "debug", short: "D", label: "debug mode.", action: DebugAction.new)

Built-ins

ERROR_MSGS = {invalid_option: "invalid option '%s'!", bad_command: "bad sub_command '%s'!", wrong_argument_count: "wrong argument count!", option_requires_value: "option --%s requires a value"}

Todo

cleanups for this file :nodoc:

ROOT_ID = "/"

ID for the main command, used internally

VERBOSE_OPT = OptionConfig.new(name: "verbose", short: "v", label: "verbose mode.", simple_action: OptParser::OptProc.new do |v| Cleaner.verbose = trueend)

VERSION_OPT = OptionConfig.new(name: "version", label: "display version and exit.", action: VersionAction.new)

Constructors

.new(name, label, description = "", arguments_string = "", arguments_range = 0..MAX_ARGS)

Creates the parser for the program. At lease a name and label are required.

Examples:

For a simple program that will not use subcommands, and accepts 1 argument

opts = OptParser.new(NAME, LABEL, DESC,
  argument_string: "FILE",
  argument_range: 1..1)

For a program that will have subcommands

opts = OptParser.new(NAME, LABEL, DESC)
View source

Class methods

.assert_arg_count(args : Array(String), range : Range(Int32, Int32))

Asserts argument count is in range, used internally, no need to call this directly

View source

.assert_option_value(opt, value)

Asserts option has a value set or a default, used internally, no need to call this directly

View source

.version

View source

.version=(val : String)

View source

Methods

#append(cmd : CommandConfig)

Appends Configuration for a subcommand, including its options.

Examples:

Adding a subcommand

opts.append(EVAL_OPTS)

where EVAL_OPTS is a OptParserTypes::CommandConfig

View source

#append_option(opt : OptionConfig, id = ROOT_ID)

Appends configuration for an option, id is the name/id of the subcommand, when not given option is added to the main/root command.

Examples:

Adding to main command

opts.append_option(DEBUG_OPT)

where DEBUG_OPT is an OptParserTypes::OptionConfig

Although you can also use this method to add options to subcommands, prefered way is to use append and configure subcommands with all options that way.

View source

#args

Arguments Parsed

View source

#auto_help

Controls to add or not to add a help option to command+subcommands

View source

#auto_help=(auto_help : Bool)

Controls to add or not to add a help option to command+subcommands

View source

#commands_str

The header to user for subcommands (defaults are usually fine)

View source

#commands_str=(commands_str : String)

The header to user for subcommands (defaults are usually fine)

View source

#max_width

Controls Maximum text width

View source

#max_width=(max_width : Int32)

Controls Maximum text width

View source

#options(id = ROOT_ID)

Options Parsed

View source

#options_str

The header to use for options (defaults are usually fine)

View source

#options_str=(options_str : String)

The header to use for options (defaults are usually fine)

View source

#parse(args = ARGV, id = ROOT_ID)

Do the actual parsing, called after all subcommands and options are added to OptParser object. Will call any action associated with subcommands and options If you have no subcommands then you'll probably want to call .options and .args to get the options and arguments passed to the program.

View source

#print_help(id = ROOT_ID)

Is called automatically but just in case you want to call it manually

View source