optopus 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +5 -3
  2. data/lib/optopus.rb +42 -9
  3. metadata +5 -7
data/README CHANGED
@@ -14,6 +14,9 @@ gem install optopus
14
14
  require 'optopus'
15
15
 
16
16
  opts = optopus do
17
+ desc 'program information'
18
+ option :info, '-I'
19
+
17
20
  desc 'print lots of debugging information'
18
21
  option :debug, '-d', '--debug'
19
22
 
@@ -33,7 +36,7 @@ gem install optopus
33
36
  option :timestamp, '-T', '--timestamp TIME', :type => Time
34
37
 
35
38
  # read yaml config file and overwrite options
36
- file '-c', '--config-file FILE'
39
+ config_file '-c', '--config-file FILE'
37
40
 
38
41
  after do
39
42
  # postprocessing
@@ -41,8 +44,7 @@ gem install optopus
41
44
  end
42
45
 
43
46
  error do |e|
44
- $stderr.puts e.message
45
- exit 1
47
+ abort(e.message)
46
48
  end
47
49
  end
48
50
 
data/lib/optopus.rb CHANGED
@@ -15,6 +15,14 @@ module Optopus
15
15
  @opts = opts
16
16
  end
17
17
 
18
+ def banner=(v) ; @opts.banner = v ; end
19
+ def program_name=(v) ; @opts.program_name = v ; end
20
+ def summary_width=(v) ; @opts.psummary_width = v ; end
21
+ def summary_indent=(v) ; @opts.summary_indent = v ; end
22
+ def default_argv=(v) ; @opts.default_argv = v ; end
23
+ def version=(v) ; @opts.version = v ; end
24
+ def release=(v) ; @opts.release = v ; end
25
+
18
26
  def desc(str)
19
27
  @desc = str
20
28
  end
@@ -24,7 +32,7 @@ module Optopus
24
32
  @desc = nil
25
33
  end
26
34
 
27
- def file(args_hd, *args_tl)
35
+ def config_file(args_hd, *args_tl)
28
36
  @desc ||= 'reading config file'
29
37
  @opts.add_file([args_hd] + args_tl, @desc)
30
38
  @desc = nil
@@ -85,15 +93,25 @@ module Optopus
85
93
 
86
94
  class Options
87
95
  def initialize
96
+ @parser = OptionParser.new
88
97
  @opts_args = []
89
98
  end
90
99
 
100
+ def banner=(v) ; @parser.banner = v ; end
101
+ def program_name=(v) ; @parser.program_name = v ; end
102
+ def summary_width=(v) ; @parser.psummary_width = v ; end
103
+ def summary_indent=(v) ; @parser.summary_indent = v ; end
104
+ def default_argv=(v) ; @parser.default_argv = v ; end
105
+ def version=(v) ; @parser.version = v ; end
106
+ def release=(v) ; @parser.release = v ; end
107
+
91
108
  def add(name, args, desc, block)
92
109
  args, defval = fix_args(args, desc)
93
110
  @opts_args << [name.to_sym, args, defval, block]
94
111
  end
95
112
 
96
113
  def add_file(args, desc)
114
+ raise 'config file option is defined' if @file_args
97
115
  args, defval = fix_args(args, desc)
98
116
  @file_args = args
99
117
  end
@@ -107,15 +125,16 @@ module Optopus
107
125
  end
108
126
 
109
127
  def parse!
110
- parser = OptionParser.new
111
128
  options = {}
129
+ has_arg_v = false
112
130
  has_arg_h = false
113
131
 
114
132
  @opts_args.each do |name, args, defval, block|
115
133
  options[name] = defval
134
+ has_arg_v = (args.first == '-v')
116
135
  has_arg_h = (args.first == '-h')
117
136
 
118
- parser.on(*args) do |*v|
137
+ @parser.on(*args) do |*v|
119
138
  value = v.first || true
120
139
  options[name] = value
121
140
  CheckerContext.evaluate(v, {:value => value}, &block) if block
@@ -123,11 +142,17 @@ module Optopus
123
142
  end
124
143
 
125
144
  if @file_args
126
- parser.on(*@file_args) do |v|
145
+ @parser.on(*@file_args) do |v|
127
146
  config = YAML.load_file(v)
128
147
 
129
148
  @opts_args.each do |name, args, defval, block|
130
- value = config[name] || config[name.to_s]
149
+ if args[1].kind_of?(String) and args[1] =~ /-+([^\s=]+)/
150
+ key = $1
151
+ else
152
+ key = name.to_s
153
+ end
154
+
155
+ value = config[key] || config[name.to_s]
131
156
 
132
157
  next unless value
133
158
 
@@ -136,7 +161,7 @@ module Optopus
136
161
  pat, conv = OptionParser::DefaultList.atype[type]
137
162
 
138
163
  if pat and pat !~ value
139
- raise OptionParser::InvalidArgument.new("(#{name}: #{value})")
164
+ raise OptionParser::InvalidArgument.new(v, "(#{name}: #{value})")
140
165
  end
141
166
 
142
167
  value = conv.call(value) if conv
@@ -146,14 +171,22 @@ module Optopus
146
171
  end
147
172
  end
148
173
 
174
+ unless has_arg_v
175
+ @parser.on_tail('-v', '--version', 'show version') do
176
+ v = @parser.ver or abort("#{@parser.program_name}: version unknown")
177
+ puts v
178
+ exit
179
+ end
180
+ end
181
+
149
182
  unless has_arg_h
150
- parser.on_tail('-h', '--help', 'Show this message') do
151
- puts parser.help
183
+ @parser.on_tail('-h', '--help', 'show this message') do
184
+ puts @parser.help
152
185
  exit
153
186
  end
154
187
  end
155
188
 
156
- parser.parse!(ARGV)
189
+ @parser.parse!
157
190
  CheckerContext.evaluate([], {:options => options},&@on_after) if @on_after
158
191
 
159
192
  return options
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optopus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-31 00:00:00 +09:00
19
- default_executable:
18
+ date: 2011-05-31 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description:
@@ -30,7 +29,6 @@ extra_rdoc_files: []
30
29
  files:
31
30
  - README
32
31
  - lib/optopus.rb
33
- has_rdoc: true
34
32
  homepage: https://bitbucket.org/winebarrel/optopus
35
33
  licenses: []
36
34
 
@@ -60,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
58
  requirements: []
61
59
 
62
60
  rubyforge_project:
63
- rubygems_version: 1.4.2
61
+ rubygems_version: 1.7.2
64
62
  signing_key:
65
63
  specification_version: 3
66
64
  summary: Enhanced option parser.