Skip to content

Defining Subcommands

mosop edited this page Dec 1, 2016 · 4 revisions

A subcommand is a child command that is categorized under a specific namespace. For example, the git command has its several subcommands, clone, commit, push, etc.

To define subcommands, define a supercommand class that inherits Cli::Supercommand first. Then define subcommand classes into the super command class.

class Git < Cli::Supercommand
  class Clone < Cli::Command
   # ...
  end

  class Commit < Cli::Command
    # ...
  end

  class Push < Cli::Command
    # ...
  end
end

Default Subcommand

You can mark one of subcommands as default. The default subcommand can be run without an explicit name in a command line.

class Bundle < Cli::Supercommand
  command "install", default: true

  class Install < Cli::Command
    # ...
  end

  class Update < Cli::Command
    # ...
  end
end

Bundle.run %w(install)  # explicitly runs install
Bundle.run %w()         # implicitly runs install

Clone this wiki locally