| Class | CommandOptionParser |
| In: |
lib/kwalify/util/option-parser.rb
|
| Parent: | Object |
ex.
## create parser
arg_none = "hv" # ex. -h -v
arg_required = "xf" # ex. -x suffix -f filename
arg_optional = "i" # ex. -i (or -i10)
parser = CommandOptionParser.new(arg_none, arg_required, arg_optional)
## parse options
argv = %w[-h -v -f filename -i 10 aaa bbb]
options, properties = parser.parse(argv)
p options #=> { ?h=>true, ?v=>true, ?f=>"filename", ?i=>true }
p argv #=> ["10", "aaa", "bbb"]
## parse options #2
argv = %w[-hvx.txt -ffilename -i10 aaa bbb]
options, properties = parser.parse(argv)
p options #=> { ?h=>true, ?v=>true, ?x=>".txt", ?f=>"filename", ?i=>10 }
p argv #=> ["aaa", "bbb"]
## parse properties
argv = %w[-hi --index=10 --user-name=foo --help]
options, properties = parser.parse(argv)
p options #=> {?h=>true, ?i=>true}
p properties #=> {"index"=>"10", "user-name"=>"foo", "help"=>nil}
## parse properties with auto-convert
argv = %w[-hi --index=10 --user-name=foo --help]
options, properties = parser.parse(argv, true)
p options #=> {?h=>true, ?i=>true}
p properties #=> {:index=>10, :user_name=>foo, :help=>true}
## -a: unknown option.
argv = %w[-abc]
begin
options, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex.message # -a: unknown option.
end
## -f: argument required.
argv = %w[-f]
begin
options, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex.message # -f: argument required.
end
## --@prop=10: invalid property.
argv = %w[--@prop=10]
begin
options, properties = parser.parse(argv)
rescue CommandOptionError => ex
$stderr.puts ex.message # --@prop=10: invalid property.
end
arg_none: option string which takes no argument arg_required: option string which takes argument arg_otpional: option string which may takes argument optionally