Class: Acclaim::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/acclaim/command.rb,
lib/acclaim/command/help.rb,
lib/acclaim/command/parser.rb,
lib/acclaim/command/version.rb,
lib/acclaim/command/help/template.rb

Overview

A command is a single word whose meaning the program understands. It calls upon a function of the program, which may be fine-tuned with options and given arguments.

app --global-option do --option

A subcommand benefits from its parent’s option processing.

app --global-option do something --option-for-do --option-for-something

A command can be created in the following form:

class App::Command < Acclaim::Command
  option :verbose, '-v', '--verbose', 'Run verbosely'
end

A subcommand can be created by inheriting from another command:

class App::Command::Do < App::Command
  opt :what, '-W', '--what', 'What to do', arity: [1, 0], required: true
  when_called do |options, arguments|
    puts "Verbose? #{options.verbose? ? :yes : :no}"
    puts "Doing #{options.what} with #{arguments.join ' and ')}!"
  end
end

Then, in your application’s binary, you may simply write:

App::Command.run *ARGV

See it in action:

$ app --verbose do --what testing acclaim safeguard
Verbose? yes
Doing testing with acclaim and safeguard!

Defined Under Namespace

Modules: ClassMethods, Help, Version Classes: Parser

Class Method Summary collapse

Class Method Details

.inherited(sub) ⇒ Object

Add the class methods to the subclass and add it to this command’s list of subcommands.



207
208
209
210
# File 'lib/acclaim/command.rb', line 207

def self.inherited(sub)
  sub.extend ClassMethods
  subcommands << sub if respond_to? :subcommands
end