Class: Skylab::Face::Cli
- Inherits:
-
Object
- Object
- Skylab::Face::Cli
- Extended by:
- Skylab::Face::Command::TreeDefiner
- Defined in:
- lib/myterm/vendor/skylab/face/cli.rb
Direct Known Subclasses
Constant Summary collapse
Constants included from Skylab::Face::Command::Treeish
Skylab::Face::Command::Treeish::Indent
Constants included from Colors
Skylab::Face::Colors::Esc, Skylab::Face::Colors::Styles
Instance Attribute Summary collapse
-
#err ⇒ Object
readonly
Returns the value of attribute err.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
Class Method Summary collapse
Instance Method Summary collapse
- #argument_error(e, cmd) ⇒ Object
-
#initialize ⇒ Cli
constructor
A new instance of Cli.
- #program_name ⇒ Object (also: #invocation_string)
- #run(argv) ⇒ Object
- #version ⇒ Object
Methods included from Skylab::Face::Command::TreeDefiner
command_definitions, command_tree, inherited, method_added, namespace, on, option_definitions, option_parser
Methods included from Skylab::Face::Command::Treeish
[], #ambiguous_command, #command_tree, #expecting, #find_command, #help, #option_parser, #run_opts, #unrecognized_command, #usage_string
Methods included from Skylab::Face::Command::Nodeish
#build_empty_option_parser, #invite, #parent=, #usage
Methods included from Colors
#bold, #hi, #highlight_header, #ohno, #style, #yelo
Constructor Details
#initialize ⇒ Cli
Returns a new instance of Cli.
355 356 357 358 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 355 def initialize @out = $stdout @err = $stderr end |
Instance Attribute Details
#err ⇒ Object (readonly)
Returns the value of attribute err.
359 360 361 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 359 def err @err end |
#out ⇒ Object (readonly)
Returns the value of attribute out.
359 360 361 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 359 def out @out end |
Class Method Details
.version(*a, &block) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 390 def version *a, &block if a.any? and block raise ArgumentError.new("can't process args and block together.") elsif a.any? or block option_definitions.detect { |arr, _| '-v' == arr[0] } or on('-v', '--version', 'shows version') { version } if block @version = block else @version = a.length == 1 ? a.first : a end else @version.kind_of?(Proc) ? @version.call : @version end end |
Instance Method Details
#argument_error(e, cmd) ⇒ Object
361 362 363 364 365 366 367 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 361 def argument_error e, cmd e.backtrace[0,2].detect { |s| s.match(/\A[^:]+/)[0] == __FILE__ } or raise e msg = e..sub(/\((\d+) for (\d+)\)\Z/) do "(#{$1.to_i - 1} for #{$2.to_i - 1})" end cmd.usage msg end |
#program_name ⇒ Object Also known as: invocation_string
368 369 370 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 368 def program_name @program_name ||= File.basename($PROGRAM_NAME) end |
#run(argv) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 372 def run argv argv.empty? and return empty_argv runner = self begin argv.first =~ /^-/ and return runner.run_opts(argv) cmd = runner.find_command(argv) end while (cmd and cmd.respond_to?(:find_command) and runner = cmd) cmd and req = cmd.parse(argv) and begin runner.send(cmd.method_symbol, req, * req.method_parameters) rescue ArgumentError => e argument_error e, cmd end end |
#version ⇒ Object
386 387 388 |
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 386 def version @err.puts hi([program_name, interface.version].compact.join(' ')) end |