summaryrefslogtreecommitdiff
path: root/sample/optparse
diff options
context:
space:
mode:
Diffstat (limited to 'sample/optparse')
-rwxr-xr-x[-rw-r--r--]sample/optparse/opttest.rb74
-rwxr-xr-xsample/optparse/subcommand.rb19
2 files changed, 76 insertions, 17 deletions
diff --git a/sample/optparse/opttest.rb b/sample/optparse/opttest.rb
index 683c450d57..b2013c5253 100644..100755
--- a/sample/optparse/opttest.rb
+++ b/sample/optparse/opttest.rb
@@ -13,59 +13,99 @@ ARGV.options do
|opts|
opts.banner << " argv..."
- # separater
+ # separator
opts.on_tail
opts.on_tail("common options:")
# no argument, shows at tail
- opts.on_tail("--help", "show this message") {puts opts; exit}
+ opts.on_tail("--help", "show this message") do
+ puts opts
+ exit
+ end
# mandatory argument
opts.on("-r", "--require=LIBRARY", String,
- "require the LIBRARY, before",
- "executing your script") {|@library|}
+ "require the LIBRARY, before",
+ "executing your script") do
+ |lib|
+ @library = lib
+ end
# optional argument
opts.on("-i", "--inplace=[EXTENSION]",
- "edit ARGV files in place", # multiline description
- "(make backup if EXTENSION supplied)") {|@inplace| @inplace ||= ''}
+ "edit ARGV files in place", # multiline description
+ "(make backup if EXTENSION supplied)") do
+ |inplace|
+ @inplace = inplace || ''
+ end
- opts.on("-N=[NUM]", Integer) {|@number|}
+ opts.on("-N=[NUM]", Integer) do
+ |num|
+ @number = num
+ end
# additional class
- opts.on("-t", "--[no-]time[=TIME]", Time, "it's the time") {|@time|}
+ opts.on("-t", "--[no-]time[=TIME]", Time, "it's the time") do
+ |time|
+ @time = time
+ end
# limit argument syntax
opts.on("-[0-7]", "-F", "--irs=[OCTAL]", OptionParser::OctalInteger,
- "specify record separator", "(\\0, if no argument)") {|@irs|}
+ "specify record separator", "(\\0, if no argument)") do
+ |irs|
+ @irs = irs
+ end
# boolean switch(default true)
@exec = true
- opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|}
+ opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") do
+ |exec|
+ @exec = exec
+ end
# array
- opts.on("-a", "--list[=LIST,LIST]", Array, "list") {|@list|}
+ opts.on("-a", "--list[=LIST,LIST]", Array, "list") do
+ |list|
+ @list = list
+ end
# fixed size array
- opts.on("--pair[=car,cdr]", Array, "pair") {|@x, @y|}
+ opts.on("--pair[=car,cdr]", Array, "pair") do
+ |x, y|
+ @x = x
+ @y = y
+ end
# keyword completion
opts.on("--code=CODE", CODES, CODE_ALIASES, "select coding system",
- "("+CODES.join(",")+",", " "+CODE_ALIASES.keys.join(",")+")") {|@code|}
+ "("+CODES.join(",")+",", " "+CODE_ALIASES.keys.join(",")+")") do
+ |c|
+ @code = c
+ end
# optional argument with keyword completion
- opts.on("--type[=TYPE]", [:text, :binary], "select type(text, binary)") {|@type|}
+ opts.on("--type[=TYPE]", [:text, :binary], "select type(text, binary)") do
+ |t|
+ @type = t
+ end
# boolean switch with optional argument(default false)
- opts.on("-v", "--[no-]verbose=[FLAG]", "run verbosely") {|@verbose|}
+ opts.on("-v", "--[no-]verbose=[FLAG]", "run verbosely") do
+ |v|
+ @verbose = v
+ end
# easy way, set local variable
- opts.on("-q", "--quit", "quit when ARGV is empty") {|@quit|}
+ opts.on("-q", "--quit", "quit when ARGV is empty") do
+ |q|
+ @quit = q
+ end
# adding on the fly
opts.on("--add=SWITCH=[ARG]", "add option on the fly", /\A(\w+)(?:=.+)?\Z/) do
|opt, var|
- opts.on("--#{opt}", "added in runtime", &eval("proc {|@#{var}|}"))
+ opts.on("--#{opt}", "added in runtime", &eval("proc {|x|@#{var}=x}"))
end
opts.on_head("specific options:")
diff --git a/sample/optparse/subcommand.rb b/sample/optparse/subcommand.rb
new file mode 100755
index 0000000000..21c42dd60a
--- /dev/null
+++ b/sample/optparse/subcommand.rb
@@ -0,0 +1,19 @@
+#! /usr/bin/ruby
+# contributed by Minero Aoki.
+
+require 'optparse'
+
+parser = OptionParser.new
+parser.on('-i') { puts "-i" }
+parser.on('-o') { puts '-o' }
+
+subparsers = Hash.new {|h,k|
+ $stderr.puts "no such subcommand: #{k}"
+ exit 1
+}
+subparsers['add'] = OptionParser.new.on('-i') { puts "add -i" }
+subparsers['del'] = OptionParser.new.on('-i') { puts "del -i" }
+subparsers['list'] = OptionParser.new.on('-i') { puts "list -i" }
+
+parser.order!(ARGV)
+subparsers[ARGV.shift].parse!(ARGV) unless ARGV.empty?