Class: Baf::CLI

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

Constant Summary collapse

ArgumentError =
Class.new ::Baf::ArgumentError
EX_USAGE =
64
EX_SOFTWARE =
70

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, arguments, **opts) ⇒ CLI

Returns a new instance of CLI.



52
53
54
55
56
57
58
59
# File 'lib/baf/cli.rb', line 52

def initialize env, arguments, **opts
  @env = env
  @arguments = arguments
  @parser = opts.fetch(:parser) { OptionParser.new }
  @registrant = opts.fetch(:registrant) { OptionsRegistrant.new }

  registrant.register(env, parser) { setup }
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



50
51
52
# File 'lib/baf/cli.rb', line 50

def arguments
  @arguments
end

#envObject (readonly)

Returns the value of attribute env.



50
51
52
# File 'lib/baf/cli.rb', line 50

def env
  @env
end

#parserObject (readonly)

Returns the value of attribute parser.



50
51
52
# File 'lib/baf/cli.rb', line 50

def parser
  @parser
end

Class Method Details

.run(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/baf/cli.rb', line 17

def run arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr
  cli = new env = build_env(stdin, stdout, stderr), arguments
  cli.parse_arguments!
  cli.run
rescue ArgumentError => e
  stderr.puts e
  exit EX_USAGE
rescue StandardError => e
  if respond_to? :handle_error
    status = handle_error cli.env, e
    exit status if status.respond_to? :to_int
  else
    stderr.puts "#{e.class.name}: #{e}"
    stderr.puts e.backtrace.map { |l| '  %s' % l }
  end
  exit EX_SOFTWARE
end

Instance Method Details



64
65
66
# File 'lib/baf/cli.rb', line 64

def banner arg
  registrant.banner = arg
end

#flag_debugObject



73
74
75
# File 'lib/baf/cli.rb', line 73

def flag_debug
  flag :d, :debug
end

#flag_verboseObject



77
78
79
# File 'lib/baf/cli.rb', line 77

def flag_verbose
  flag :v, :verbose
end

#flag_version(version) ⇒ Object



81
82
83
84
# File 'lib/baf/cli.rb', line 81

def flag_version version
  flag :V, :version, 'print version', -> *, env { env.puts version; exit },
    tail: true
end

#option(*args, &block) ⇒ Object



86
87
88
89
# File 'lib/baf/cli.rb', line 86

def option *args, &block
  args = [*args, block] if block_given?
  registrant.option *args
end

#parse_arguments!Object



91
92
93
94
95
# File 'lib/baf/cli.rb', line 91

def parse_arguments!
  parser.parse! arguments
rescue OptionParser::InvalidOption
  fail ArgumentError, parser
end

#ruby2_keywordsObject



47
# File 'lib/baf/cli.rb', line 47

def ruby2_keywords *; end

#runObject



101
102
# File 'lib/baf/cli.rb', line 101

def run
end

#setupObject



61
62
# File 'lib/baf/cli.rb', line 61

def setup
end

#usage!Object



97
98
99
# File 'lib/baf/cli.rb', line 97

def usage!
  fail ArgumentError, parser
end