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' }

parser =

Basic example for on the fly creation:

parser =
parser.add_command('test') do |cmd|
  options.on('-m', '--my-opt', 'My option') { 'Do something' }

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:


For specifying whether sub-commands are allowed.


For specifying command specific options.


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.


For a short description of the command (getter/setter).


For a detailed description of the command (getter/setter).


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).



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

new(name, takes_commands: true)

Initializes the command called name.



Specifies whether this command can take sub-commands.

Public Instance Methods


For sorting commands by name.


Sets the given block as the action block that is used on when executing this command.

If a sub-class is created for specifying a command, then the execute method should be overridden instead of setting an action block.

See also: execute

add_command(other_command, default: false) {|cmd| ... } → command
add_command('other', default: false) {|cmd| ...} → command

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.

argument_desc(name → desc, ...)

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 number of arguments required for the execution of the command, i.e. the number of arguments the action block or the execute method takes.

If the returned number is negative, it means that the minimum number of arguments is -n-1.

See: Method#arity, Proc#arity

command_chain → [top_level_command, super_command, ..., command]

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 banner (including the usage line) of the command.

The usage line is command specific but the rest is the same for all commands and can be set via command_parser.main_options.banner.


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

help_options(title, options)

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.

Also aliased as: long_desc=
Alias for: long_desc

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.

options {|opts| ...} → opts
options → opts

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.

Also aliased as: short_desc=
Alias for: short_desc

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.

Also aliased as: takes_commands=
Alias for: takes_commands

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...]

See: usage_options, usage_arguments, usage_commands


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:


See: usage

Protected Instance Methods

cond_format_help_section(title, *lines, condition: true, indent: true, preformatted: false)

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:

    help - Provide help for individual commands



The formatted help section is only returned if the condition is true.


Whether the lines should be indented with CommandParser#help_indent spaces.


Assume that the given lines are already correctly formatted and don't try to reformat them.

format(content, width: command_parser.help_line_width, indent: command_parser.help_indent, indent_first_line: false)

Returns the text in content formatted so that no line is longer than width characters.



The maximum width of a line. If not specified, the CommandParser#help_line_width value is used.


This option specifies the amount of spaces prepended to each line. If not specified, the CommandParser#help_indent value is used.


If this option is true, then the first line is also indented.