Class: CommandLine::Arguments::Definition

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

Constant Summary collapse

ENDLESS_PARAMETERS =
99_999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Definition

Returns a new instance of Definition.



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

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

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



91
92
93
# File 'lib/cli/command_line_arguments.rb', line 91

def commands
  @commands
end

#optionsObject (readonly)

Returns the value of attribute options.



91
92
93
# File 'lib/cli/command_line_arguments.rb', line 91

def options
  @options
end

#parametersObject

Returns the value of attribute parameters.



91
92
93
# File 'lib/cli/command_line_arguments.rb', line 91

def parameters
  @parameters
end

Instance Method Details

#[](option_name) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/cli/command_line_arguments.rb', line 100

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

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

Yields:

  • (command_definition)


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

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)


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

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

#minimum_parameters=(count_specifier) ⇒ Object



109
110
111
# File 'lib/cli/command_line_arguments.rb', line 109

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

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



117
118
119
120
# File 'lib/cli/command_line_arguments.rb', line 117

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

#switch(name, switch_alias = nil) ⇒ Object



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

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