Class: Fuelcell::Action::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Callable, Subcommands
Defined in:
lib/fuelcell/action/command.rb

Overview

Represents the action you want to perform. It also holds a list of option definitions used by the parser, along with meta data used by the help system and callable which is any object that implements call, this is your action.

Direct Known Subclasses

NotFound, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callable

#call, #callable, #callable?

Methods included from Subcommands

#[], #add, #exist?, #global_options, #search

Constructor Details

#initialize(name) ⇒ Command

Every command initializes with only its name set expecting the dsl to finish assigning the rest of its properties

Parameters:

  • name (String)


21
22
23
24
25
26
27
# File 'lib/fuelcell/action/command.rb', line 21

def initialize(name)
  @name  = name.to_s
  @usage = nil
  @desc  = nil
  @opts  = OptsManager.new
  @args  = ArgsManager.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/fuelcell/action/command.rb', line 8

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/fuelcell/action/command.rb', line 8

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/fuelcell/action/command.rb', line 8

def opts
  @opts
end

Instance Method Details

#add_global_options(cmd) ⇒ Fuelcell::Action::Command

Allows all the global option definitions from this command hierarchy to be add to the command given



66
67
68
69
70
71
72
# File 'lib/fuelcell/action/command.rb', line 66

def add_global_options(cmd)
  return cmd if self === cmd
  global_options(cmd).each do |_key, opt_definition|
    cmd.opt opt_definition
  end
  cmd
end

#command(key) { ... } ⇒ Object

Allows a command to add subcommands to itself

Parameters:

  • key (String)

Yields:

  • instance_eval into Fuelcell::Command

Returns:

  • Fuelcell::Command



55
56
57
58
59
# File 'lib/fuelcell/action/command.rb', line 55

def command(key, &block)
  cmd = Command.new(key)
  cmd.instance_eval(&block)
  add cmd
end

#desc(text = nil) ⇒ String

Both getting and setter for description

Parameters:

  • text (String) (defaults to: nil)

Returns:

  • (String)


45
46
47
48
# File 'lib/fuelcell/action/command.rb', line 45

def desc(text = nil)
  return @desc if text.nil?
  @desc = text
end

#usage(text = nil, desc_text = nil) ⇒ String

This will assign both usage text and description, but when called with no args it will get the usage

Parameters:

  • text (String) (defaults to: nil)
  • desc_text (String) (defaults to: nil)

Returns:

  • (String)


35
36
37
38
39
# File 'lib/fuelcell/action/command.rb', line 35

def usage(text = nil, desc_text = nil)
  return @usage   if text.nil?
  desc(desc_text) unless desc_text.nil?
  @usage = text
end