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:



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

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

#argvObject

Returns the value of attribute argv.



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

def argv
  @argv
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



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

def exit_status
  @exit_status
end

#oparseObject

Returns the value of attribute oparse.



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

def oparse
  @oparse
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

#stderrObject

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



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

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Class Method Details

.run(param = {}) ⇒ Object



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

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

Instance Method Details

#bail(msg) ⇒ Object

This method exits with a message on stderr



58
59
60
61
# File 'lib/rbkb/cli.rb', line 58

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



67
68
69
# File 'lib/rbkb/cli.rb', line 67

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



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

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



109
110
111
112
113
114
115
116
# File 'lib/rbkb/cli.rb', line 109

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

  parse

  # the overriding class implements actual functionality beyond here
end

#make_parserObject

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rbkb/cli.rb', line 77

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

  return @oparse
end

#parseObject

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



97
98
99
100
101
102
103
# File 'lib/rbkb/cli.rb', line 97

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

  # the overriding class may implement additional arguments from here
end