Class: Keybox::Application::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/keybox/application/base.rb

Direct Known Subclasses

PasswordGenerator, PasswordSafe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/keybox/application/base.rb', line 28

def initialize(argv = [])
    # make sure we have an empty array, we could be
    # initially passed nil explicitly
    argv ||= []

    # for testing instrumentation
    @stdin  = $stdin
    @stdout = $stdout
    @stderr = $stderr

    @options        = self.default_options
    @parsed_options = self.default_options
    @parser         = self.option_parser
    @error_message  = nil

    begin
        @parser.parse!(argv)
    rescue OptionParser::ParseError => pe
        msg = ["#{@parser.program_name}: #{pe}",
                "Try `#{@parser.program_name} --help` for more information"]
        @error_message = msg.join("\n")
    end
end

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



21
22
23
# File 'lib/keybox/application/base.rb', line 21

def error_message
  @error_message
end

#optionsObject

all applications have options and an error message



19
20
21
# File 'lib/keybox/application/base.rb', line 19

def options
  @options
end

#parsed_optionsObject

Returns the value of attribute parsed_options.



20
21
22
# File 'lib/keybox/application/base.rb', line 20

def parsed_options
  @parsed_options
end

#stderrObject

Returns the value of attribute stderr.



25
26
27
# File 'lib/keybox/application/base.rb', line 25

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



26
27
28
# File 'lib/keybox/application/base.rb', line 26

def stdin
  @stdin
end

#stdoutObject

these allow for testing instrumentation



24
25
26
# File 'lib/keybox/application/base.rb', line 24

def stdout
  @stdout
end

Instance Method Details

#configuration_file_optionsObject



75
76
77
# File 'lib/keybox/application/base.rb', line 75

def configuration_file_options
    Hash.new
end

#default_optionsObject



67
68
69
70
71
72
73
# File 'lib/keybox/application/base.rb', line 67

def default_options
    options = OpenStruct.new
    options.debug           = 0
    options.show_version    = false
    options.show_help       = false
    return options
end

#error_version_helpObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/keybox/application/base.rb', line 95

def error_version_help
    if @error_message then
        @stderr.puts @error_message
        exit 1
    elsif @parsed_options.show_version then
        @stdout.puts "#{@parser.program_name}: version #{Keybox::VERSION.join(".")}"
        exit 0
    elsif @parsed_options.show_help then
        @stdout.puts @parser
        exit 0
    end
end

#merge_optionsObject

load the default options, layer on the file options and then merge in the command line options



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/keybox/application/base.rb', line 81

def merge_options
    options = default_options.marshal_dump
    configuration_file_options.each_pair do |key,value|
        options[key] = value
    end

    @parsed_options.marshal_dump.each_pair do |key,value|
        options[key] = value
    end

    @options = OpenStruct.new(options)
end

#option_parserObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/keybox/application/base.rb', line 52

def option_parser
    OptionParser.new do |op|
        op.separator ""
        op.separator "Options:"

        op.on("-h", "--help") do
            @parsed_options.show_help = true
        end

        op.on("-v", "--version", "Show version information") do
            @parsed_options.show_version = true
        end 
    end
end

#runObject



108
109
110
111
# File 'lib/keybox/application/base.rb', line 108

def run 
    error_version_help
    @stdout.puts "Keybox Base Application.  Doing nothing but output this line."
end