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
- #banner(arg) ⇒ Object
- #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
- #usage! ⇒ Object
Constructor Details
#initialize(env, arguments, **opts) ⇒ CLI
Returns a new instance of CLI.
50 51 52 53 54 55 56 57 |
# File 'lib/baf/cli.rb', line 50 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.
48 49 50 |
# File 'lib/baf/cli.rb', line 48 def arguments @arguments end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
48 49 50 |
# File 'lib/baf/cli.rb', line 48 def env @env end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
48 49 50 |
# File 'lib/baf/cli.rb', line 48 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
#banner(arg) ⇒ Object
62 63 64 |
# File 'lib/baf/cli.rb', line 62 def arg registrant. = arg end |
#flag(*args) ⇒ Object
66 67 68 |
# File 'lib/baf/cli.rb', line 66 def flag *args registrant.flag *args end |
#flag_debug ⇒ Object
70 71 72 |
# File 'lib/baf/cli.rb', line 70 def flag_debug flag :d, :debug end |
#flag_verbose ⇒ Object
74 75 76 |
# File 'lib/baf/cli.rb', line 74 def flag_verbose flag :v, :verbose end |
#flag_version(version) ⇒ Object
78 79 80 81 |
# File 'lib/baf/cli.rb', line 78 def flag_version version flag :V, :version, 'print version', -> *, env { env.puts version; exit }, tail: true end |
#option(*args) ⇒ Object
83 84 85 86 |
# File 'lib/baf/cli.rb', line 83 def option *args args = [*args, Proc.new] if block_given? registrant.option *args end |
#parse_arguments! ⇒ Object
88 89 90 91 92 |
# File 'lib/baf/cli.rb', line 88 def parse_arguments! parser.parse! arguments rescue OptionParser::InvalidOption raise ArgumentError, parser end |
#run ⇒ Object
98 99 |
# File 'lib/baf/cli.rb', line 98 def run end |
#setup ⇒ Object
59 60 |
# File 'lib/baf/cli.rb', line 59 def setup end |
#usage! ⇒ Object
94 95 96 |
# File 'lib/baf/cli.rb', line 94 def usage! fail ArgumentError, parser end |