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
|
|
|
|
opts.on_tail("--help", "show this message") {puts opts; exit}
|
|
|
|
|
|
|
|
# mandatory argument
|
|
|
|
opts.on("-r", "--require=LIBRARY", String,
|
|
|
|
"require the LIBRARY, before",
|
2006-10-03 11:59:45 -04:00
|
|
|
"executing your script") {|lib|@library=lib}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# optional argument
|
|
|
|
opts.on("-i", "--inplace=[EXTENSION]",
|
|
|
|
"edit ARGV files in place", # multiline description
|
2006-10-03 11:59:45 -04:00
|
|
|
"(make backup if EXTENSION supplied)") {|inplace| @inplace = inplace || ''}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-N=[NUM]", Integer) {|num|@number=num}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# additional class
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-t", "--[no-]time[=TIME]", Time, "it's the time") {|time|@time=time}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# limit argument syntax
|
|
|
|
opts.on("-[0-7]", "-F", "--irs=[OCTAL]", OptionParser::OctalInteger,
|
2006-10-03 11:59:45 -04:00
|
|
|
"specify record separator", "(\\0, if no argument)") {|irs|@irs=irs}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# boolean switch(default true)
|
|
|
|
@exec = true
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-n", "--no-exec[=FLAG]", TrueClass, "not really execute") {|exec|@exec=exec}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# array
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-a", "--list[=LIST,LIST]", Array, "list") {|list|@list=list}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# fixed size array
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("--pair[=car,cdr]", Array, "pair") {|x,y|@x=x; @y=y}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# keyword completion
|
|
|
|
opts.on("--code=CODE", CODES, CODE_ALIASES, "select coding system",
|
2006-10-03 11:59:45 -04:00
|
|
|
"("+CODES.join(",")+",", " "+CODE_ALIASES.keys.join(",")+")") {|c|@code=c}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# optional argument with keyword completion
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("--type[=TYPE]", [:text, :binary], "select type(text, binary)") {|t|@type=t}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# boolean switch with optional argument(default false)
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-v", "--[no-]verbose=[FLAG]", "run verbosely") {|v|@verbose=v}
|
2003-12-01 03:14:38 -05:00
|
|
|
|
|
|
|
# easy way, set local variable
|
2006-10-03 11:59:45 -04:00
|
|
|
opts.on("-q", "--quit", "quit when ARGV is empty") {|q|@quit=q}
|
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}"}
|