Class: CLAide::ARGV
- Inherits:
-
Object
- Object
- CLAide::ARGV
- 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
-
.coerce(argv) ⇒ ARGV
Coerces an object to the ARGV class if needed.
Instance Method Summary collapse
-
#all_options(name) ⇒ Array<String>
Returns an array of all the values of the option with the specified
name
among the remaining parameters. -
#arguments ⇒ Array<String>
A list of the remaining arguments.
-
#arguments! ⇒ Array<String>
A list of the remaining arguments.
-
#empty? ⇒ Boolean
Whether or not there are any remaining unhandled parameters.
-
#flag?(name, default = nil) ⇒ Boolean?
Returns
true
if the flag by the specifiedname
is among the remaining parameters and is not negated. -
#initialize(argv) ⇒ ARGV
constructor
A new instance of ARGV.
-
#option(name, default = nil) ⇒ String?
Returns the value of the option by the specified
name
is among the remaining parameters. -
#options ⇒ Hash
A hash that consists of the remaining flags and options and their values.
-
#remainder ⇒ Array<String>
A list of the remaining unhandled parameters, in the same format a user specifies it in.
-
#remainder! ⇒ Array<String>
A list of the remaining unhandled parameters, in the same format the user specified them.
-
#shift_argument ⇒ String
The first argument in the remaining parameters.
Constructor Details
Class Method Details
Instance Method Details
#all_options(name) ⇒ Array<String>
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.
204 205 206 207 208 209 210 |
# File 'lib/claide/argv.rb', line 204 def (name) = [] while entry = option(name) << entry end end |
#arguments ⇒ Array<String>
Returns A list of the remaining arguments.
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>
This version also removes the arguments from the remaining parameters.
Returns A list of the remaining 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
Returns Whether or not there are any remaining unhandled parameters.
32 33 34 |
# File 'lib/claide/argv.rb', line 32 def empty? @entries.empty? end |
#flag?(name, default = nil) ⇒ Boolean?
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.
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?
This will remove the option from the remaining parameters.
Returns the value of the option by the specified
name
is among the remaining parameters.
183 184 185 |
# File 'lib/claide/argv.rb', line 183 def option(name, default = nil) delete_entry(:option, name, default) end |
#options ⇒ Hash
Returns A hash that consists of the remaining flags and options and their values.
80 81 82 83 84 85 86 |
# File 'lib/claide/argv.rb', line 80 def = {} @entries.each do |type, (key, value)| [key] = value unless type == :arg end end |
#remainder ⇒ Array<String>
Returns A list of the remaining unhandled parameters, in the same format a user specifies it in.
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.
68 69 70 |
# File 'lib/claide/argv.rb', line 68 def remainder! remainder.tap { @entries.clear } end |
#shift_argument ⇒ String
This will remove the argument from the remaining parameters.
Returns The first argument in the remaining parameters.
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 |