class Comandante::OptParser

Included Modules

Defined in:

comandante/opt_parser.cr

Constant Summary

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 = true end)
VERSION_OPT = OptionConfig.new(name: "version", label: "display version and exit.", action: VersionAction.new)

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.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 Method Detail

def self.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]
def self.assert_option_value(opt, value) #

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


[View source]
def self.version #

[View source]
def self.version=(val : String) #

[View source]

Instance Method Detail

def 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]
def 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]
def args : Array(String) #

Arguments Parsed


[View source]
def auto_help : Bool #

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


[View source]
def auto_help=(auto_help : Bool) #

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


[View source]
def commands_str : String #

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


[View source]
def commands_str=(commands_str : String) #

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


[View source]
def max_width : Int32 #

Controls Maximum text width


[View source]
def max_width=(max_width : Int32) #

Controls Maximum text width


[View source]
def options(id = ROOT_ID) #

Options Parsed


[View source]
def options_str : String #

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


[View source]
def options_str=(options_str : String) #

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


[View source]
def 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]
def print_help(id = ROOT_ID) #

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


[View source]