Module: Commander

Defined in:
lib/commander/configure.rb,
lib/commander/runner.rb,
lib/commander/command.rb,
lib/commander/methods.rb,
lib/commander/version.rb,
lib/commander/platform.rb,
lib/commander/delegates.rb,
lib/commander/help_formatters.rb,
lib/commander/user_interaction.rb,
lib/commander/help_formatters/base.rb,
lib/commander/help_formatters/terminal.rb,
lib/commander/help_formatters/terminal_compact.rb

Overview

Commander is the top-level namespace for the whole gem: see Commander::Methods for the DSL mixin, Commander::Runner for the engine that drives it, and Commander::Command for individual commands.

Defined Under Namespace

Modules: Delegates, HelpFormatter, Methods, Platform, UI Classes: Command, Runner

Constant Summary collapse

VERSION =
'6.0.0'

Class Method Summary collapse

Class Method Details

.configure(*configuration_opts) ⇒ Object

Runs a Commander program within an isolated, anonymous module rather than polluting the including object (as Commander::Methods normally does when included directly). Useful for embedding a Commander CLI inside a larger application without leaking its DSL methods. Calls Runner#run! once the block has finished configuring commands.

Any configuration_opts are passed through as block arguments.

Examples

Commander.configure do
program :name, 'my-tool'
program :version, '1.0.0'
program :description, 'Does something useful'

command :greet do |c|
  c.action { |args, _options| say "Hello, #{args.first}!" }
end
end

Commander.configure(default_name) do |name|
program :name, name
end


32
33
34
35
36
37
38
39
40
41
# File 'lib/commander/configure.rb', line 32

def configure(*configuration_opts, &)
  configuration_module = Module.new
  configuration_module.extend Commander::Methods

  configuration_module.class_exec(*configuration_opts, &)

  configuration_module.class_exec do
    run!
  end
end