Module: ShellOpts
- Defined in:
- lib/shellopts.rb,
lib/shellopts/ansi.rb,
lib/shellopts/args.rb,
lib/shellopts/dump.rb,
lib/shellopts/lexer.rb,
lib/shellopts/stack.rb,
lib/shellopts/token.rb,
lib/shellopts/option.rb,
lib/shellopts/parser.rb,
lib/shellopts/grammar.rb,
lib/shellopts/program.rb,
lib/shellopts/version.rb,
lib/shellopts/analyzer.rb,
lib/shellopts/renderer.rb,
lib/shellopts/formatter.rb,
lib/shellopts/interpreter.rb,
lib/shellopts/argument_type.rb
Overview
Option rendering
-a, --all # Only used in brief and doc formats (enum)
--all # Only used in usage (long)
-a # Only used in usage (short)
Option group rendering
-a, --all -b, --beta # Only used in brief formats (enum)
--all --beta # Used in usage (long)
-a -b # Used in usage (short)
-a, --all # Only used in doc format (:multi)
-b, --beta
Command rendering
cmd --all --beta [cmd1|cmd2] ARG1 ARG2 # Single-line formats (:single)
cmd --all --beta [cmd1|cmd2] ARGS... # Not used
cmd -a -b [cmd1|cmd2] ARG1 ARG2
cmd -a -b [cmd1|cmd2] ARGS... # Not used
cmd -a -b [cmd1|cmd2] ARG1 ARG2 # One line for each argument description (:enum)
cmd -a -b [cmd1|cmd2] ARG3 ARG4 # (used in the USAGE section)
cmd --all --beta # Multi-line formats (:multi)
[cmd1|cmd2] ARG1 ARG2
cmd --all --beta
<commands> ARGS
Defined Under Namespace
Modules: Debug, ErrorHandling, Grammar, Message, Stack, Verbose
Classes: Analyzer, AnalyzerError, Ansi, Args, Command, CompilerError, Error, Failure, Formatter, InternalError, Interpreter, Lexer, LexerError, Line, Option, Parser, ParserError, Program, ShellOpts, ShellOptsError, Token
Constant Summary
collapse
- VERSION =
"2.9.6"
- @@PROGRAM_PATH =
The full path to the program. This is initialized when shellopts.rb is
loaded so it is not affected by later changes of directory
File.absolute_path($PROGRAM_NAME)
- @@ENVIRONMENT_PATH =
The full path to the user's current directory when the program was called
File.absolute_path(Dir.getwd)
Class Method Summary
collapse
-
.clear_screen ⇒ Object
-
.clear_screen=(clear) ⇒ Object
-
.debug(message, newline: true) ⇒ Object
-
.debug? ⇒ Boolean
-
.environment_path ⇒ Object
-
.error(message, exit: 1) ⇒ Object
-
.exception ⇒ Object
-
.exception=(value) ⇒ Object
-
.failure(message, exit: 1) ⇒ Object
-
.handle_exit(value) ⇒ Object
Exit program with the given status if an integer and status 1 if not.
-
.instance ⇒ Object
-
.instance=(instance) ⇒ Object
-
.instance? ⇒ Boolean
-
.mesg(message, newline: true) ⇒ Object
Emit a message on standard output.
-
.notice(message, newline: true) ⇒ Object
Emit a message on standard error.
-
.process(spec, argv, silent: nil, quiet: nil, verbose: nil, debug: nil, **opts) ⇒ Object
-
.program_path ⇒ Object
-
.quiet? ⇒ Boolean
-
.shellopts ⇒ Object
-
.silent? ⇒ Boolean
Returns the corresponding option status on the program object.
-
.verb(level = 1, message, newline: true) ⇒ Object
Emit a message on standard output.
-
.verbose?(level = 1) ⇒ Boolean
Class Method Details
.clear_screen ⇒ Object
454
|
# File 'lib/shellopts.rb', line 454
def self.clear_screen = @clear_screen
|
.clear_screen=(clear) ⇒ Object
455
|
# File 'lib/shellopts.rb', line 455
def self.clear_screen=(clear) @clear_screen = clear end
|
.debug(message, newline: true) ⇒ Object
524
525
526
527
528
529
530
|
# File 'lib/shellopts.rb', line 524
def self.debug(message, newline: true)
method = newline ? :puts : :print
if debug?
$stdout.send(method, message)
$stdout.flush
end
end
|
.debug? ⇒ Boolean
475
|
# File 'lib/shellopts.rb', line 475
def self.debug?() instance.program.__debug__ end
|
.environment_path ⇒ Object
432
|
# File 'lib/shellopts.rb', line 432
def self.environment_path = @@ENVIRONMENT_PATH
|
.error(message, exit: 1) ⇒ Object
477
478
479
480
481
482
483
484
485
|
# File 'lib/shellopts.rb', line 477
def self.error(message, exit: 1)
raise Error.new(message) if exception
if instance?
instance.error(message, exit: exit)
else
$stderr.puts "#{File.basename($PROGRAM_NAME)}: #{message}"
handle_exit(exit)
end
end
|
.exception ⇒ Object
447
|
# File 'lib/shellopts.rb', line 447
def self.exception = @exception
|
.exception=(value) ⇒ Object
448
|
# File 'lib/shellopts.rb', line 448
def self.exception=(value) @exception = value end
|
.failure(message, exit: 1) ⇒ Object
487
488
489
490
491
492
493
494
495
|
# File 'lib/shellopts.rb', line 487
def self.failure(message, exit: 1)
raise Error.new(message) if exception
if instance?
instance.failure(message, exit: exit)
else
$stderr.puts "#{File.basename($PROGRAM_NAME)}: #{message}"
handle_exit(exit)
end
end
|
.handle_exit(value) ⇒ Object
Exit program with the given status if an integer and status 1 if not. Do
not exit if status is falsy
535
536
537
|
# File 'lib/shellopts.rb', line 535
def self.handle_exit(value)
exit(value.is_a?(Integer) ? value : 1) if value
end
|
.instance ⇒ Object
461
|
# File 'lib/shellopts.rb', line 461
def self.instance() @instance or raise Error, "ShellOpts is not initialized" end
|
.instance=(instance) ⇒ Object
462
|
# File 'lib/shellopts.rb', line 462
def self.instance=(instance) @instance = instance end
|
.instance? ⇒ Boolean
460
|
# File 'lib/shellopts.rb', line 460
def self.instance?() !@instance.nil? end
|
.mesg(message, newline: true) ⇒ Object
Emit a message on standard output. The --quiet option suppresses these messages
507
508
509
510
511
512
513
|
# File 'lib/shellopts.rb', line 507
def self.mesg(message, newline: true)
method = newline ? :puts : :print
if !quiet?
$stdout.send(method, message)
$stdout.flush
end
end
|
.notice(message, newline: true) ⇒ Object
Emit a message on standard error. The --silent option suppresses these messages
498
499
500
501
502
503
504
|
# File 'lib/shellopts.rb', line 498
def self.notice(message, newline: true)
method = newline ? :puts : :print
if !silent?
$stderr.send(method, message)
$stderr.flush
end
end
|
.process(spec, argv, silent: nil, quiet: nil, verbose: nil, debug: nil, **opts) ⇒ Object
434
435
436
437
438
439
440
441
442
|
# File 'lib/shellopts.rb', line 434
def self.process(spec, argv, silent: nil, quiet: nil, verbose: nil, debug: nil, **opts)
constrain silent, String, true, false, nil
constrain quiet, String, true, false, nil
silent = silent.nil? ? Message.is_included? || Verbose.is_included? : silent
quiet = quiet.nil? ? Message.is_included? || Verbose.is_included? : quiet
verbose = verbose.nil? ? ::ShellOpts::Verbose.is_included? : verbose
debug = debug.nil? ? Debug.is_included? : debug
ShellOpts.process(spec, argv, silent: silent, quiet: quiet, verbose: verbose, debug: debug, **opts)
end
|
.program_path ⇒ Object
428
|
# File 'lib/shellopts.rb', line 428
def self.program_path = @@PROGRAM_PATH
|
.quiet? ⇒ Boolean
473
|
# File 'lib/shellopts.rb', line 473
def self.quiet?() silent? || instance.program.__quiet__ end
|
.shellopts ⇒ Object
463
|
# File 'lib/shellopts.rb', line 463
def self.shellopts() instance end
|
.silent? ⇒ Boolean
Returns the corresponding option status on the program object. Note that
the "bare" ShellOpts standard option methods (eg. ShellOpts.silent)
determines if an option can be used while the query methods (eg.
ShellOpts.silent?) reports if the option was present on the command line
The methods below are implemented using the name-independent members of
Program: silent etc.
472
|
# File 'lib/shellopts.rb', line 472
def self.silent?() instance.program.__silent__ end
|
.verb(level = 1, message, newline: true) ⇒ Object
Emit a message on standard output. The --verbose option controls these messages
516
517
518
519
520
521
522
|
# File 'lib/shellopts.rb', line 516
def self.verb(level = 1, message, newline: true)
method = newline ? :puts : :print
if verbose?(level)
$stdout.send(method, message)
$stdout.flush
end
end
|
.verbose?(level = 1) ⇒ Boolean
474
|
# File 'lib/shellopts.rb', line 474
def self.verbose?(level = 1) level <= instance.program.__verbose__ end
|