Class: Nexposecli::ArgParse
- Inherits:
-
Object
- Object
- Nexposecli::ArgParse
- Defined in:
- lib/nexposecli/argparse.rb
Defined Under Namespace
Classes: Error, ExtraneousArgument, InvalidArgument, MissingArgument
Instance Attribute Summary collapse
-
#opt_table ⇒ Object
Returns the value of attribute opt_table.
Instance Method Summary collapse
-
#initialize(yaml_str) ⇒ ArgParse
constructor
A new instance of ArgParse.
- #parse ⇒ Object
- #usage(usage_str = nil) ⇒ Object
Constructor Details
#initialize(yaml_str) ⇒ ArgParse
Returns a new instance of ArgParse.
16 17 18 19 20 |
# File 'lib/nexposecli/argparse.rb', line 16 def initialize(yaml_str) @opt_def = YAML.parse(yaml_str).transform @opt_table = Hash.new end |
Instance Attribute Details
#opt_table ⇒ Object
Returns the value of attribute opt_table.
14 15 16 |
# File 'lib/nexposecli/argparse.rb', line 14 def opt_table @opt_table end |
Instance Method Details
#parse ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/nexposecli/argparse.rb', line 22 def parse short_to_name = {} @opt_def.each { |hash| next if hash['comment'] if hash['short'] sa = '-' + hash['short'] short_to_name[sa] = hash['name'] end opt_table[hash['name']] = hash['default'] if hash['default'] } acount = 0 arg_len = ARGV.length opt_name = nil opt_hash = nil while (acount <= ARGV.length) #puts "ARGV: " + ARGV.inspect #puts "acount: #{acount}" #puts "SW: #{ARGV[acount]}" str = ARGV[acount] #puts "1STR: #{str}" opt_arg = true case str when /^--([^=]+)$/ opt_name = $1.gsub(/-/, '_') opt_hash = @opt_def.select { |h| h['name'] == opt_name }.shift raise InvalidArgument, "No such opt: #{str}" unless opt_hash #p opt_hash ARGV.delete_at(acount) if opt_hash['required'] opt_arg = ARGV.delete_at(acount) #puts "ARG: '#{opt_arg}'" end when /^--([^=]+)=(.*)/ opt_name, opt_arg = $1, $2 opt_name.gsub!(/-/, '_') opt_hash = @opt_def.select { |h| h['name'] == opt_name }.shift raise InvalidArgument, "No such opt: #{str}" unless opt_hash raise ExtraneousArgument, "Option '--#{opt_name}' does not take an argument" unless opt_hash['required'] ARGV.delete_at(acount) when /^-(.*)/ #puts "*three" sopts = $1.split(//) slen = sopts.length ARGV.delete_at(acount) sopts.each_with_index { |short_opt, i| sopt = '-' + short_opt opt_name = short_to_name[sopt] raise InvalidArgument, "No such opt: #{sopt}" unless opt_name opt_hash = @opt_def.select { |h| h['name'] == opt_name }.shift #p @opt_def #p sopt, opt_name, opt_hash raise InvalidArgument, "No such opt: #{sopt}" unless opt_hash # p opt_hash raise MissingArgument, "#{sopt} requires argument" if opt_hash['required'] && i != slen - 1 if i == slen - 1 && opt_hash['required'] opt_arg = ARGV.delete_at(acount) raise MissingArgument, "#{sopt} requires argument" unless opt_arg end @opt_table[opt_name] = opt_arg #puts "NA: #{opt_name} #{opt_arg}" } next else #puts "STR: #{str}" #puts "ELSE: #{ARGV}" #puts acount += 1 next end if opt_arg == '-' # read from stdin opt_arg = STDIN.readlines.map(&:strip).map { |s| s != '' ? s : nil }.compact opt_hash['proc'] = nil end @opt_table[opt_name] = opt_arg #puts "acount end : #{acount}" #puts end #p @opt_table @opt_table.keys.each { |opt_name| opt_hash = @opt_def.select { |h| h['name'] == opt_name }.shift if prc = opt_hash['proc'] @opt_table[opt_name] = @opt_table[opt_name].instance_eval(prc) end } #p @opt_table return OpenStruct.new(@opt_table) end |
#usage(usage_str = nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/nexposecli/argparse.rb', line 141 def usage(usage_str = nil) if usage_str str = "Usage: #{usage_str}\n" else str = "" end @opt_def.each { |opt_hash| if opt_hash['comment'] str << "\n" + opt_hash['comment'] + "\n\n" else long_opt = '--' + opt_hash["name"].gsub(/_/, '-') short_opt = opt_hash['short'] ? '-' + opt_hash['short'] : " " desc = opt_hash['desc'] req_str = opt_hash['required'] ? " ARG" : " " def_str = opt_hash['default'] ? " (def: #{opt_hash['default']})" : " " str << sprintf(" %-40s (%s) : %s\n", "#{long_opt}#{req_str}", short_opt, desc + def_str) end } return str end |