Class: Keybox::Application::Base
- Inherits:
-
Object
- Object
- Keybox::Application::Base
- Defined in:
- lib/keybox/application/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#error_message ⇒ Object
Returns the value of attribute error_message.
-
#highline ⇒ Object
readonly
Returns the value of attribute highline.
-
#options ⇒ Object
all applications have options and an error message.
-
#parsed_options ⇒ Object
Returns the value of attribute parsed_options.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
these allow for testing instrumentation.
Class Method Summary collapse
-
.home_directory ⇒ Object
thank you Jamis - from Capistrano.
Instance Method Summary collapse
- #configuration_file_options ⇒ Object
- #default_options ⇒ Object
- #error_version_help ⇒ Object
-
#initialize(argv = []) ⇒ Base
constructor
A new instance of Base.
-
#merge_options ⇒ Object
load the default options, layer on the file options and then merge in the command line options.
- #option_parser ⇒ Object
- #run ⇒ Object
-
#set_io(stdin = $stdin, stdout = $stdout, stderr = $stderr) ⇒ Object
Allow the IO to be reset.
Constructor Details
#initialize(argv = []) ⇒ Base
Returns a new instance of Base.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/keybox/application/base.rb', line 39 def initialize(argv = []) # make sure we have an empty array, we could be # initially passed nil explicitly argv ||= [] # setup default io streams set_io @options = self. @parsed_options = OpenStruct.new @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_message ⇒ Object
Returns the value of attribute error_message.
22 23 24 |
# File 'lib/keybox/application/base.rb', line 22 def @error_message end |
#highline ⇒ Object (readonly)
Returns the value of attribute highline.
28 29 30 |
# File 'lib/keybox/application/base.rb', line 28 def highline @highline end |
#options ⇒ Object
all applications have options and an error message
20 21 22 |
# File 'lib/keybox/application/base.rb', line 20 def @options end |
#parsed_options ⇒ Object
Returns the value of attribute parsed_options.
21 22 23 |
# File 'lib/keybox/application/base.rb', line 21 def @parsed_options end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
26 27 28 |
# File 'lib/keybox/application/base.rb', line 26 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
27 28 29 |
# File 'lib/keybox/application/base.rb', line 27 def stdin @stdin end |
#stdout ⇒ Object (readonly)
these allow for testing instrumentation
25 26 27 |
# File 'lib/keybox/application/base.rb', line 25 def stdout @stdout end |
Class Method Details
.home_directory ⇒ Object
thank you Jamis - from Capistrano
32 33 34 35 36 |
# File 'lib/keybox/application/base.rb', line 32 def home_directory # :nodoc: ENV["HOME"] || (ENV["HOMEPATH"] && "#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}") || "/" end |
Instance Method Details
#configuration_file_options ⇒ Object
101 102 103 |
# File 'lib/keybox/application/base.rb', line 101 def Hash.new end |
#default_options ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/keybox/application/base.rb', line 91 def if not @default_options then @default_options = OpenStruct.new @default_options.debug = 0 @default_options.show_version = false @default_options.show_help = false end return @default_options end |
#error_version_help ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/keybox/application/base.rb', line 121 def error_version_help if @error_message then @stderr.puts @error_message exit 1 elsif @parsed_options.show_version then @highline.say "#{@parser.program_name}: version #{Keybox::VERSION}" exit 0 elsif @parsed_options.show_help then @highline.say @parser.to_s exit 0 end end |
#merge_options ⇒ Object
load the default options, layer on the file options and then merge in the command line options
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/keybox/application/base.rb', line 107 def = .marshal_dump self..each_pair do |key,value| [key] = value end @parsed_options.marshal_dump.each_pair do |key,value| [key] = value end @options = OpenStruct.new() end |
#option_parser ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/keybox/application/base.rb', line 76 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 |
#run ⇒ Object
134 135 136 137 |
# File 'lib/keybox/application/base.rb', line 134 def run error_version_help @highline.say "Keybox Base Application. Doing nothing but output this line." end |
#set_io(stdin = $stdin, stdout = $stdout, stderr = $stderr) ⇒ Object
Allow the IO to be reset. This is generally just for testing instrumentation, but it may be useful for something else too.
66 67 68 69 70 71 72 73 74 |
# File 'lib/keybox/application/base.rb', line 66 def set_io(stdin = $stdin,stdout = $stdout,stderr = $stderr) # for testing instrumentation @stdin = stdin @stdout = stdout @stderr = stderr # Instance of HighLine for the colorization of output @highline = ::HighLine.new(@stdin,@stdout) end |