Class: Application
- Inherits:
-
Object
- Object
- Application
- Defined in:
- lib/appl.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Done, OptionError
Constant Summary collapse
- APPL_VERSION =
"1.15".freeze
- OPTIONS_ENV =
nil- STOPOPT =
"stop option processing"- UNKNOWN =
"Unknown option: `%s'."- UNPROCA =
"Warning: unprocessed arguments: %s"- ALIASES =
[].freeze
- W_OPTS =
10- W_ARGS =
16
Class Method Summary collapse
- .alias_option(orig, opt) ⇒ Object
- .all_names ⇒ Object
- .cmdline_arguments ⇒ Object
- .define_option(opt, *param) ⇒ Object (also: def_option)
- .delete_option(opt) ⇒ Object
- .each_option ⇒ Object
- .full_name ⇒ Object
- .help ⇒ Object
- .option_act(args, opt, rest) ⇒ Object
- .root ⇒ Object
- .run(args = nil) ⇒ Object
- .show_message(msg, extra = nil) ⇒ Object
- .show_options ⇒ Object
- .unalias_option(opt) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #execute ⇒ Object
- #help ⇒ Object
-
#initialize(name = nil, args = nil) ⇒ Application
constructor
A new instance of Application.
- #run ⇒ Object
- #version ⇒ Object
- #warn_unprocessed ⇒ Object
Constructor Details
#initialize(name = nil, args = nil) ⇒ Application
Returns a new instance of Application.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/appl.rb', line 24 def initialize name = nil, args = nil @name, @args = name, args self.class.each_option { |opt,desc,arg,dfl,act| begin send act, dfl if dfl rescue NoMethodError raise OptionError, "Option action `#{act}' is not defined." end } while @args.first =~ /\A-/ do opt = $' @args.shift if opt =~ /\A-/ then break if !$' or $'.empty? opt = $' if opt =~ /^=/ then opt = $` ; @args.unshift $' end act = self.class.option_act @args, opt, nil send *act else until not opt or opt.empty? do c = opt.slice! 0, 1 if opt =~ /^=/ then opt = nil ; @args.unshift $' end act = self.class.option_act @args, c, opt send *act end end end end |
Class Method Details
.alias_option(orig, opt) ⇒ Object
146 147 148 149 150 |
# File 'lib/appl.rb', line 146 def alias_option orig, opt unalias_option opt @aliases[ opt.to_s] = orig.to_s nil end |
.all_names ⇒ Object
222 223 224 |
# File 'lib/appl.rb', line 222 def all_names [ self::NAME, *self::ALIASES].join "|" end |
.cmdline_arguments ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/appl.rb', line 286 def cmdline_arguments r = [] oe = self::OPTIONS_ENV eo = ENV[ oe] if oe if eo then eo.scan /"((?:\\.|[^"])*")|[^" \t]+/ do r.push $1 ? (eval $1) : $& end end r.concat $* $*.clear r end |
.define_option(opt, *param) ⇒ Object Also known as: def_option
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/appl.rb', line 124 def define_option opt, *param delete_option opt act = param.shift desc = param.pop arg = param.shift if arg then if param.empty? then arg, dfl = arg.split /:/, 2 if dfl =~ /\A:/ then dfl = $'.to_sym end else dfl = param.shift end end d = param.map { |x| "#{x}#$/" }.join desc.insert 0, d [ opt.to_s] = [ desc, arg, dfl, act] nil end |
.delete_option(opt) ⇒ Object
152 153 154 155 156 |
# File 'lib/appl.rb', line 152 def delete_option opt @aliases.reject! { |k,v| v == opt } .delete opt nil end |
.each_option ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/appl.rb', line 185 def each_option .each { |opt,(desc,arg,dfl,act)| case dfl when Symbol then dfl = const_get dfl end yield opt, desc, arg, dfl, act } end |
.full_name ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/appl.rb', line 226 def full_name n = [] s = self loop do l = s.all_names n.unshift l break if s == root s = s.root end n.join ' ' end |
.help ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/appl.rb', line 238 def help fn = full_name puts "#{fn} -- #{self::SUMMARY}" puts d = self::DESCRIPTION.gsub /#(\w+?)?#/ do case $1 when "NAME" then fn end end puts d puts if block_given? then puts yield end end |
.option_act(args, opt, rest) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/appl.rb', line 194 def option_act args, opt, rest dada = find_option_act opt dada or raise OptionError, root::UNKNOWN % opt desc, arg, dfl, act = *dada r = [ act] if arg then p = rest.slice! 0, rest.length if rest and not rest.empty? r.push p||args.shift end r end |
.root ⇒ Object
218 219 220 |
# File 'lib/appl.rb', line 218 def root self end |
.run(args = nil) ⇒ Object
89 90 91 92 |
# File 'lib/appl.rb', line 89 def run args = nil e = execute args exit e end |
.show_message(msg, extra = nil) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/appl.rb', line 273 def msg, extra = nil if $stderr.tty? then msg = "\e[31;1m#{msg}\e[m" if extra then extra = "\e[31m#{extra}\e[m" end end if extra then msg = [ msg, extra].join " " end $stderr.puts msg end |
.show_options ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/appl.rb', line 206 def do |opt,arg,dfl,desc| opt = opt.length == 1 ? "-#{opt}" : "--#{opt}" arg &&= "#{arg}" dfl &&= "[#{dfl}]" arg << dfl if arg && dfl l = " %-*s %-*s %s" % [ root::W_OPTS, opt, root::W_ARGS, arg, desc] l.rstrip! puts l end end |
.unalias_option(opt) ⇒ Object
158 159 160 161 |
# File 'lib/appl.rb', line 158 def unalias_option opt @aliases.delete opt nil end |
.version ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/appl.rb', line 256 def version puts "#{self::NAME} #{self::VERSION} -- #{self::SUMMARY}" puts self::COPYRIGHT if const_defined? :COPYRIGHT puts "License: #{self::LICENSE}" if const_defined? :LICENSE a = [] a.push self::AUTHOR if const_defined? :AUTHOR a.concat self::AUTHORS if const_defined? :AUTHORS if a.any? then a.flatten! a.uniq! j = a.join ", " h = a.length == 1 ? "Author" : "Authors" puts "#{h}: #{j}" end puts "Ruby version: #{RUBY_VERSION}" end |
Instance Method Details
#execute ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/appl.rb', line 72 def execute r = run r = 0 unless Integer === r warn_unprocessed r rescue SignalException raise if @debug self.class. $!.inspect 128 + $!.signo rescue raise if @debug self.class. "Error: #$!", "(#{$!.class})" $!.to_i rescue 1 end |
#help ⇒ Object
53 54 55 56 |
# File 'lib/appl.rb', line 53 def help self.class.help raise Done end |
#run ⇒ Object
63 64 |
# File 'lib/appl.rb', line 63 def run end |
#version ⇒ Object
58 59 60 61 |
# File 'lib/appl.rb', line 58 def version self.class.version raise Done end |
#warn_unprocessed ⇒ Object
66 67 68 69 70 |
# File 'lib/appl.rb', line 66 def warn_unprocessed if @args.any? then $stderr.puts self.class.root::UNPROCA % (@args.join " ") end end |