Class: Skylab::Face::Cli

Inherits:
Object
  • Object
show all
Extended by:
Skylab::Face::Command::TreeDefiner
Includes:
Colors, Skylab::Face::Command::Nodeish, Skylab::Face::Command::Treeish
Defined in:
lib/myterm/vendor/skylab/face/cli.rb

Direct Known Subclasses

Myterm::Cli

Constant Summary collapse

Face =
Skylab::Face

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

Class Method Summary collapse

Instance Method Summary collapse

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

#initializeCli

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

#errObject (readonly)

Returns the value of attribute err.



359
360
361
# File 'lib/myterm/vendor/skylab/face/cli.rb', line 359

def err
  @err
end

#outObject (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.message.sub(/\((\d+) for (\d+)\)\Z/) do
    "(#{$1.to_i - 1} for #{$2.to_i - 1})"
  end
  cmd.usage msg
end

#program_nameObject 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

#versionObject



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