Skip to content

Defining Subcommands

mosop edited this page Jan 18, 2018 · 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 supercommand 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 make one of subcommands a default. The default subcommand can be run without an explicit name.

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