Class: CliSwitch

Inherits:
Object
  • Object
show all
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 options

Defined Under Namespace

Classes: Option

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliSwitch

Returns a new instance of CliSwitch.



62
63
64
# File 'lib/cliswitch.rb', line 62

def initialize
  @options_config = self.class.class_options_config
end

Instance Attribute Details

#argsObject

instance attribute



57
58
59
# File 'lib/cliswitch.rb', line 57

def args
  @args
end

#optionsObject

Returns the value of attribute options.



58
59
60
# File 'lib/cliswitch.rb', line 58

def options
  @options
end

#options_configObject

Returns the value of attribute options_config.



59
60
61
# File 'lib/cliswitch.rb', line 59

def options_config
  @options_config
end

Class Method Details

.class_options_configObject



155
156
157
# File 'lib/cliswitch.rb', line 155

def self.class_options_config
  @class_options_config
end

.optionObject

MyClass < CliSwitch

  option ...
end


149
150
151
152
153
# File 'lib/cliswitch.rb', line 149

def self.option(...)
  # A class instance variable for every subclass
  @class_options_config ||= []
  @class_options_config << Option.new(...)
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 = search_in_options_config(_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?('-') && search_in_options_config(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