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.



41
42
43
44
45
46
47
48
# File 'lib/baf/cli.rb', line 41

def initialize env, arguments, **opts
  @env        = env
  @arguments  = arguments
  @parser     = opts[:parser]     || OptionParser.new
  @registrant = opts[:registrant] || OptionsRegistrant.new

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



39
40
41
# File 'lib/baf/cli.rb', line 39

def arguments
  @arguments
end

#envObject (readonly)

Returns the value of attribute env.



39
40
41
# File 'lib/baf/cli.rb', line 39

def env
  @env
end

#parserObject (readonly)

Returns the value of attribute parser.



39
40
41
# File 'lib/baf/cli.rb', line 39

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
# File 'lib/baf/cli.rb', line 17

def run arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr
  cli = new env_class.new(input: stdin, output: stdout), arguments
  cli.parse_arguments!
  cli.run
rescue ArgumentError => e
  stderr.puts e
  exit EX_USAGE
rescue StandardError => e
  stderr.puts "#{e.class.name}: #{e}"
  stderr.puts e.backtrace.map { |l| '  %s' % l }
  exit EX_SOFTWARE
end

Instance Method Details

#flag(*args) ⇒ Object



53
54
55
# File 'lib/baf/cli.rb', line 53

def flag *args
  registrant.flag *args
end

#flag_debugObject



57
58
59
# File 'lib/baf/cli.rb', line 57

def flag_debug
  flag :d, :debug
end

#flag_verboseObject



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

def flag_verbose
  flag :v, :verbose
end

#flag_version(version) ⇒ Object



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

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

#option(*args) ⇒ Object



70
71
72
73
# File 'lib/baf/cli.rb', line 70

def option *args
  args = [*args, Proc.new] if block_given?
  registrant.option *args
end

#parse_arguments!Object



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

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

#runObject



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

def run
end

#setupObject



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

def setup
end