Class: CLAide::ARGV

Inherits:
Object
  • Object
show all
Defined in:
lib/claide/argv.rb

Overview

This class is responsible for parsing the parameters specified by the user, accessing individual parameters, and keep state by removing handled parameters.

Defined Under Namespace

Modules: Parser

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ ARGV


25
26
27
# File 'lib/claide/argv.rb', line 25

def initialize(argv)
  @entries = Parser.parse(argv)
end

Class Method Details

.coerce(argv) ⇒ ARGV


14
15
16
17
18
19
20
# File 'lib/claide/argv.rb', line 14

def self.coerce(argv)
  if argv.is_a?(ARGV)
    argv
  else
    ARGV.new(argv)
  end
end

Instance Method Details

#all_options(name) ⇒ Array<String>

Note:

This will remove the option from the remaining parameters.

Returns an array of all the values of the option with the specified name among the remaining parameters.

Examples:


argv = CLAide::ARGV.new(['--ignore=foo', '--ignore=bar'])
argv.all_options('include')  # => []
argv.all_options('ignore')   # => ['bar', 'foo']
argv.remainder               # => []

204
205
206
207
208
209
210
# File 'lib/claide/argv.rb', line 204

def all_options(name)
  options = []
  while entry = option(name)
    options << entry
  end
  options
end

#argumentsArray<String>

Returns A list of the remaining arguments.

Examples:


argv = CLAide::ARGV.new(['tea', 'white', '--no-milk', 'biscuit'])
argv.shift_argument # => 'tea'
argv.arguments      # => ['white', 'biscuit']

96
97
98
# File 'lib/claide/argv.rb', line 96

def arguments
  @entries.map { |type, value| value if type == :arg }.compact
end

#arguments!Array<String>

Note:

This version also removes the arguments from the remaining parameters.

Returns A list of the remaining arguments.

Examples:


argv = CLAide::ARGV.new(['tea', 'white', '--no-milk', 'biscuit'])
argv.arguments  # => ['tea', 'white', 'biscuit']
argv.arguments! # => ['tea', 'white', 'biscuit']
argv.arguments  # => []

112
113
114
115
116
117
118
# File 'lib/claide/argv.rb', line 112

def arguments!
  arguments = []
  while arg = shift_argument
    arguments << arg
  end
  arguments
end

#empty?Boolean


32
33
34
# File 'lib/claide/argv.rb', line 32

def empty?
  @entries.empty?
end

#flag?(name, default = nil) ⇒ Boolean?

Note:

This will remove the flag from the remaining parameters.

Returns true if the flag by the specified name is among the remaining parameters and is not negated.

Examples:


argv = CLAide::ARGV.new(['tea', '--no-milk', '--sweetner=honey'])
argv.flag?('milk')       # => false
argv.flag?('milk')       # => nil
argv.flag?('milk', true) # => true
argv.remainder           # => ['tea', '--sweetner=honey']

158
159
160
# File 'lib/claide/argv.rb', line 158

def flag?(name, default = nil)
  delete_entry(:flag, name, default, true)
end

#option(name, default = nil) ⇒ String?

Note:

This will remove the option from the remaining parameters.

Returns the value of the option by the specified name is among the remaining parameters.

Examples:


argv = CLAide::ARGV.new(['tea', '--no-milk', '--sweetner=honey'])
argv.option('sweetner')          # => 'honey'
argv.option('sweetner')          # => nil
argv.option('sweetner', 'sugar') # => 'sugar'
argv.remainder                   # => ['tea', '--no-milk']

183
184
185
# File 'lib/claide/argv.rb', line 183

def option(name, default = nil)
  delete_entry(:option, name, default)
end

#optionsHash

Returns A hash that consists of the remaining flags and options and their values.

Examples:


argv = CLAide::ARGV.new(['tea', '--no-milk', '--sweetner=honey'])
argv.options # => { 'milk' => false, 'sweetner' => 'honey' }

80
81
82
83
84
85
86
# File 'lib/claide/argv.rb', line 80

def options
  options = {}
  @entries.each do |type, (key, value)|
    options[key] = value unless type == :arg
  end
  options
end

#remainderArray<String>

Returns A list of the remaining unhandled parameters, in the same format a user specifies it in.

Examples:


argv = CLAide::ARGV.new(['tea', '--no-milk', '--sweetner=honey'])
argv.shift_argument # => 'tea'
argv.remainder      # => ['--no-milk', '--sweetner=honey']

45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/claide/argv.rb', line 45

def remainder
  @entries.map do |type, (key, value)|
    case type
    when :arg
      key
    when :flag
      "--#{'no-' if value == false}#{key}"
    when :option
      "--#{key}=#{value}"
    end
  end
end

#remainder!Array<String>

Returns A list of the remaining unhandled parameters, in the same format the user specified them.

Examples:


argv = CLAide::ARGV.new(['tea', '--no-milk', '--sweetner=honey'])
argv.shift_argument # => 'tea'
argv.remainder!     # => ['--no-milk', '--sweetner=honey']
argv.remainder      # => []

68
69
70
# File 'lib/claide/argv.rb', line 68

def remainder!
  remainder.tap { @entries.clear }
end

#shift_argumentString

Note:

This will remove the argument from the remaining parameters.

Returns The first argument in the remaining parameters.

Examples:


argv = CLAide::ARGV.new(['tea', 'white'])
argv.shift_argument # => 'tea'
argv.arguments      # => ['white']

130
131
132
133
134
135
136
# File 'lib/claide/argv.rb', line 130

def shift_argument
  if index = @entries.find_index { |type, _| type == :arg }
    entry = @entries[index]
    @entries.delete_at(index)
    entry.last
  end
end