Class: CmdlineParser
- Inherits:
-
Object
- Object
- CmdlineParser
- Defined in:
- lib/cmdline_parser.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#choices ⇒ Object
Returns the value of attribute choices.
-
#missing_mandatory ⇒ Object
Returns the value of attribute missing_mandatory.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #choice(name) {|opt| ... } ⇒ Object
-
#initialize(opt = {}) ⇒ CmdlineParser
constructor
A new instance of CmdlineParser.
- #parse_array(argv) ⇒ Object
- #show_help ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ CmdlineParser
Returns a new instance of CmdlineParser.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cmdline_parser.rb', line 8 def initialize(opt = {}) @choices = [] @short_map = {} @long_map = {} @params = [] @missing_mandatory = {} @search = {} @error_as_message = opt[:error_as_message] @error_as_message ||= false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(arg) ⇒ Object (private)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cmdline_parser.rb', line 158 def method_missing(arg) if @search[arg] != nil return @search[arg].to_s end if arg.to_s()[arg.to_s.length-4..-1] == "_obj" key = arg.to_s()[0...-4] if @search[key.to_sym] != nil return @search[key.to_sym] end end old_method_missing(arg) #puts "method missing! #{arg}" end |
Instance Attribute Details
#choices ⇒ Object
Returns the value of attribute choices.
4 5 6 |
# File 'lib/cmdline_parser.rb', line 4 def choices @choices end |
#missing_mandatory ⇒ Object
Returns the value of attribute missing_mandatory.
4 5 6 |
# File 'lib/cmdline_parser.rb', line 4 def missing_mandatory @missing_mandatory end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/cmdline_parser.rb', line 4 def params @params end |
Instance Method Details
#choice(name) {|opt| ... } ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/cmdline_parser.rb', line 19 def choice(name) opt = Opt.new opt.name = name yield opt @choices << opt @search[opt.name] = opt end |
#parse_array(argv) ⇒ Object
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 |
# File 'lib/cmdline_parser.rb', line 27 def parse_array(argv) prepare_index ignore_value = [] 0.upto(argv.length-1) do |i| token = argv[i].strip if @short_map.keys.include? token opt = @short_map[token] opt.activated = true if opt.has_value == true tmpVal = argv[i+1] if is_command(tmpVal) if @error_as_message STDERR.puts "Argument needed for flag #{token}" else raise MissingArgumentException, "Argument needed for flag #{token}" end else if tmpVal != nil opt.value = tmpVal ignore_value << tmpVal else if @error_as_message STDERR.puts "Argument needed for flag #{token}" else raise MissingArgumentException, "Argument needed for flag #{token}" end end end end if @missing_mandatory.keys.include? opt.name @missing_mandatory.delete(opt.name) end elsif @long_map.keys.include? token opt = @long_map[token] opt.activated = true if opt.has_value == true tmpVal = argv[i+1] if is_command(tmpVal) if @error_as_message STDERR.puts "Argument needed for flag #{token}" else raise MissingArgumentException, "Argument needed for flag #{token}" end else if tmpVal != nil opt.value = tmpVal ignore_value << tmpVal else if @error_as_message STDERR.puts "Argument needed for flag #{token}" else raise MissingArgumentException, "Argument needed for flag #{token}" end end end end if @missing_mandatory.keys.include? opt.name @missing_mandatory.delete(opt.name) end else if not ignore_value.include? token @params << token end end end if @missing_mandatory.length > 0 msg = [] @missing_mandatory.values.each do |ch| msg << "#{ch.short} or #{ch.long}" end if @error_as_message STDERR.puts "Error : Flag #{msg.join(",")} is required for this command" else raise MissingArgumentException,"Flag #{msg.join(",")} is required for this command" end end end |
#show_help ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cmdline_parser.rb', line 113 def show_help STDOUT.puts "" STDOUT.puts "Ruby Cmdline Parser version #{CmdlineParser::VERSION}" STDOUT.puts "" STDOUT.puts " Available commands:" # STDOUT.puts "" @choices.each do |c| STDOUT.print "%5s or %-12s" % [c.short, c.long] if c.has_value STDOUT.print "<param> " else STDOUT.print " " end if c.mandatory STDOUT.print "[Required] " else STDOUT.print " " end STDOUT.puts c.desc end STDOUT.puts "" end |