Class: CommandLineBoss
- Inherits:
-
Object
- Object
- CommandLineBoss
- Defined in:
- lib/command_line_boss.rb,
lib/command_line_boss/version.rb,
lib/command_line_boss/help_option.rb,
lib/command_line_boss/logger_options.rb
Overview
Command line interface parser based on OptionsParser
Defined Under Namespace
Modules: HelpOption, LoggerOptions
Constant Summary collapse
- VERSION =
Gem version
'0.2.3'
Instance Attribute Summary collapse
-
#program_name ⇒ String
readonly
The name of the program to report in the usage line.
Instance Method Summary collapse
-
#error_messages ⇒ Array<String>
The error messages generated by the validation methods.
-
#failed? ⇒ Boolean
true if the parser encountered errors.
-
#initialize(program_name: $PROGRAM_NAME) ⇒ CommandLineBoss
constructor
Create a new command line parser.
-
#parse(args) ⇒ CommandLineParser
Parse the command line arguments and return self.
-
#parse!(args) ⇒ CommandLineParser
Parse the command line arguments and return self.
-
#succeeded? ⇒ Boolean
true if the parser encountered no errors.
Constructor Details
#initialize(program_name: $PROGRAM_NAME) ⇒ CommandLineBoss
Create a new command line parser
25 26 27 28 29 30 31 |
# File 'lib/command_line_boss.rb', line 25 def initialize(program_name: $PROGRAM_NAME) @program_name = program_name @parser = OptionParser.new.tap { |p| p.set_program_name(program_name) } @error_messages = [] set_defaults if private_methods.include?(:set_defaults) end |
Instance Attribute Details
#program_name ⇒ String (readonly)
The name of the program to report in the usage line
The program name is used in the usage line when the --help option is given. This is given as an optional argument so that it can be overridden in tests.
90 91 92 |
# File 'lib/command_line_boss.rb', line 90 def program_name @program_name end |
Instance Method Details
#error_messages ⇒ Array<String>
The error messages generated by the validation methods
99 |
# File 'lib/command_line_boss.rb', line 99 def = @error_messages.dup.freeze |
#failed? ⇒ Boolean
true if the parser encountered errors
118 |
# File 'lib/command_line_boss.rb', line 118 def failed? = !succeeded? |
#parse(args) ⇒ CommandLineParser
Parse the command line arguments and return self
The caller will have to check the #failed? to see if there were any errors.
If there were errors, #error_messages will contain the error messages.
46 47 48 49 50 51 52 |
# File 'lib/command_line_boss.rb', line 46 def parse(args) @args = args.dup parse_arguments validate if @error_messages.empty? self end |
#parse!(args) ⇒ CommandLineParser
Parse the command line arguments and return self
If there were any errors parsing the command line arguments, this method will output the error messages to stderr and exit the program with a non-zero status code.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/command_line_boss.rb', line 69 def parse!(args) parse(args) if failed? warn .join("\n") exit 1 end self end |
#succeeded? ⇒ Boolean
true if the parser encountered no errors
109 |
# File 'lib/command_line_boss.rb', line 109 def succeeded? = @error_messages.empty? |