Class: CommandLine::Arguments::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/command_line_arguments.rb

Constant Summary collapse

ENDLESS_PARAMETERS =
99999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Definition

Returns a new instance of Definition.



97
98
99
100
101
102
# File 'lib/cli/command_line_arguments.rb', line 97

def initialize(parent)
  @parent = parent
  @options  = {}
  @commands = {}
  @parameters = nil
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



95
96
97
# File 'lib/cli/command_line_arguments.rb', line 95

def commands
  @commands
end

#optionsObject (readonly)

Returns the value of attribute options.



95
96
97
# File 'lib/cli/command_line_arguments.rb', line 95

def options
  @options
end

#parametersObject

Returns the value of attribute parameters.



95
96
97
# File 'lib/cli/command_line_arguments.rb', line 95

def parameters
  @parameters
end

Instance Method Details

#[](option_name) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/cli/command_line_arguments.rb', line 104

def [](option_name)
  option_symbol = CommandLine::Option.rewrite(option_name)
  if the_option = @options.detect { |(name, odef)| odef =~ option_symbol }
    the_option[1]
  else
    raise CommandLine::UnknownOption, option_name
  end
end

#command(name) {|command_definition| ... } ⇒ Object

Yields:

  • (command_definition)


132
133
134
135
136
# File 'lib/cli/command_line_arguments.rb', line 132

def command(name, &block)
  command_definition = Definition.new(self)
  yield(command_definition) if block_given?
  @commands[CommandLine::Option.rewrite(name)] = command_definition
end

#has_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/cli/command_line_arguments.rb', line 138

def has_command?(command)
  @commands[CommandLine::Option.rewrite(command)]
end

#minimum_parameters=(count_specifier) ⇒ Object



113
114
115
# File 'lib/cli/command_line_arguments.rb', line 113

def minimum_parameters=(count_specifier)
  @parameters = count_specifier..ENDLESS_PARAMETERS
end

#option(name, options = {}) ⇒ Object



123
124
125
126
# File 'lib/cli/command_line_arguments.rb', line 123

def option(name, options = {})
  clo = CommandLine::Option.new(name, options)
  @options[clo.name] = clo
end

#switch(name, switch_alias = nil) ⇒ Object



128
129
130
# File 'lib/cli/command_line_arguments.rb', line 128

def switch(name, switch_alias = nil)
  option(name, :alias => switch_alias, :parameters => 0)
end