2003-12-01 03:14:38 -05:00
|
|
|
#!/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..."
|
|
|
|
|
2009-05-27 14:48:49 -04:00
|
|
|
# separator
|
2003-12-01 03:14:38 -05:00
|
|
|
opts.on_tail
|
|
|
|
opts.on_tail("common options:")
|
|
|
|
|
|
|
|
# no argument, shows at tail
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on_tail("--help", "show this message") do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# mandatory argument
|
|
|
|
opts.on("-r", "--require=LIBRARY", String,
|
2013-07-21 13:27:40 -04:00
|
|
|
"require the LIBRARY, before",
|
|
|
|
"executing your script") do
|
|
|
|
|lib|
|
|
|
|
@library = lib
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# optional argument
|
|
|
|
opts.on("-i", "--inplace=[EXTENSION]",
|
2013-07-21 13:27:40 -04:00
|
|
|
"edit ARGV files in place", # multiline description
|
|
|
|
"(make backup if EXTENSION supplied)") do
|
|
|
|
|inplace|
|
|
|
|
@inplace = inplace || ''
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-N=[NUM]", Integer) do
|
|
|
|
|num|
|
|
|
|
@number = num
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# additional class
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-t", "--[no-]time[=TIME]", Time, "it's the time") do
|
|
|
|
|time|
|
|
|
|
@time = time
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# limit argument syntax
|
|
|
|
opts.on("-[0-7]", "-F", "--irs=[OCTAL]", OptionParser::OctalInteger,
|
2013-07-21 13:27:40 -04:00
|
|
|
"specify record separator", "(\\0, if no argument)") do
|
|
|
|
|irs|
|
|
|
|
@irs = irs
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# boolean switch(default true)
|
|
|
|
@exec = true
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") do
|
|
|
|
|exec|
|
|
|
|
@exec = exec
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# array
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-a", "--list[=LIST,LIST]", Array, "list") do
|
|
|
|
|list|
|
|
|
|
@list = list
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# fixed size array
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("--pair[=car,cdr]", Array, "pair") do
|
|
|
|
|x, y|
|
|
|
|
@x = x
|
|
|
|
@y = y
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# keyword completion
|
|
|
|
opts.on("--code=CODE", CODES, CODE_ALIASES, "select coding system",
|
2013-07-21 13:27:40 -04:00
|
|
|
"("+CODES.join(",")+",", " "+CODE_ALIASES.keys.join(",")+")") do
|
|
|
|
|c|
|
|
|
|
@code = c
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# optional argument with keyword completion
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("--type[=TYPE]", [:text, :binary], "select type(text, binary)") do
|
|
|
|
|t|
|
|
|
|
@type = t
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# boolean switch with optional argument(default false)
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-v", "--[no-]verbose=[FLAG]", "run verbosely") do
|
|
|
|
|v|
|
|
|
|
@verbose = v
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# easy way, set local variable
|
2013-07-21 13:27:40 -04:00
|
|
|
opts.on("-q", "--quit", "quit when ARGV is empty") do
|
|
|
|
|q|
|
|
|
|
@quit = q
|
|
|
|
end
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# adding on the fly
|
|
|
|
opts.on("--add=SWITCH=[ARG]", "add option on the fly", /\A(\w+)(?:=.+)?\Z/) do
|
|
|
|
|opt, var|
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("--#{opt}", "added in runtime", &eval("proc {|x|@#{var}=x}"))
|
2003-12-01 03:14:38 -05:00
|
|
|
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
|
2004-03-20 11:45:41 -05:00
|
|
|
begin print ARGV.options; exit end if @quit
|
2003-12-01 03:14:38 -05:00
|
|
|
ARGV.options = nil # no more parse
|
|
|
|
puts "ARGV = #{ARGV.join(' ')}" if !ARGV.empty?
|
|
|
|
#opts.variable.each {|sym| puts "#{sym} = #{opts.send(sym).inspect}"}
|