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

Returns a new instance of ARGV.

Parameters:

  • argv (Array<#to_s>)

    A list of parameters.



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

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

Class Method Details

.coerce(argv) ⇒ ARGV

Returns Coerces an object to the ARGV class if needed.

Parameters:

  • argv (Object)

    The object which should be converted to the ARGV class.

Returns:

  • (ARGV)

    Coerces an object to the ARGV class if needed.



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               # => []

Parameters:

  • name (String)

    The name of the option to look for among the remaining parameters.

Returns:

  • (Array<String>)

    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 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']

Returns:

  • (Array<String>)

    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>

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  # => []

Returns:

  • (Array<String>)

    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.

Returns:

  • (Boolean)

    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?

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', '--sweetener=honey'])
argv.flag?('milk')       # => false
argv.flag?('milk')       # => nil
argv.flag?('milk', true) # => true
argv.remainder           # => ['tea', '--sweetener=honey']

Parameters:

  • name (String)

    The name of the flag to look for among the remaining parameters.

  • default (Boolean) (defaults to: nil)

    The value that is returned in case the flag is not among the remaining parameters.

Returns:

  • (Boolean, nil)

    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?

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', '--sweetener=honey'])
argv.option('sweetener')          # => 'honey'
argv.option('sweetener')          # => nil
argv.option('sweetener', 'sugar') # => 'sugar'
argv.remainder                   # => ['tea', '--no-milk']

Parameters:

  • name (String)

    The name of the option to look for among the remaining parameters.

  • default (String) (defaults to: nil)

    The value that is returned in case the option is not among the remaining parameters.

Returns:

  • (String, nil)

    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

#optionsHash

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

Examples:


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

Returns:

  • (Hash)

    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 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', '--sweetener=honey'])
argv.shift_argument # => 'tea'
argv.remainder      # => ['--no-milk', '--sweetener=honey']

Returns:

  • (Array<String>)

    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.

Examples:


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

Returns:

  • (Array<String>)

    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_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']

Returns:

  • (String)

    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