Class: RubyUtils::BaseHelpOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_utils/base_help_output.rb

Overview

Abstract output class for help text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#parametersHash (readonly, private)

Returns hash which stores available parameters and their help text.

Returns:

  • (Hash)

    hash which stores available parameters and their help text



25
26
27
# File 'lib/ruby_utils/base_help_output.rb', line 25

def parameters
  @parameters
end

Class Method Details

.add_dual_argument_text(symbol, argument, parameters, text) ⇒ Object (private)

method to add a (key, value) pair where the value contains help text with two argument

Parameters:

  • symbol (Symbol)

    the key

  • argument (String)

    the string part containing the argument

  • parameters (String)

    the string part containing the required parameters

  • text (String)

    the string part containing the description text



121
122
123
124
# File 'lib/ruby_utils/base_help_output.rb', line 121

def self.add_dual_argument_text(symbol, argument, parameters, text)
  add_text(symbol, build_entry(argument.light_blue, "arguments:",
                                      parameters, text))
end

.add_one_argument_help_entriesObject (private)

method to specify and add the help entries with help text and one argument



49
50
51
52
53
# File 'lib/ruby_utils/base_help_output.rb', line 49

def self.add_one_argument_help_entries
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: add_one_argument_help_entries from its" \
         " base class".red
end

.add_simple_text(symbol, argument, text) ⇒ Object (private)

method to add a (key, value) pair where the value contains the help text

Parameters:

  • symbol (Symbol)

    the key

  • argument (String)

    the string part containing the argument

  • text (String)

    the string part containing the description text



99
100
101
# File 'lib/ruby_utils/base_help_output.rb', line 99

def self.add_simple_text(symbol, argument, text)
  add_text(symbol, argument.light_blue.concat(text))
end

.add_single_argument_text(symbol, argument, parameter, text) ⇒ Object (private)

method to add a (key, value) pair where the value contains help text with one argument

Parameters:

  • symbol (Symbol)

    the key

  • argument (String)

    the string part containing the argument

  • parameter (String)

    the string part containing the required parameter

  • text (String)

    the string part containing the description text



109
110
111
112
# File 'lib/ruby_utils/base_help_output.rb', line 109

def self.add_single_argument_text(symbol, argument, parameter, text)
  add_text(symbol, build_entry(argument.light_blue, "argument:",
                                      parameter, text))
end

.add_single_help_entriesObject (private)

method to specify and add the help entries with help text only



41
42
43
44
45
46
# File 'lib/ruby_utils/base_help_output.rb', line 41

def self.add_single_help_entries
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: add_single_help_entries from its base" \
         " class".red
 
end

.add_text(symbol, text) ⇒ Object (private)

method to add a (key, value) pair to the parameter hash

Parameters:

  • symbol (Symbol)

    the key

  • text (String)

    the value containing a formatted string



91
92
93
# File 'lib/ruby_utils/base_help_output.rb', line 91

def self.add_text(symbol, text)
  @parameters[symbol] = text
end

.add_two_argument_help_entriesObject (private)

method to specify and add the help entries with help text and two arguments



56
57
58
59
60
# File 'lib/ruby_utils/base_help_output.rb', line 56

def self.add_two_argument_help_entries
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: add_two_argument_help_entries from its" \
         " base class".red
end

.build_entry(parameter, quantity, argument, text) ⇒ String (private)

method to build the entry text when dealing with one ore more parameters

Parameters:

  • parameter (String)

    the string part containing the required paramter

  • quantity (String)

    string entry to reflect the number of parameters

  • argument (String)

    the string part containing the argument

  • text (String)

    the string part containing the description text

Returns:

  • (String)

    the complete string representing the help entry



132
133
134
# File 'lib/ruby_utils/base_help_output.rb', line 132

def self.build_entry(parameter, quantity, argument, text)
  parameter + quantity.red + argument.yellow + text
end

.get_script_nameObject (private)

method to set the name of the script project



83
84
85
86
# File 'lib/ruby_utils/base_help_output.rb', line 83

def self.get_script_name
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: get_script_name from its base class".red
end

.initialize_outputObject (private)

method to initialize the hash containing the help entries



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_utils/base_help_output.rb', line 28

def self.initialize_output
  @parameters = Hash.new()
  add_simple_text(:help, " -h, --help      ", "show help text")
  add_simple_text(:version, " -v, --version   ",
                  "prints the current version of the project")
  add_single_argument_text(:file, " -f, --file      ", " <file>",
        "; optional parameter that indicates a filepath to a readable file")
  add_single_help_entries
  add_one_argument_help_entries
  add_two_argument_help_entries
end

method to print the available configuration parameter



76
77
78
79
80
# File 'lib/ruby_utils/base_help_output.rb', line 76

def self.print_configuration_parameter
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: print_configuration_parameter from its" \
         " base class".red
end

method to print the default help text



137
138
139
140
141
142
143
144
145
# File 'lib/ruby_utils/base_help_output.rb', line 137

def self.print_help
  print_help_head
  @parameters.each_value { |value|
    puts value
  }

  print_invalid_combinations
  print_configuration_parameter
end

method to print the help text for the given parameter

Parameters:

  • parameter (Symbol)

    provided parameter

Raises:

  • (ArgumentError)

    a non existent parameter is provided



11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_utils/base_help_output.rb', line 11

def self.print_help_for(parameter)
  initialize_output if (@parameters == nil)
  if (@parameters[parameter])
    puts "#{get_script_name} help:".light_yellow + "\n#{@parameters[parameter]}"
  elsif (parameter)
    print_help
  else
    raise ArgumentError, "help entry for #{parameter} does not exist".red
  end
end

method to print the headlines of the general help entry



63
64
65
66
# File 'lib/ruby_utils/base_help_output.rb', line 63

def self.print_help_head
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: print_help_head from its base class".red
end

method to print the invalid parameter combinations



69
70
71
72
73
# File 'lib/ruby_utils/base_help_output.rb', line 69

def self.print_invalid_combinations
    fail NotImplementedError, " Error: the subclass #{self.class} needs " \
         "to implement the method: print_invalid_combinations from its" \
         " base class".red
end