base_app 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/base_app.rb +64 -0
  2. metadata +58 -0
data/lib/base_app.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'optparse'
3
+
4
+ class BaseApp
5
+ attr_accessor :status
6
+
7
+ def initialize
8
+ @status = 0
9
+ @options = {}
10
+ @required_opts = []
11
+ end
12
+
13
+ def command_line_arguments
14
+ [['q','quiet','Be less verbose']]
15
+ end
16
+
17
+ def construct_opts
18
+ opts = OptionParser.new do |opts|
19
+ command_line_arguments.each do |argspec|
20
+ short, long, description, required = argspec
21
+ param_name = long.gsub '=', ''
22
+ @required_opts << param_name if required
23
+ create_setter(long)
24
+ opts.on("-#{short}", "--#{long}", description) do |val|
25
+ @options[param_name] = val
26
+ setter = param_name.gsub("-", "_") + "="
27
+ self.send(setter,val)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def create_setter(name)
34
+ name = name.gsub("-", "_").gsub("=", "")
35
+ self.class.send(:attr_accessor, name) unless self.respond_to?(name)
36
+ end
37
+
38
+ def parse_command_line_options
39
+ opts = construct_opts
40
+ opts.parse!
41
+ @required_opts.each do |name|
42
+ unless @options[name]
43
+ raise "Error, required argument '#{name}' must be supplied."
44
+ end
45
+ end
46
+ end
47
+
48
+ def self.main
49
+ app = self.new
50
+ app.parse_command_line_options
51
+ max_width = app.command_line_arguments.map {|x| x[1].length}.max
52
+ unless app.quiet
53
+ puts "#$0 Parameters:"
54
+ app.command_line_arguments.each do |opt_spec|
55
+ short, arg, descr = opt_spec
56
+ option = arg.gsub(/=$/,'').gsub('-','_')
57
+ printf( " %*s : %s\n", max_width, arg.gsub('=',''), app.send(option.to_sym))
58
+ end
59
+ puts ""
60
+ end
61
+ app.run
62
+ exit app.status
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base_app
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Burton
8
+ - Trotter Cashion
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-01-11 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: |
18
+ Simplified command line applications with a base class handling options parsing and other life cycle management.
19
+
20
+ email: kyle.burton@gmail.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files: []
26
+
27
+ files:
28
+ - lib/base_app.rb
29
+ has_rdoc: true
30
+ homepage: http://asymmetrical-view.com
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Base class for command line applications.
57
+ test_files: []
58
+