Class: Acclaim::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/acclaim/command.rb,
lib/acclaim/command/dsl.rb,
lib/acclaim/command/help.rb,
lib/acclaim/command/parser.rb,
lib/acclaim/command/version.rb,
lib/acclaim/command/dsl/root.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!

Since:

  • 0.0.1

Defined Under Namespace

Modules: DSL, Help, Version Classes: Parser

Class Method Summary collapse

Class Method Details

.inherited(command) ⇒ Object

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

Parameters:

  • command (Class)

    the class that inherited from this command

Since:

  • 0.0.1



53
54
55
56
# File 'lib/acclaim/command.rb', line 53

def inherited(command)
  command.extend Command::DSL
  subcommands << command if respond_to? :subcommands
end