Class: Bake::Parser
- Inherits:
-
Object
- Object
- Bake::Parser
- Defined in:
- lib/common/options/parser.rb
Direct Known Subclasses
BakeFormatOptions, BakeRtextServiceOptions, BakecleanOptions, BakeqacOptions, BakeryOptions, Options
Instance Method Summary collapse
-
#add_option(params, block) ⇒ Object
def add_option(opt) @arguments = opt end.
- #get_block(argument) ⇒ Object
-
#initialize(argv) ⇒ Parser
constructor
A new instance of Parser.
- #num_parameter?(argument) ⇒ Boolean
- #parse_internal(ignore_invalid, subOptions = nil) ⇒ Object
- #valid?(argument) ⇒ Boolean
Constructor Details
#initialize(argv) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 |
# File 'lib/common/options/parser.rb', line 8 def initialize(argv) @arguments = {} @argv = argv end |
Instance Method Details
#add_option(params, block) ⇒ Object
def add_option(opt)
@arguments[opt.param] = opt
end
17 18 19 |
# File 'lib/common/options/parser.rb', line 17 def add_option(params, block) params.each { |p| @arguments[p] = block } end |
#get_block(argument) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/common/options/parser.rb', line 27 def get_block(argument) arg = nil block = nil @arguments.each do |a, b| if argument.start_with?(a) && a != "" return [b, nil] if a == argument if b && b.parameters.length==3 && argument[a.length..-1].scan(/\A\d*\z/).length > 0 block = b arg = argument[a.length..-1] end end end return [block, arg] end |
#num_parameter?(argument) ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/common/options/parser.rb', line 42 def num_parameter?(argument) b, inPlaceArg = get_block(argument) return 0 unless b return 0 if inPlaceArg return b.parameters.length end |
#parse_internal(ignore_invalid, subOptions = nil) ⇒ Object
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 |
# File 'lib/common/options/parser.rb', line 49 def parse_internal(ignore_invalid, subOptions = nil) pos = 0 begin while pos < @argv.length do arg = @argv[pos] if not valid?arg # used in bake config, must be passed from bakery to bake if subOptions and subOptions.valid?arg num = subOptions.num_parameter?(arg) if num > 0 if pos+1 < @argv.length and @argv[pos+1][0] != "-" pos = pos + 1 else if num != 2 # default raise "Argument for option #{arg} missing" end end end end index = arg.index('-') if index != nil and index == 0 raise "Option #{arg} unknown" if not ignore_invalid else @arguments[""].call(arg) # default parameter without "-" end else option, inPlaceArg = get_block(arg) hasArgument = (pos+1 < @argv.length and @argv[pos+1][0] != "-") if option.parameters.length == 3 if (hasArgument || inPlaceArg) if inPlaceArg option.call(inPlaceArg, nil, nil) else ignore = option.call(@argv[pos+1], nil, nil) # do not use inplace value pos = pos + 1 if ignore != :ignore end else option.call(nil, nil, nil) end elsif option.parameters.length == 2 if hasArgument option.call(@argv[pos+1], nil) # do not use default value pos = pos + 1 else option.call(nil, nil) # use default value end elsif option.parameters.length == 1 && hasArgument option.call(@argv[pos+1]) pos = pos + 1 elsif option.parameters.length == 0 option.call() else raise "Argument for option #{arg} missing" end end pos = pos + 1 end rescue SystemExit => e raise rescue Exception => e Bake.formatter.printError("Error: " + e.) ExitHelper.exit(1) end end |
#valid?(argument) ⇒ Boolean
21 22 23 24 25 |
# File 'lib/common/options/parser.rb', line 21 def valid?(argument) @arguments.any? { |a, b| argument == a || (a != "" && argument.start_with?(a) && (!b || (b.parameters.length==3 && argument[a.length..-1].scan(/\A\d*\z/).length > 0))) } end |