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.18".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
- .is_cmd? ⇒ Boolean
- .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 52 53 54 55 56 57 |
# 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 if dfl then if arg then send act, dfl else send act end end 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
160 161 162 163 164 |
# File 'lib/appl.rb', line 160 def alias_option orig, opt unalias_option opt @aliases[ opt.to_s] = orig.to_s nil end |
.all_names ⇒ Object
235 236 237 |
# File 'lib/appl.rb', line 235 def all_names [ self::NAME, *self::ALIASES].join "|" end |
.cmdline_arguments ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/appl.rb', line 299 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/appl.rb', line 136 def define_option opt, *param delete_option opt act = param.shift desc = param.pop arg = param.shift if arg then if arg == true then arg, dfl = nil, true elsif 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 @options[ opt.to_s] = [ desc, arg, dfl, act] nil end |
.delete_option(opt) ⇒ Object
166 167 168 169 170 |
# File 'lib/appl.rb', line 166 def delete_option opt @aliases.reject! { |k,v| v == opt } @options.delete opt nil end |
.each_option ⇒ Object
199 200 201 202 203 204 205 206 |
# File 'lib/appl.rb', line 199 def each_option @options.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
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/appl.rb', line 239 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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/appl.rb', line 251 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 |
.is_cmd? ⇒ Boolean
97 98 99 |
# File 'lib/appl.rb', line 97 def is_cmd? const_defined? :NAME end |
.option_act(args, opt, rest) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/appl.rb', line 208 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
231 232 233 |
# File 'lib/appl.rb', line 231 def root self end |
.run(args = nil) ⇒ Object
101 102 103 104 |
# File 'lib/appl.rb', line 101 def run args = nil e = execute args exit e end |
.show_message(msg, extra = nil) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/appl.rb', line 286 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
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/appl.rb', line 220 def do |opt,arg,dfl,desc| opt = opt.length == 1 ? "-#{opt}" : "--#{opt}" arg = "#{arg}" arg << "[#{dfl}]" if dfl l = " %-*s %-*s %s" % [ root::W_OPTS, opt, root::W_ARGS, arg, desc] l.rstrip! puts l end end |
.unalias_option(opt) ⇒ Object
172 173 174 175 |
# File 'lib/appl.rb', line 172 def unalias_option opt @aliases.delete opt nil end |
.version ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/appl.rb', line 269 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/appl.rb', line 78 def execute r = run warn_unprocessed case r when Integer then r & 0xff else 0 end rescue SignalException raise if @debug self.class. $!.inspect 128 + $!.signo rescue raise if @debug self.class. "Error: #$!", "(#{$!.class})" $!.to_i rescue 1 end |
#run ⇒ Object
69 70 |
# File 'lib/appl.rb', line 69 def run end |
#version ⇒ Object
64 65 66 67 |
# File 'lib/appl.rb', line 64 def version self.class.version raise Done end |
#warn_unprocessed ⇒ Object
72 73 74 75 76 |
# File 'lib/appl.rb', line 72 def warn_unprocessed if @args.any? then $stderr.puts self.class.root::UNPROCA % (@args.join " ") end end |