diff options
Diffstat (limited to 'ruby_1_8_6/sample/optparse')
-rw-r--r-- | ruby_1_8_6/sample/optparse/opttest.rb | 85 | ||||
-rwxr-xr-x | ruby_1_8_6/sample/optparse/subcommand.rb | 19 |
2 files changed, 0 insertions, 104 deletions
diff --git a/ruby_1_8_6/sample/optparse/opttest.rb b/ruby_1_8_6/sample/optparse/opttest.rb deleted file mode 100644 index 61b157bce5..0000000000 --- a/ruby_1_8_6/sample/optparse/opttest.rb +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/ruby -I. - -require 'optparse' -require 'optparse/time' -require 'pp' - -# keywords -CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary] -CODE_ALIASES = {"jis" => "iso-2022-jp", "sjis" => "shift_jis"} -POSSIBLE_CODES = "(#{(CODES+CODE_ALIASES.keys).join(',')})" - -ARGV.options do - |opts| - opts.banner << " argv..." - - # separater - opts.on_tail - opts.on_tail("common options:") - - # no argument, shows at tail - opts.on_tail("--help", "show this message") {puts opts; exit} - - # mandatory argument - opts.on("-r", "--require=LIBRARY", String, - "require the LIBRARY, before", - "executing your script") {|@library|} - - # optional argument - opts.on("-i", "--inplace=[EXTENSION]", - "edit ARGV files in place", # multiline description - "(make backup if EXTENSION supplied)") {|@inplace| @inplace ||= ''} - - opts.on("-N=[NUM]", Integer) {|@number|} - - # additional class - opts.on("-t", "--[no-]time[=TIME]", Time, "it's the time") {|@time|} - - # limit argument syntax - opts.on("-[0-7]", "-F", "--irs=[OCTAL]", OptionParser::OctalInteger, - "specify record separator", "(\\0, if no argument)") {|@irs|} - - # boolean switch(default true) - @exec = true - opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|@exec|} - - # array - opts.on("-a", "--list[=LIST,LIST]", Array, "list") {|@list|} - - # fixed size array - opts.on("--pair[=car,cdr]", Array, "pair") {|@x, @y|} - - # keyword completion - opts.on("--code=CODE", CODES, CODE_ALIASES, "select coding system", - "("+CODES.join(",")+",", " "+CODE_ALIASES.keys.join(",")+")") {|@code|} - - # optional argument with keyword completion - opts.on("--type[=TYPE]", [:text, :binary], "select type(text, binary)") {|@type|} - - # boolean switch with optional argument(default false) - opts.on("-v", "--[no-]verbose=[FLAG]", "run verbosely") {|@verbose|} - - # easy way, set local variable - opts.on("-q", "--quit", "quit when ARGV is empty") {|@quit|} - - # 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}|}")) - end - - opts.on_head("specific options:") - - # no argument - opts.on_tail("--version", "show version") do - puts OptionParser::Version.join('.') - exit - end - opts.parse! -end - -pp self -(print ARGV.options; exit) if @quit -ARGV.options = nil # no more parse -puts "ARGV = #{ARGV.join(' ')}" if !ARGV.empty? -#opts.variable.each {|sym| puts "#{sym} = #{opts.send(sym).inspect}"} diff --git a/ruby_1_8_6/sample/optparse/subcommand.rb b/ruby_1_8_6/sample/optparse/subcommand.rb deleted file mode 100755 index 21c42dd60a..0000000000 --- a/ruby_1_8_6/sample/optparse/subcommand.rb +++ /dev/null @@ -1,19 +0,0 @@ -#! /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? |