Class: Rbkb::Cli::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/rbkb/cli.rb

Overview

Rbkb::Cli::Executable is an abstract class for creating command line executables using the Ruby Black Bag framework.

Direct Known Subclasses

B64, Bgrep, Blit, Chars, Crc32, D64, Dedump, Feed, Hexify, Len, PlugCli, Rstrings, Slice, Unhexify, Urldec, Urlenc, Xor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param = {}) {|_self| ... } ⇒ Executable

Instantiates a new Executable object.

The 'param' argument is a named value hash. The following keys are significant:

:argv - An array of cli arguments (default ARGV) :opts - executable/function options for use when running 'go' :stdout, - IO redirection (mostly for unit tests) :stderr, :stdin

The above keys are deleted from the 'param' hash and stored as instance variables with attr_accessors. All other parameters are ignored.

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
40
41
42
# File 'lib/rbkb/cli.rb', line 33

def initialize(param = {})
  @argv   ||= param.delete(:argv) || ARGV
  @stdout ||= param.delete(:stdout) || STDOUT
  @stderr ||= param.delete(:stderr) || STDERR
  @stdin  ||= param.delete(:stdin) || STDIN
  @opts   ||= param.delete(:opts) || {}
  @parser_got_range = nil
  yield self if block_given?
  make_parser
end

Instance Attribute Details

#argv ⇒ Object

Returns the value of attribute argv.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def argv
  @argv
end

#exit_status ⇒ Object (readonly)

Returns the value of attribute exit_status.



17
18
19
# File 'lib/rbkb/cli.rb', line 17

def exit_status
  @exit_status
end

#oparse ⇒ Object

Returns the value of attribute oparse.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def oparse
  @oparse
end

#opts ⇒ Object

Returns the value of attribute opts.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def opts
  @opts
end

#stderr ⇒ Object

Returns the value of attribute stderr.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def stderr
  @stderr
end

#stdin ⇒ Object

Returns the value of attribute stdin.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def stdin
  @stdin
end

#stdout ⇒ Object

Returns the value of attribute stdout.



16
17
18
# File 'lib/rbkb/cli.rb', line 16

def stdout
  @stdout
end

Class Method Details

.run(param = {}) ⇒ Object



12
13
14
# File 'lib/rbkb/cli.rb', line 12

def self.run(param = {})
  new(param).go
end

Instance Method Details

#bail(msg) ⇒ Object

This method exits with a message on stderr



55
56
57
58
# File 'lib/rbkb/cli.rb', line 55

def bail(msg)
  @stderr.puts msg if msg
  self.exit(1)
end

#bail_args(arg_err) ⇒ Object

This method wraps a 'bail' with a basic argument error mesage and hint for the '-h or --help' flag The 'arg_err' parameter is a string with the erroneous arguments



63
64
65
# File 'lib/rbkb/cli.rb', line 63

def bail_args(arg_err)
  bail "Error: bad arguments - #{arg_err}\n  Hint: Use -h or --help"
end

#exit(ret) ⇒ Object

Wrapper for Kernel.exit() so we can unit test cli tools



45
46
47
48
49
50
51
52
# File 'lib/rbkb/cli.rb', line 45

def exit(ret)
  @exit_status = ret
  if defined? Rbkb::Cli::TESTING
    throw((ret == 0 ? :exit_zero : :exit_err), ret)
  else
    Kernel.exit(ret)
  end
end

#go(argv = nil) ⇒ Object

Abstract 'runner'. Override this method with super() from inherited executables. The base method just slurps in an optional argv and runs 'parse' if it hasn't already



106
107
108
109
110
111
112
113
# File 'lib/rbkb/cli.rb', line 106

def go(argv = nil)
  @exit_status = nil
  @argv = argv if argv

  parse

  # the overriding class implements actual functionality beyond here
end

#make_parser ⇒ Object

Prepares an OptionsParser object with blackbag standard options This is called from within initialize() and should be overridden in inherited classes to add additional OptionParser-based parsers.

See parse for actual parsing.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbkb/cli.rb', line 72

def make_parser
  @oparse ||= OptionParser.new
  @oparse.banner = "Usage: #{File.basename $0} [options]"

  @oparse.on('-h', '--help', 'Show this message') do
    bail(@oparse)
  end

  @oparse.on('-v', '--version', 'Show version and exit') do
    @stdout.puts("Ruby BlackBag version #{Rbkb::VERSION}")
    self.exit(0)
  end

  @oparse
end

#parse ⇒ Object

Abstract argument parser. Override this method with super() from inherited executables. The base method just calls OptionParser.parse! on the internal @oparse object.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbkb/cli.rb', line 91

def parse
  # parse flag arguments
  begin
    @oparse.parse!(@argv)
  rescue StandardError
    (bail_args($!))
  end
  @parsed = true

  # the overriding class may implement additional arguments from here
end