Class: Baf::CLI
- Inherits:
-
Object
- Object
- Baf::CLI
- Defined in:
- lib/baf/cli.rb
Constant Summary collapse
- ArgumentError =
Class.new(::Baf::ArgumentError)
- EX_USAGE =
64
- EX_SOFTWARE =
70
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
Instance Method Summary collapse
- #flag(*args) ⇒ Object
- #flag_debug ⇒ Object
- #flag_verbose ⇒ Object
- #flag_version(version) ⇒ Object
-
#initialize(env, arguments, **opts) ⇒ CLI
constructor
A new instance of CLI.
- #option(*args) ⇒ Object
- #parse_arguments! ⇒ Object
- #run ⇒ Object
- #setup ⇒ Object
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
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
39 40 41 |
# File 'lib/baf/cli.rb', line 39 def arguments @arguments end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
39 40 41 |
# File 'lib/baf/cli.rb', line 39 def env @env end |
#parser ⇒ Object (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_debug ⇒ Object
57 58 59 |
# File 'lib/baf/cli.rb', line 57 def flag_debug flag :d, :debug end |
#flag_verbose ⇒ Object
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 |
#run ⇒ Object
81 82 |
# File 'lib/baf/cli.rb', line 81 def run end |
#setup ⇒ Object
50 51 |
# File 'lib/baf/cli.rb', line 50 def setup end |