Class: CommandLineBoss

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(program_name: $PROGRAM_NAME) ⇒ CommandLineBoss

Create a new command line parser

Examples:

parser = CommandLineParser.new

Parameters:

  • program_name (String) (defaults to: $PROGRAM_NAME)

    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.



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)
  define_options
end

Instance Attribute Details

#program_nameString (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.

Examples:

options.program_name #=> 'sync_paranoids_milestones'

Returns:

  • (String)


90
91
92
# File 'lib/command_line_boss.rb', line 90

def program_name
  @program_name
end

Instance Method Details

#error_messagesArray<String>

The error messages generated by the validation methods

Examples:

options.error_messages #=> ["ERROR: The sheet 'Sheet1' was given more than once"]

Returns:

  • (Array<String>)


99
# File 'lib/command_line_boss.rb', line 99

def error_messages = @error_messages.dup.freeze

#failed?Boolean

true if the parser encountered errors

Examples:

options.failed? #=> false

Returns:

  • (Boolean)


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.

Examples:

parser = CommandLineParser.new.call(ARGV)

Parameters:

  • args (Array<String>)

    the command line arguments

Returns:

  • (CommandLineParser)

    returns self



46
47
48
49
50
51
52
# File 'lib/command_line_boss.rb', line 46

def parse(args)
  @args = args.dup
  parse_options
  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.

Examples:

parser = CommandLineParser.new.parse!(ARGV)

Parameters:

  • args (Array<String>)

    the command line arguments

Returns:

  • (CommandLineParser)

    returns self

Raises:

  • (SystemExit)

    if the command line arguments are invalid



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 error_messages.join("\n")
    exit 1
  end

  self
end

#succeeded?Boolean

true if the parser encountered no errors

Examples:

options.succeeded? #=> true

Returns:

  • (Boolean)


109
# File 'lib/command_line_boss.rb', line 109

def succeeded? = @error_messages.empty?