Module: Rubikon::ArgumentVector
- Defined in:
- lib/rubikon/argument_vector.rb
Overview
This module will extend the argument array passed to the application, usually ARGV. It provides functionality to parse Rubikon specific tokens from the strings contained in the argument list passed to the application.
Instance Method Summary collapse
-
#command!(commands) ⇒ Command, Fixnum
Gets the command to use from the list of arguments passed to the application.
-
#expand! ⇒ Object
Turns arguments using a special syntax into arguments that are parseable.
-
#params!(params, pos = 0) ⇒ Array<Parameter>
Selects active parameters from a list of available parameters.
-
#scoped_args!(has_args, pos = 0) ⇒ Object
Gets all arguments passed to a specific scope, i.e.
Instance Method Details
#command!(commands) ⇒ Command, Fixnum
Gets the command to use from the list of arguments passed to the application. The first argument matching a command name or alias will cause the corresponding command to be selected.
The command and all arguments equal to ‘–’ will be removed from the array.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rubikon/argument_vector.rb', line 26 def command!(commands) command = nil command_index = 0 each_with_index do |arg, i| break if arg == '--' command = commands[arg.to_sym] unless command.nil? command_index = i delete_at i break end end delete '--' command ||= commands[:__default] return command, command_index end |
#expand! ⇒ Object
Turns arguments using a special syntax into arguments that are parseable.
Single character parameters may be joined together like ‘-dv’. This method will split them into separate parameters like ‘-d -v’.
Additionally a parameter argument may be attached to the parameter itself using ‘=’ like ‘–path=/tmp’. This method will also split them into ‘–path /tmp’.
54 55 56 57 58 59 60 61 62 |
# File 'lib/rubikon/argument_vector.rb', line 54 def each_with_index do |arg, i| next if !arg.start_with?('-') self[i] = arg.split('=', 2) next if arg.start_with?('--') self[i] = arg[1..-1].split('').uniq.map { |a| '-' + a } end flatten! end |
#params!(params, pos = 0) ⇒ Array<Parameter>
Selects active parameters from a list of available parameters
For every option found in the argument list #scoped_args! is called to find the arguments for that option.
All parameters found will be removed from the array.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubikon/argument_vector.rb', line 76 def params!(params, pos = 0) active_params = [] to_delete = [] each_with_index do |arg, i| next if i < pos || arg.nil? || !arg.start_with?('-') param = params[(arg.start_with?('--') ? arg[2..-1] : arg[1..1]).to_sym] unless param.nil? to_delete << i scoped_args! param, i + 1 if param.is_a? Option active_params << param end end to_delete.reverse.each { |i| delete_at i } active_params end |
#scoped_args!(has_args, pos = 0) ⇒ Object
Gets all arguments passed to a specific scope, i.e. a command or an option.
All arguments in the scope will be removed from the array.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rubikon/argument_vector.rb', line 103 def scoped_args!(has_args, pos = 0) to_delete = [] each_with_index do |arg, i| next if i < pos break if arg.start_with?('-') || !has_args.send(:more_args?) to_delete << i has_args.send(:<<, arg) end to_delete.reverse.each { |i| delete_at i } end |