Module: SOPT

Defined in:
lib/rbbt/util/simpleopt.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.commandObject

Returns the value of attribute command.



3
4
5
# File 'lib/rbbt/util/simpleopt.rb', line 3

def command
  @command
end

.descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/rbbt/util/simpleopt.rb', line 3

def description
  @description
end

.input_defaultsObject

Returns the value of attribute input_defaults.



4
5
6
# File 'lib/rbbt/util/simpleopt.rb', line 4

def input_defaults
  @input_defaults
end

.input_descriptionsObject

Returns the value of attribute input_descriptions.



4
5
6
# File 'lib/rbbt/util/simpleopt.rb', line 4

def input_descriptions
  @input_descriptions
end

.input_shortcutsObject

Returns the value of attribute input_shortcuts.



4
5
6
# File 'lib/rbbt/util/simpleopt.rb', line 4

def input_shortcuts
  @input_shortcuts
end

.input_typesObject

Returns the value of attribute input_types.



4
5
6
# File 'lib/rbbt/util/simpleopt.rb', line 4

def input_types
  @input_types
end

.inputsObject

Returns the value of attribute inputs.



4
5
6
# File 'lib/rbbt/util/simpleopt.rb', line 4

def inputs
  @inputs
end

.summaryObject

Returns the value of attribute summary.



3
4
5
# File 'lib/rbbt/util/simpleopt.rb', line 3

def summary
  @summary
end

.synopsysObject

Returns the value of attribute synopsys.



3
4
5
# File 'lib/rbbt/util/simpleopt.rb', line 3

def synopsys
  @synopsys
end

Class Method Details

.allObject



32
33
34
# File 'lib/rbbt/util/simpleopt.rb', line 32

def all
  @all ||= {}
end

.docObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rbbt/util/simpleopt.rb', line 123

def self.doc
  doc = <<-EOF
#{command}(1) -- #{summary}
#{"=" * (command.length + summary.length + 7)}

## SYNOPSYS

#{synopsys}

## DESCRIPTION

#{description}

## OPTIONS

#{input_doc(inputs, input_types, input_descriptions, input_defaults, input_shortcuts)}
  EOF
end

.get(opts) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rbbt/util/simpleopt.rb', line 182

def self.get(opts)
  info = parse(opts)

  switches = {}
  info.each do |name, i|
    switches[i[:short]] = name if i[:short]
    switches[i[:long]] = name if i[:long]
  end

  options = Hash.new(false)
  rest = []

  index = 0
  while index < ARGV.length do
    orig_arg = ARGV[index]

    if orig_arg =~ /=/
      arg, value = orig_arg.match(/(.*?)=(.*)/).values_at 1, 2
    else
      arg = orig_arg
      value = nil
    end

    if switches.include? arg
      name = switches[arg]
      i = info[name]
      if i[:arg]
        if value.nil?
          value = ARGV[index + 1]
          index += 1
        end
        options[name.to_sym] = value
      else
        options[name.to_sym] = value == "false" ? false : true
      end
    else
      rest << orig_arg
    end
    index += 1
  end

  ARGV.delete_if do true end
  rest.each do |e| ARGV << e end

  options
end

.input_doc(inputs, input_types = nil, input_descriptions = nil, input_defaults = nil, input_shortcuts = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rbbt/util/simpleopt.rb', line 105

def self.input_doc(inputs, input_types = nil, input_descriptions = nil, input_defaults = nil, input_shortcuts = nil)
  type = description = default = nil
  shortcut = ""
  inputs.collect do |name|

    type = input_types[name] unless input_types.nil?
    description = input_descriptions[name] unless input_descriptions.nil?
    default = input_defaults[name] unless input_defaults.nil?
    shortcut = input_shortcuts[name] unless input_shortcuts.nil?

    type = :string if type.nil?

    str  = "  * " << SOPT.input_format(name, type.to_sym, default, shortcut) << "\n"
    str << "    " << description << "\n" if description and not description.empty?
    str
  end * "\n"
end

.input_format(name, type = nil, default = nil, short = "") ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbbt/util/simpleopt.rb', line 87

def self.input_format(name, type = nil, default = nil, short = "")
  short = short_for(name) if not short.nil? and short.empty?

  input_str = short.nil? ? "--#{name}" : "-#{short}, --#{name}"
  input_str << case type
  when nil
    "#{default != nil ? " (default '#{default}')" : ""}:"
  when :boolean
    "[=false]#{default != nil ? " (default '#{default}')" : ""}:"
  when :tsv, :text
    "=<filename.#{type}|->#{default != nil ? " (default '#{default}')" : ""}; Use '-' for STDIN:"
  when :array
    "=<string[,string]*|filename.list|->#{default != nil ? " (default '#{default}')" : ""}; Use '-' for STDIN:"
  else
    "=<#{ type }>#{default != nil ? " (default '#{default}')" : ""}:"
  end
end

.name(info) ⇒ Object



142
143
144
# File 'lib/rbbt/util/simpleopt.rb', line 142

def self.name(info)
  (info[:long] || info[:short]).sub(/^-*/,'')
end

.parse(opts) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbbt/util/simpleopt.rb', line 146

def self.parse(opts)
  info = {}

  opts.split(/:/).each do |opt|
    next if opt.strip.empty?

    short, long = opt.strip.sub(/(^[^\s]*)\*/,'\1').split('--').values_at(0,1)
    long, short = short, nil if long.nil?

    if long.index(" ")
      long, description = long.match(/^([^\s]+)\s+(.*)/).values_at 1, 2
    else
      description = nil
    end
    
    i= { :arg => !!opt.match(/^[^\s]*\*/) }

    i[:short]       = short unless short.nil? || short.empty?
    i[:long]        = '--' + long unless long.nil? || long.empty?
    i[:description] = description unless description.nil? || description.empty?

    if shortcuts.include? short
      i[:short] = short_for(i[:long])
      Log.debug{ "Short for #{ long } is taken. Changed to #{i[:short]}" }
    else
      shortcuts << i[:short] if short
    end

    record(i)

    info[name(i)] = i
  end

  info
end

.record(info) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rbbt/util/simpleopt.rb', line 62

def record(info)
  input = info[:long].sub("--", '')
  inputs << input
  input_types[input] = info[:arg] ? :string : :boolean
  input_descriptions[input] = info[:description]
  input_defaults[input] = info[:default]
  input_shortcuts[input] = info[:short]? info[:short].sub("-",'') : nil
end

.resetObject



57
58
59
60
# File 'lib/rbbt/util/simpleopt.rb', line 57

def reset
  @shortcuts = []
  @all = {}
end

.short_for(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rbbt/util/simpleopt.rb', line 73

def self.short_for(name)
  short = []
  chars = name.to_s.chars.to_a

  short << chars.shift 
  shortcuts = input_shortcuts.values.compact.flatten
  while shortcuts.include? short * "" and chars.any?
    short << chars.shift 
  end
  return nil if chars.empty?

  short * ""
end

.shortcutsObject



28
29
30
# File 'lib/rbbt/util/simpleopt.rb', line 28

def shortcuts
  @shortcuts ||= []
end