Module: BatchKit::Arguments

Includes:
ArgParser::DSL
Included in:
Job
Defined in:
lib/batch-kit/arguments.rb

Overview

Defines a module for adding argument parsing to a job via the arg-parser gem, and displaying help via the color-console gem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject (readonly)

Adds an arguments accessor that returns the results from parsing the command-line.



16
17
18
# File 'lib/batch-kit/arguments.rb', line 16

def arguments
  @arguments
end

Class Method Details

.included(base) ⇒ Object

Add class methods when module is included



51
52
53
# File 'lib/batch-kit/arguments.rb', line 51

def self.included(base)
    base.extend(ClassMethods)
end

Instance Method Details

#parse_arguments(args = ARGV, show_usage_on_error = true) ⇒ Object

Parse command-line arguments



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/batch-kit/arguments.rb', line 20

def parse_arguments(args = ARGV, show_usage_on_error = true)
    if defined?(BatchKit::Job) && self.is_a?(BatchKit::Job)
        args_def.title ||= self.job.name.titleize
        args_def.purpose ||= self.job.description
    elsif defined?(BatchKit::Sequence) && self.is_a?(BatchKit::Sequence)
        args_def.title ||= self.sequence.name.titleize
        args_def.purpose ||= self.sequence.description
    end
    arg_parser = ArgParser::Parser.new(args_def)
    @arguments = arg_parser.parse(args)
    if @arguments == false
        if arg_parser.show_help?
            arg_parser.definition.show_help(nil, Console.width || 80).each do |line|
                Console.puts line, :cyan
            end
            exit
        else
            arg_parser.errors.each{ |error| Console.puts error, :red }
            if show_usage_on_error
                arg_parser.definition.show_usage(nil, Console.width || 80).each do |line|
                    Console.puts line, :yellow
                end
            end
            exit(99)
        end
    end
    @arguments
end