Class: CliSwitch
- Inherits:
-
Object
- Object
- CliSwitch
- Defined in:
- lib/cliswitch.rb
Overview
File : cliswitch.rb Authors : ccmywish <[email protected]> Created on : <2022-04-13> Last modified : <2022-04-14>
cliswitch:
Parse switch
Defined Under Namespace
Classes: Option
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#args ⇒ Object
instance attribute.
-
#options ⇒ Object
Returns the value of attribute options.
-
#options_config ⇒ Object
Returns the value of attribute options_config.
Class Method Summary collapse
- .class_options_config ⇒ Object
-
.option ⇒ Object
MyClass < CliSwitch option …
Instance Method Summary collapse
-
#initialize ⇒ CliSwitch
constructor
A new instance of CliSwitch.
- #parse(arr) ⇒ Object
Constructor Details
#initialize ⇒ CliSwitch
Returns a new instance of CliSwitch.
62 63 64 |
# File 'lib/cliswitch.rb', line 62 def initialize @options_config = self.class. end |
Instance Attribute Details
#args ⇒ Object
instance attribute
57 58 59 |
# File 'lib/cliswitch.rb', line 57 def args @args end |
#options ⇒ Object
Returns the value of attribute options.
58 59 60 |
# File 'lib/cliswitch.rb', line 58 def @options end |
#options_config ⇒ Object
Returns the value of attribute options_config.
59 60 61 |
# File 'lib/cliswitch.rb', line 59 def @options_config end |
Class Method Details
.class_options_config ⇒ Object
155 156 157 |
# File 'lib/cliswitch.rb', line 155 def self. @class_options_config end |
Instance Method Details
#parse(arr) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cliswitch.rb', line 67 def parse(arr) @args = [] @options = [] new_arr = [] # # Clear hyphen switch's stick effect # arr.each_with_index do if _1 =~ /^-\w.*/ if _1.size > 2 new_arr << _1[0,2] << _1[2..] next end end new_arr << _1 end arr = new_arr # # Parse the result to @options # arr.each_with_index do # hyphen switch match if _1 =~ /^-\w$/ or _1 =~ /^--\w.+$/ if op = (_1) # Whether receive the next arg case op.arg_required when 'noarg' # do nothing when 'required' op.next_arg = arr[_2 + 1] arr.delete_at(_2+1) when 'optional' next_arg = arr[_2 + 1] if !next_arg.nil? if next_arg.start_with?('-') && (next_arg) # do nothing else op.next_arg = next_arg arr.delete_at(_2+1) end end when 'noarg' # do nothing end @options << op else puts "Error: Unknown option: #{_1}" end next end # join args @args << _1 end # Not validate by default, because it's not necessary # validate_mandatory_options() # Debug # puts "=> args:" # p @args # puts "=> options:" # p @options # return @args, @options end |