Module: ALib::MainModule::InstanceMethods
Overview
–}}}
Constant Summary
Constants included from Constants
Constants::EXIT_FAILURE, Constants::EXIT_SUCCESS
Constants included from Logging
Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3
Constants included from ALib
BSearch, Configfile, ConfigurableMain, Find, Listfile, OrderedAutoHash, SimpleMain, VERSION
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#cmdline ⇒ Object
readonly
Returns the value of attribute cmdline.
-
#console ⇒ Object
(also: #console?)
readonly
Returns the value of attribute console.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#listoptions ⇒ Object
readonly
Returns the value of attribute listoptions.
-
#logdev ⇒ Object
readonly
Returns the value of attribute logdev.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#program ⇒ Object
readonly
Returns the value of attribute program.
-
#verbosity ⇒ Object
readonly
Returns the value of attribute verbosity.
Instance Method Summary collapse
-
#die(opts = {}) ⇒ Object
–}}}.
-
#init_logging(opts = @options) ⇒ Object
–}}}.
- #initialize(argv = ARGV, env = ENV) ⇒ Object
-
#klass ⇒ Object
–}}}.
- #logcatch ⇒ Object
-
#main ⇒ Object
–}}}.
-
#optional_arguments ⇒ Object
–}}}.
-
#parse_argv ⇒ Object
–}}}.
-
#parse_options ⇒ Object
–}}}.
- #post_main ⇒ Object
- #post_parse_argv ⇒ Object
- #post_parse_options ⇒ Object
- #post_run ⇒ Object
- #pre_main ⇒ Object
- #pre_parse_argv ⇒ Object
-
#pre_parse_options ⇒ Object
–}}}.
- #pre_run ⇒ Object
- #required_arguments ⇒ Object
- #run ⇒ Object
-
#usage(port = STDERR) ⇒ Object
–}}}.
Methods included from Logging
Methods included from Logging::LogMethods
#__logger_mutex, #__logger_sync, #btrace, #emsg, #errmsg, #log_err, #logger=
Methods included from ALib
configurable_main, export, simple_main
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
2319 2320 2321 |
# File 'lib/alib.rb', line 2319 def argv @argv end |
#cmdline ⇒ Object (readonly)
Returns the value of attribute cmdline.
2321 2322 2323 |
# File 'lib/alib.rb', line 2321 def cmdline @cmdline end |
#console ⇒ Object (readonly) Also known as: console?
Returns the value of attribute console.
2322 2323 2324 |
# File 'lib/alib.rb', line 2322 def console @console end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
2320 2321 2322 |
# File 'lib/alib.rb', line 2320 def env @env end |
#listoptions ⇒ Object (readonly)
Returns the value of attribute listoptions.
2324 2325 2326 |
# File 'lib/alib.rb', line 2324 def @listoptions end |
#logdev ⇒ Object (readonly)
Returns the value of attribute logdev.
2326 2327 2328 |
# File 'lib/alib.rb', line 2326 def logdev @logdev end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
2317 2318 2319 |
# File 'lib/alib.rb', line 2317 def logger @logger end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
2325 2326 2327 |
# File 'lib/alib.rb', line 2325 def op @op end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2323 2324 2325 |
# File 'lib/alib.rb', line 2323 def @options end |
#program ⇒ Object (readonly)
Returns the value of attribute program.
2318 2319 2320 |
# File 'lib/alib.rb', line 2318 def program @program end |
#verbosity ⇒ Object (readonly)
Returns the value of attribute verbosity.
2327 2328 2329 |
# File 'lib/alib.rb', line 2327 def verbosity @verbosity end |
Instance Method Details
#die(opts = {}) ⇒ Object
–}}}
2544 2545 2546 2547 2548 2549 2550 2551 |
# File 'lib/alib.rb', line 2544 def die opts = {} #--{{{ msg = Util::getopt 'msg', opts, klass.usage errno = Util::getopt 'errno', opts, EXIT_FAILURE STDERR.puts("#{ msg }") exit(Integer(errno)) #--}}} end |
#init_logging(opts = @options) ⇒ Object
–}}}
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 |
# File 'lib/alib.rb', line 2477 def init_logging opts = @options #--{{{ log = Util::getopt 'log', opts log_age = Util::getopt 'log_age', opts log_size = Util::getopt 'log_size', opts verbosity = Util::getopt 'verbosity', opts log_age = Integer log_age rescue nil log_size = Integer log_size rescue nil $logger = @logger = Logger::new(log || STDERR, log_age, log_size) # # hack to fix Logger sync bug # begin class << @logger; attr :logdev unless @logger.respond_to?(:logdev); end @logdev = @logger.logdev.dev @logdev.sync = true rescue nil end level = nil verbosity ||= 'info' verbosity = case verbosity when /^\s*(?:4|d|debug)\s*$/io level = 'Logging::DEBUG' 4 when /^\s*(?:3|i|info)\s*$/io level = 'Logging::INFO' 3 when /^\s*(?:2|w|warn)\s*$/io level = 'Logging::WARN' 2 when /^\s*(?:1|e|error)\s*$/io level = 'Logging::ERROR' 1 when /^\s*(?:0|f|fatal)\s*$/io level = 'Logging::FATAL' 0 else abort "illegal verbosity setting <#{ verbosity }>" end @logger.level = 2 - ((verbosity % 5) - 2) @logger #--}}} end |
#initialize(argv = ARGV, env = ENV) ⇒ Object
2331 2332 2333 2334 2335 2336 2337 2338 2339 |
# File 'lib/alib.rb', line 2331 def initialize argv = ARGV, env = ENV #--{{{ @argv = Util::mcp(argv.to_a) @env = Util::mcp(env.to_hash) @program = File:: $0 @cmdline = ([@program] + @argv).join ' ' @console = STDIN.tty? #--}}} end |
#klass ⇒ Object
–}}}
2340 |
# File 'lib/alib.rb', line 2340 def klass; self.class; end |
#logcatch ⇒ Object
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 |
# File 'lib/alib.rb', line 2387 def logcatch #--{{{ ret = nil @logger ||= Logger::new STDERR begin ret = yield rescue Exception => e unless SystemExit === e fatal{ e } exit EXIT_FAILURE if false if logger.debug? fatal{ e } exit EXIT_FAILURE else fatal{ emsg(e) } exit EXIT_FAILURE end end else exit e.status end end ret #--}}} end |
#main ⇒ Object
–}}}
2552 2553 2554 2555 2556 |
# File 'lib/alib.rb', line 2552 def main #--{{{ raise NotImplementedError, 'you need to define main' #--}}} end |
#optional_arguments ⇒ Object
–}}}
2346 2347 2348 2349 2350 |
# File 'lib/alib.rb', line 2346 def optional_arguments #--{{{ klass::optional_arguments #--}}} end |
#parse_argv ⇒ Object
–}}}
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 |
# File 'lib/alib.rb', line 2522 def parse_argv #--{{{ a, b = [], [] klass::required_arguments.each do |arg| value = @argv.shift if value send "#{ arg }=", value else die 'msg' => "required_argument <#{ arg }> not given" end a << send("#{ arg }") end klass::optional_arguments.each do |arg| value = @argv.shift if value send "#{ arg }=", value end b << send("#{ arg }") end [a, b, @argv] #--}}} end |
#parse_options ⇒ Object
–}}}
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 |
# File 'lib/alib.rb', line 2456 def #--{{{ @op = OptionParser::new @options = {} @listoptions = Hash::new{|h,k| h[k] = []} klass::optspec.each do |spec| k = spec.first.gsub(%r/(?:--)|(?:=.*$)|(?:\s+)/o,'') @op.def_option(*spec) do |v| @options[k] = v @listoptions[k] << v end end #begin op.parse! @argv #rescue OptionParser::InvalidOption => e # preverve unknown options #e.recover(argv) #end @options #--}}} end |
#post_main ⇒ Object
2386 |
# File 'lib/alib.rb', line 2386 def post_main; end |
#post_parse_argv ⇒ Object
2382 |
# File 'lib/alib.rb', line 2382 def post_parse_argv; end |
#post_parse_options ⇒ Object
2380 |
# File 'lib/alib.rb', line 2380 def ; end |
#post_run ⇒ Object
2384 |
# File 'lib/alib.rb', line 2384 def post_run; end |
#pre_main ⇒ Object
2385 |
# File 'lib/alib.rb', line 2385 def pre_main; end |
#pre_parse_argv ⇒ Object
2381 |
# File 'lib/alib.rb', line 2381 def pre_parse_argv; end |
#pre_parse_options ⇒ Object
–}}}
2379 |
# File 'lib/alib.rb', line 2379 def ; end |
#pre_run ⇒ Object
2383 |
# File 'lib/alib.rb', line 2383 def pre_run; end |
#required_arguments ⇒ Object
2341 2342 2343 2344 2345 |
# File 'lib/alib.rb', line 2341 def required_arguments #--{{{ klass::required_arguments #--}}} end |
#run ⇒ Object
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 |
# File 'lib/alib.rb', line 2352 def run #--{{{ logcatch do begin pre_run if(@options.has_key?('help') or (@argv.size == 1 and @argv.first =~ %r/help/i)) usage STDOUT exit EXIT_SUCCESS end pre_parse_argv parse_argv post_parse_argv init_logging pre_main status = main post_main post_run exit(status ? EXIT_SUCCESS : EXIT_FAILURE) rescue Errno::EPIPE STDOUT.tty? ? raise : exit(EXIT_FAILURE) end end #--}}} end |
#usage(port = STDERR) ⇒ Object
–}}}
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 |
# File 'lib/alib.rb', line 2413 def usage port = STDERR #--{{{ port << klass::usage << "\n" if klass::usage port << klass::examples << "\n" if klass::examples if klass::optspec port << 'OPTIONS' << "\n" klass::optspec.each do |os| a, b, c = os long, short, desc = nil [a,b,c].each do |word| next unless word word.strip! case word when %r/^--[^-]/o long = word when %r/^-[^-]/o short = word else desc = word end end spec = ((long and short) ? [long, short] : [long]) if spec port << Util::columnize(spec.join(', '), :width => 80, :indent => 2) port << "\n" end if desc port << Util::columnize(desc, :width => 80, :indent => 8) port << "\n" end end port << "\n" end port #--}}} end |