class CmdParse::Command
Base class for commands¶ ↑
This class implements all needed methods so that it can be used by the CommandParser class.
Commands can either be created by sub-classing or on the fly when using the add_command method. The latter allows for a more terse specification of a command while the sub-class approach allows to customize all aspects of a command by overriding methods.
Basic example for sub-classing:
class TestCommand < CmdParse::Command def initialize super('test', takes_commands: false) options.on('-m', '--my-opt', 'My option') { 'Do something' } end end parser = CmdParse::CommandParser.new parser.add_command(TestCommand.new) parser.parse
Basic example for on the fly creation:
parser = CmdParse::CommandParser.new parser.add_command('test') do |cmd| takes_commands(false) options.on('-m', '--my-opt', 'My option') { 'Do something' } end parser.parse
Basic Properties¶ ↑
The only thing that is mandatory to set for a Command is its name. If the command does not take any sub-commands, then additionally an action block needs to be specified or the execute method overridden.
However, there are several other methods that can be used to configure the behavior of a command:
takes_commands-
For specifying whether sub-commands are allowed.
options-
For specifying command specific options.
add_command-
For specifying sub-commands if the command takes them.
Help Related Methods¶ ↑
Many of this class' methods are related to providing useful help output. While the most common methods can directly be invoked to set or retrieve information, many other methods compute the needed information dynamically and therefore need to be overridden to customize their return value.
short_desc-
For a short description of the command (getter/setter).
long_desc-
For a detailed description of the command (getter/setter).
argument_desc-
For describing command arguments (setter).
help,help_banner,help_short_desc,help_long_desc,help_commands,help_arguments,help_options-
For outputting the general command help or individual sections of the command help (getter).
usage,usage_options,usage_arguments,usage_commands-
For outputting the usage line or individual parts of it (getter).
Built-in Commands¶ ↑
cmdparse ships with two built-in commands:
-
HelpCommand(for showing help messages) and -
VersionCommand(for showing version information).
Attributes
Returns the mapping of command name to command for all sub-commands of this command.
A data store (initially an empty Hash) that can be used for storing anything. For example, it can be used to store option values. cmdparse itself doesn't do anything with it.
Returns the name of the default sub-command or nil if there isn't any.
The name of the command.
Sets or returns the super-command of this command. The super-command is either a Command instance for normal commands or a CommandParser instance for the main command (ie. CommandParser#main_command).
Public Class Methods
Initializes the command called name.
Options:
takes_commands-
Specifies whether this command can take sub-commands.
Public Instance Methods
For sorting commands by name.
Adds a command to the command list.
The argument command can either be a Command object or a String in which case a new Command object is created. In both cases the Command object is yielded.
If the optional argument default is true, then the command is used when no other sub-command is specified on the command line.
If this command takes no other commands, an error is raised.
Sets the descriptions for one or more arguments using name-description pairs.
The used names should correspond to the names used in usage_arguments.
Returns the command chain, i.e. a list containing this command and all of its super-commands, starting at the top level command.
Returns the associated CommandParser instance for this command or nil if no command parser is associated.
Invokes the action block with the parsed arguments.
This method is called by the CommandParser instance if this command was specified on the command line to be executed.
Sub-classes can either specify an action block or directly override this method (the latter is preferred).
Returns a string containing the help message for the command.
Returns the formatted arguments of this command.
For the output format see cond_format_help_section
Returns the formatted sub-commands of this command.
For the output format see cond_format_help_section
Returns the formatted detailed description.
For the output format see cond_format_help_section
Returns the formatted option descriptions for the given OptionParser instance.
The section title needs to be specified with the title argument.
For the output format see cond_format_help_section
Returns the formatted short description.
For the output format see cond_format_help_section
Sets the detailed description of the command if an argument is given. Always returns the detailed description.
This may be a single string or an array of strings for multiline description. Each string is ideally shorter than 76 characters.
This hook method is called when the command (or one of its super-commands) is added to another Command instance that has an associated command parser (see command_parser).
It can be used, for example, to add global options.
Yields the OptionParser instance that is used for parsing the options of this command (if a block is given) and returns it.
Sets the short description of the command if an argument is given. Always returns the short description.
The short description is ideally shorter than 60 characters.
Returns true if the command can take one or more arguments.
Sets whether this command can take sub-command.
The argument val needs to be true or false.
Return true if this command can take sub-commands.
Returns the usage line for the command.
The usage line is automatically generated from the available information. If this is not suitable, override this method to provide a command specific usage line.
Typical usage lines looks like the following:
Usage: program [options] command [options] {sub_command1 | sub_command2}
Usage: program [options] command [options] ARG1 [ARG2] [REST...]
Returns a string describing the arguments for the command for use in the usage line.
By default the names of the action block or execute method arguments are used (done via Ruby's reflection API). If this is not wanted, override this method.
A typical return value would look like the following:
ARG1 [ARG2] [REST...]
See: usage, argument_desc
Returns a string describing the sub-commands of the commands for use in the usage line.
Override this method for providing a command specific specialization.
A typical return value would look like the following:
{command | other_command | another_command }
Returns a string describing the options of the command for use in the usage line.
If there are any options, the resulting string also includes a leading space!
A typical return value would look like the following:
[options]
See: usage
Protected Instance Methods
Conditionally formats a help section.
Returns either the formatted help section if the condition is true or an empty string otherwise.
The help section starts with a title and the given lines are indented to easily distinguish different sections.
A typical help section would look like the following:
Summary:
help - Provide help for individual commands
Options:
- condition
-
The formatted help section is only returned if the condition is
true. - indent
-
Whether the lines should be indented with
CommandParser#help_indentspaces. - preformatted
-
Assume that the given lines are already correctly formatted and don't try to reformat them.
Returns the text in content formatted so that no line is longer than width characters.
Options:
- width
-
The maximum width of a line. If not specified, the
CommandParser#help_line_widthvalue is used. - indent
-
This option specifies the amount of spaces prepended to each line. If not specified, the
CommandParser#help_indentvalue is used. - indent_first_line
-
If this option is
true, then the first line is also indented.