opt-simple 0.9.12 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3396db6ba0716fea38377347f6831c74c372324523123b0934c8356796b8b230
4
+ data.tar.gz: 60ef677dc68de3ea9459b8b6d428e2a235178187fddb4850405dd9567e3055cc
5
+ SHA512:
6
+ metadata.gz: 85f93bd1282c725161c2db2106ff7f3ca19fe1b97f86313b73b0cd000540b8e2f5832b6523aaf087b59ef977921fe7e6aba6a193a852827d8a47635fef77f972
7
+ data.tar.gz: 16a47cde44882e07c1ae688db17be9e6df212f34b3227d958510b08f7e79e47bc0346f3e0a19109c75af4aa87af4237eba77204f8e180da35c6660a5b8c8ec6e
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2011 Ethan Stryker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,6 @@
1
- == OptSimple provides a very simple interface to command line parsing.
1
+ ## OptSimple provides a very simple interface to command line parsing.
2
2
 
3
- == Description
3
+ ## Description
4
4
  Parameter specification, validity checking and argument transformations
5
5
  can be put in one place, default parameters are easily set, and an automatic
6
6
  usage statement is constructed.
@@ -25,19 +25,22 @@ The order in which the parameters are defined dictate their order on the command
25
25
 
26
26
  User defined help banners, summaries or the whole usage statement can be defined.
27
27
 
28
- == Documentation
28
+ ## Documentation
29
29
 
30
30
  See OptSimple for the API specification
31
31
 
32
- == Installation
32
+ ## Installation
33
33
 
34
34
  It is recommended to install OptSimple using RubyGems:
35
35
 
36
+ ```
36
37
  $ sudo gem install opt-simple
38
+ ```
37
39
 
38
- == Examples
40
+ ## Examples
39
41
 
40
- === One example that shows most of the behavior you might use
42
+ ### One example that shows most of the behavior you might use
43
+ ```ruby
41
44
  require 'opt_simple'
42
45
 
43
46
  defaults = {
@@ -103,7 +106,9 @@ It is recommended to install OptSimple using RubyGems:
103
106
  puts "Range: #{opts.range.first} #{opts.range.last}"
104
107
 
105
108
  puts "Things: #{opts.things.inspect}"
109
+ ```
106
110
 
111
+ ```
107
112
  Which prints out an automatic usage statement:
108
113
  Usage: opt_ex.rb [options]
109
114
 
@@ -124,9 +129,9 @@ Which prints out an automatic usage statement:
124
129
  -t, --things THINGS Some things - variable number allowed
125
130
 
126
131
  -h, --help (for this help message)
127
-
128
- === A very simple example with no error checking. Use at your own risk!
129
-
132
+ ```
133
+ ### A very simple example with no error checking. Use at your own risk!
134
+ ``` ruby
130
135
  require 'opt_simple'
131
136
 
132
137
  defaults = {
@@ -137,9 +142,9 @@ Which prints out an automatic usage statement:
137
142
 
138
143
  puts "Options"
139
144
  puts options
140
-
141
- === An example using all default behavior
142
-
145
+ ```
146
+ ### An example using all default behavior
147
+ ```ruby
143
148
  require 'opt_simple'
144
149
 
145
150
  options = OptSimple.new.parse_opts! do
@@ -151,9 +156,9 @@ Which prints out an automatic usage statement:
151
156
 
152
157
  puts "Options"
153
158
  puts options
154
-
155
- === An example that shows how to set the banner string, and add a summary.
156
-
159
+ ```
160
+ ### An example that shows how to set the banner string, and add a summary.
161
+ ```ruby
157
162
  require 'opt_simple'
158
163
 
159
164
  defaults = {
@@ -172,9 +177,9 @@ Which prints out an automatic usage statement:
172
177
 
173
178
  puts "Options"
174
179
  puts options
175
-
176
- === An example that shows that you can easily set your defaults in normal Ruby variables and provide your own help.
177
-
180
+ ```
181
+ ### An example that shows that you can easily set your defaults in normal Ruby variables and provide your own help.
182
+ ```ruby
178
183
  require 'opt_simple'
179
184
 
180
185
  in_file = nil
@@ -223,9 +228,9 @@ Which prints out an automatic usage statement:
223
228
 
224
229
  puts "ARGV"
225
230
  puts ARGV
226
-
227
- === An example showing how to register parms in multiple places before parsing the command line
228
-
231
+ ```
232
+ ### An example showing how to register parms in multiple places before parsing the command line
233
+ ```ruby
229
234
  require 'opt_simple'
230
235
 
231
236
  min = 5
@@ -241,7 +246,7 @@ Which prints out an automatic usage statement:
241
246
  option %w[-p --pattern --glob-pattern], "glob pattern","PATTERN"
242
247
  end
243
248
 
244
- os.register_opts do
249
+ os.register_opts({num: 7}) do
245
250
  option %w[-n -num --num-values],"Number of val","VAL" do |arg|
246
251
  set_opt arg.to_i
247
252
  end
@@ -263,9 +268,10 @@ Which prints out an automatic usage statement:
263
268
 
264
269
  puts "Options"
265
270
  puts options
266
-
267
- === A totally contrived example showing how to change multiple options within an option specification block
271
+ ```
272
+ ### A totally contrived example showing how to change multiple options within an option specification block
268
273
 
274
+ ```ruby
269
275
  require 'opt_simple'
270
276
 
271
277
  allowed_odds = [1,3,5,7]
@@ -298,9 +304,10 @@ Which prints out an automatic usage statement:
298
304
 
299
305
  puts "Options"
300
306
  puts opts
301
-
302
- == Questions and/or Comments
307
+ ```
308
+ ## Questions and/or Comments
303
309
 
304
- email {Ethan Stryker}[mailto:e.stryker@gmail.com]
310
+ email [Ethan Stryker](mailto:e.stryker@gmail.com])
305
311
 
306
-
312
+ ## License
313
+ MIT - see [LICENSE](LICENSE.txt)
data/lib/opt_simple.rb CHANGED
@@ -27,14 +27,15 @@ class OptSimple
27
27
 
28
28
  # default keys should should be strings,
29
29
  # args can be any list of strings, but defaults to ARGV
30
- def initialize(defaults = {},args = ARGV)
30
+ def initialize(defaults: {},args: ARGV)
31
31
  @mandatory_opts = []
32
32
  @optional_opts = []
33
33
  @parameters = []
34
34
  @param_names = {}
35
35
  @results = OptSimple::Result.new
36
36
  @longest_switch_len = 0
37
- @defaults = defaults
37
+ # set defaults to empty hash just in case user sets them to nil
38
+ @defaults = defaults || {}
38
39
  @args = args.to_a # especially for jruby
39
40
  @banner = "Usage: #{File.basename($0)} [options]"
40
41
  @summary = ""
@@ -58,10 +59,11 @@ class OptSimple
58
59
 
59
60
  # Simply register options without actually parsing them.
60
61
  # This allows registering parms in multiple places in your code.
61
- def register_opts(&block)
62
+ def register_opts(defaults: {}, &block)
62
63
  # call the block to register all the parameters and
63
64
  # their corresponding code blocks
64
65
  # We use instance_exec so that the API is cleaner.
66
+ @defaults.merge!(defaults)
65
67
  instance_exec(@results,&block)
66
68
  self
67
69
  end
@@ -146,9 +148,8 @@ class OptSimple
146
148
  end
147
149
 
148
150
  chunks.each do | pieces |
149
- if parm.block.arity > 0 and
150
- (pieces.length < parm.block.arity or
151
- pieces.any? {|p| p.start_with?('-')})
151
+ # Note that we remove the leading '-' restriction to allow for negative numbers in arguments
152
+ if parm.block.arity > 0 and (pieces.length < parm.block.arity) # or # pieces.any? {|p| p.start_with?('-')})
152
153
  raise OptSimple::ParameterUsageError.new "Not enough args following #{intersection}",self
153
154
  end
154
155
 
@@ -1,6 +1,6 @@
1
1
  module Test::Unit
2
2
  # Used to fix a minor minitest/unit incompatibility in flexmock
3
- AssertionFailedError = Class.new(StandardError)
3
+ # AssertionFailedError = Class.new(StandardError)
4
4
 
5
5
  class TestCase
6
6
 
data/test/test_arglist.rb CHANGED
@@ -5,7 +5,7 @@ require 'test_unit_extensions'
5
5
  class ArglistTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @args = %w[infile1 -v infile2 -o out.txt infile3 -- -infile4 infile5]
8
- @os = OptSimple.new({},@args)
8
+ @os = OptSimple.new(defaults: {},args: @args)
9
9
  @block = Proc.new {
10
10
  flag '-v'
11
11
  argument '-o'
data/test/test_help.rb CHANGED
@@ -8,14 +8,14 @@ class TestHelpStatement < Test::Unit::TestCase
8
8
  defaults = {
9
9
  :a => 1000
10
10
  }
11
- os = OptSimple.new(defaults)
11
+ os = OptSimple.new(defaults: defaults)
12
12
  os.add_parameter(OptSimple::Option.new '-a' )
13
13
 
14
14
  assert os.to_s =~/default.*#{defaults['a']}/
15
15
  end
16
16
 
17
17
  must "order help according to specification order" do
18
- os = OptSimple.new({},['-h'])
18
+ os = OptSimple.new(defaults: {},args: ['-h'])
19
19
  os.add_parameter(OptSimple::Option.new '-a')
20
20
  os.add_parameter(OptSimple::Flag.new '-b')
21
21
  os.add_parameter(OptSimple::Option.new '-c')
@@ -28,7 +28,7 @@ class TestHelpStatement < Test::Unit::TestCase
28
28
 
29
29
  must "allow user defined help statements" do
30
30
  my_help = "Totally irrelevant"
31
- os = OptSimple.new({},['-h'])
31
+ os = OptSimple.new(defaults: {},args: ['-h'])
32
32
  os.add_parameter(OptSimple::Option.new '-a')
33
33
  os.add_parameter(OptSimple::Flag.new '-b')
34
34
  os.help my_help
@@ -37,14 +37,14 @@ class TestHelpStatement < Test::Unit::TestCase
37
37
  end
38
38
 
39
39
  must "provide user specified summary statement in help" do
40
- os = OptSimple.new({},['-h'])
40
+ os = OptSimple.new(defaults: {},args: ['-h'])
41
41
  os.add_parameter(OptSimple::Option.new '-a')
42
42
  os.summary "My summary"
43
43
  assert os.to_s.match(Regexp.new("My summary",Regexp::MULTILINE))
44
44
  end
45
45
 
46
46
  must "add metavars to help statement" do
47
- os = OptSimple.new({},['-h'])
47
+ os = OptSimple.new(defaults: {},args: ['-h'])
48
48
  os.add_parameter(OptSimple::Option.new '-a',"some help","THING")
49
49
  assert os.to_s.match(Regexp.new("\-a.*THING.*some help",Regexp::MULTILINE))
50
50
  end
data/test/test_usage.rb CHANGED
@@ -9,7 +9,7 @@ class TestHelpStatement < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  must "throw parameter missing exception when the argument method is used but the parm isn't specified" do
12
- os = OptSimple.new({})
12
+ os = OptSimple.new(defaults: {})
13
13
  assert_raise(OptSimple::MissingArgument) do
14
14
  os.parse_opts! do
15
15
  argument '-a'
@@ -18,7 +18,7 @@ class TestHelpStatement < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  must "throw missing parameter usage error when a parameter isn't followed by enough arguments" do
21
- os = OptSimple.new({},['-a'])
21
+ os = OptSimple.new(defaults: {},args: ['-a'])
22
22
  assert_raise(OptSimple::ParameterUsageError) do
23
23
  os.parse_opts! do
24
24
  argument '-a' do |arg|
@@ -27,7 +27,7 @@ class TestHelpStatement < Test::Unit::TestCase
27
27
  end
28
28
  end
29
29
 
30
- os = OptSimple.new({},%w[--range 5])
30
+ os = OptSimple.new(defaults: {},args: %w[--range 5])
31
31
  assert_raise(OptSimple::ParameterUsageError) do
32
32
  os.parse_opts! do
33
33
  argument '--range' do |min,max|
@@ -38,12 +38,12 @@ class TestHelpStatement < Test::Unit::TestCase
38
38
  end
39
39
 
40
40
  must "raise error when unknown option is given" do
41
- os = OptSimple.new({},['-not-specified'])
41
+ os = OptSimple.new(defaults: {},args: ['-not-specified'])
42
42
  assert_raise(OptSimple::InvalidOption) { os.parse_opts! { option %w[-a --awesome] } }
43
43
  end
44
44
 
45
45
  must "handle arguments with equals and commas" do
46
- os = OptSimple.new({},['-a=2','--foo=4,5'])
46
+ os = OptSimple.new(defaults: {},args: ['-a=2','--foo=4,5'])
47
47
  x = nil
48
48
  y = nil
49
49
  opts= os.parse_opts! do
@@ -62,7 +62,7 @@ class TestHelpStatement < Test::Unit::TestCase
62
62
  end
63
63
 
64
64
  must "accumulate lists of args when asked" do
65
- os = OptSimple.new({},%w[-i foo.bar --infile bar.in])
65
+ os = OptSimple.new(defaults: {},args: %w[-i foo.bar --infile bar.in])
66
66
  o = os.parse_opts! do
67
67
  option %w[-i --infile], "Infile, multiple allowed", "FILE" do | arg |
68
68
  accumulate_opt arg
@@ -74,7 +74,7 @@ class TestHelpStatement < Test::Unit::TestCase
74
74
  end
75
75
 
76
76
  must "accumulate numbers of flags set when asked" do
77
- os = OptSimple.new({},%w[-v -v --verbose -v])
77
+ os = OptSimple.new(defaults: {},args: %w[-v -v --verbose -v])
78
78
  o = os.parse_opts! do
79
79
  flag %w[-v --verbose],"Verbosity. the more you set, the more we give" do
80
80
  accumulate_opt
@@ -85,7 +85,7 @@ class TestHelpStatement < Test::Unit::TestCase
85
85
  end
86
86
 
87
87
  must "set last arg when duplicated when accumulate opt isn't used" do
88
- os = OptSimple.new({},%w[-i foo.bar --infile bar.in -i baz])
88
+ os = OptSimple.new(defaults: {},args: %w[-i foo.bar --infile bar.in -i baz])
89
89
  o = os.parse_opts! do
90
90
  option %w[-i --infile], "Infile, multiple allowed", "FILE" do | arg |
91
91
  set_opt arg
@@ -97,7 +97,7 @@ class TestHelpStatement < Test::Unit::TestCase
97
97
  end
98
98
 
99
99
  must "allow parameters to be registered in multiple spots" do
100
- os = OptSimple.new({},%w[-i foo.bar --outfile bar.out])
100
+ os = OptSimple.new(defaults: {},args: %w[-i foo.bar --outfile bar.out])
101
101
  os.register_opts do
102
102
  option %w[-i --infile], "Infile", "FILE" do | arg |
103
103
  set_opt arg
@@ -115,6 +115,27 @@ class TestHelpStatement < Test::Unit::TestCase
115
115
  assert_equal o['o'],'bar.out'
116
116
  end
117
117
 
118
+ must "allow defaults when registering opts in multiple spots" do
119
+ os = OptSimple.new(defaults: {outfile: 'old-default'},args: %w[-i foo.bar])
120
+
121
+ os.register_opts do
122
+ option %w[-i --infile], "Infile", "FILE" do | arg |
123
+ set_opt arg
124
+ end
125
+ end
126
+
127
+ os.register_opts(defaults: {outfile: 'new-default'}) do
128
+ option %w[-o --outfile], "outfile", "FILE" do | arg |
129
+ set_opt arg
130
+ end
131
+ end
132
+ o = os.parse_opts!
133
+
134
+ assert_equal o['i'],'foo.bar'
135
+ assert_equal o['o'],'new-default'
136
+
137
+ end
138
+
118
139
  must "set flags to false by default" do
119
140
  os = OptSimple.new
120
141
  o = os.parse_opts! do
@@ -125,7 +146,7 @@ class TestHelpStatement < Test::Unit::TestCase
125
146
  end
126
147
 
127
148
  must "add undersore versions to all switch names that have a dash" do
128
- os = OptSimple.new({},%w[--some-stuff foo])
149
+ os = OptSimple.new(defaults: {},args: %w[--some-stuff foo])
129
150
  o = os.parse_opts! do
130
151
  option %w[-s --some-stuff]
131
152
  end
@@ -135,7 +156,7 @@ class TestHelpStatement < Test::Unit::TestCase
135
156
  end
136
157
 
137
158
  must "overwrite defaults when accumulate opt is used and option is seen on the CL" do
138
- os = OptSimple.new({:some_stuff => ['bar']},%w[--some-stuff foo --some-stuff bar])
159
+ os = OptSimple.new(defaults: {:some_stuff => ['bar']},args: %w[--some-stuff foo --some-stuff bar])
139
160
  o = os.parse_opts! do
140
161
  option %w[-s --some-stuff] do | arg |
141
162
  accumulate_opt arg
@@ -146,7 +167,7 @@ class TestHelpStatement < Test::Unit::TestCase
146
167
  end
147
168
 
148
169
  must "successfully pull out two options following a switch when desired" do
149
- o = OptSimple.new(nil,%w[--range 4 9]).parse_opts! do
170
+ o = OptSimple.new(defaults: nil,args: %w[--range 4 9]).parse_opts! do
150
171
  argument "--range","min,max" do | arg1, arg2 |
151
172
  set_opt [arg1.to_i,arg2.to_i]
152
173
  end
@@ -155,7 +176,17 @@ class TestHelpStatement < Test::Unit::TestCase
155
176
  assert_equal o.range.first, 4
156
177
  assert_equal o.range.last, 9
157
178
  end
158
-
179
+
180
+ must "successfully pull out two options with negative numbers following a switch when desired" do
181
+ o = OptSimple.new(defaults: nil,args: %w[--range -4 9]).parse_opts! do
182
+ argument "--range","min,max" do | arg1, arg2 |
183
+ set_opt [arg1.to_i,arg2.to_i]
184
+ end
185
+ end
186
+
187
+ assert_equal o.range.first, -4
188
+ assert_equal o.range.last, 9
189
+ end
159
190
 
160
191
  must "allow for a variable number of options following a switch when a splat is used" do
161
192
 
@@ -166,7 +197,7 @@ class TestHelpStatement < Test::Unit::TestCase
166
197
  arg_lists.each do |args |
167
198
  # Gotta account for '--whatever and --things switch in there'
168
199
  expected_len = args.length - 2
169
- os = OptSimple.new(nil,args).parse_opts! do
200
+ os = OptSimple.new(defaults: nil,args: args).parse_opts! do
170
201
  argument "--things" do | *arg |
171
202
  arg.each do |a|
172
203
  accumulate_opt a
@@ -184,7 +215,7 @@ class TestHelpStatement < Test::Unit::TestCase
184
215
  # Hmm ... just want to be flexible here. perhaps not necessary
185
216
  # Note you still gotta initialize things to an empty list to make it not nil
186
217
  must "allow for no options following an option defined with a splat" do
187
- os = OptSimple.new({:things => [] },%w[--whatever --things]).parse_opts! do
218
+ os = OptSimple.new(defaults: {:things => [] },args: %w[--whatever --things]).parse_opts! do
188
219
  option "--things" do | *arg |
189
220
  arg.each do |a|
190
221
  accumulate_opt a
@@ -196,4 +227,3 @@ class TestHelpStatement < Test::Unit::TestCase
196
227
  assert os.things.empty?
197
228
  end
198
229
  end
199
-
metadata CHANGED
@@ -1,66 +1,52 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: opt-simple
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.9.12
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Ethan Stryker
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
-
13
- date: 2011-12-27 00:00:00 +00:00
14
- default_executable:
11
+ date: 2024-01-19 00:00:00.000000000 Z
15
12
  dependencies: []
16
-
17
- description: " Parameter specification, validity checking and argument transformations \n can be put in one place, default parameters are easily set, and an \n automatic usage statement is constructed.\n"
13
+ description: " Parameter specification, validity checking and argument transformations
14
+ \n can be put in one place, default parameters are easily set, and an \n automatic
15
+ usage statement is constructed.\n"
18
16
  email: e.stryker@gmail.com
19
17
  executables: []
20
-
21
18
  extensions: []
22
-
23
19
  extra_rdoc_files: []
24
-
25
- files:
26
- - lib/test_unit_extensions.rb
20
+ files:
21
+ - LICENSE.txt
22
+ - README.md
23
+ - extensions/test_unit_extensions.rb
27
24
  - lib/opt_simple.rb
28
- - lib/tmp.rb
29
- - test/test_help.rb
30
- - test/test_usage.rb
25
+ - lib/test_unit_extensions.rb
31
26
  - test/test_arglist.rb
32
- - test/test_internals.rb
27
+ - test/test_help.rb
33
28
  - test/test_return.rb
34
- - extensions/test_unit_extensions.rb
35
- - README
36
- - GPLv2-LICENSE
37
- has_rdoc: true
29
+ - test/test_usage.rb
38
30
  homepage: http://opt-simple.rubyforge.org/
39
31
  licenses: []
40
-
32
+ metadata: {}
41
33
  post_install_message:
42
34
  rdoc_options: []
43
-
44
- require_paths:
35
+ require_paths:
45
36
  - lib
46
- required_ruby_version: !ruby/object:Gem::Requirement
47
- none: false
48
- requirements:
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
49
39
  - - ">="
50
- - !ruby/object:Gem::Version
51
- version: "0"
52
- required_rubygems_version: !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
55
44
  - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
58
47
  requirements: []
59
-
60
- rubyforge_project: opt-simple
61
- rubygems_version: 1.6.2
48
+ rubygems_version: 3.3.7
62
49
  signing_key:
63
- specification_version: 3
50
+ specification_version: 4
64
51
  summary: A simple and elegant command line option parser.
65
52
  test_files: []
66
-
data/GPLv2-LICENSE DELETED
@@ -1,18 +0,0 @@
1
- OptSimple is a utility for parsing command line arguments.
2
- Copyright (C) 2011 Ethan Stryker
3
-
4
- This program is free software; you can redistribute it and/or
5
- modify it under the terms of the GNU General Public License
6
- as published by the Free Software Foundation; either version 2
7
- of the License, or (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details.
13
-
14
- You should have received a copy of the GNU General Public License
15
- along with this program; if not, write to the Free Software
16
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
-
18
- Questions/Comments on OptSimple can be mailed to Ethan Stryker, e.stryker@gmail.com
data/lib/tmp.rb DELETED
@@ -1,63 +0,0 @@
1
- # in parse_opts!
2
- # now actually parse the args, and call all the stored up blocks from the options
3
- @parameters.each do | parm |
4
- intersection = @args & parm.switches
5
-
6
- unless intersection.empty?
7
- mandatory_check.delete(parm.switches)
8
-
9
- arg_locations = []
10
- @args.each_with_index {|arg,i| arg_locations << i if intersection.include?(arg) }
11
- # ...
12
- def flag(switches,help="",&block)
13
- parm = Flag.new(switches,help,&block)
14
- parm.set_opt false
15
- add_parameter parm
16
-
17
- # add it to the results to make checking flags nicer.
18
- # use the first name b/c they are all aliased anyways.
19
- @results.merge! parm.param_options
20
- end
21
-
22
- def option(switches,help="",metavar="ARG",&block)
23
- parm = Option.new(switches,help,metavar,&block)
24
-
25
- # set the defaults, if there are any in the defaults hash
26
- intersection = parm.names & @defaults.keys
27
- if intersection.length == 1
28
- parm.set_opt @defaults[intersection.first]
29
- @results.merge! parm.param_options
30
- elsif intersection.length > 1
31
- raise OptSimple::Error "Clashes in the defaults for #{parm.switches}"
32
- end
33
-
34
- add_parameter(parm)
35
- end
36
-
37
- class Option < Parameter
38
- attr_reader = :metavar
39
- def initialize(switches,help="",metavar="ARG",&block)
40
- super(switches,help,&block)
41
- @metavar = metavar
42
- unless block_given?
43
- @block = Proc.new {|arg| @param_options[names.first] = arg}
44
- end
45
- @first_call = true # use this so we can accumulate non-defaults
46
- end
47
- def switch_len #:nodoc:
48
- metavar_space = @metavar.empty? ? 0 : @metavar.length + 1
49
- super + metavar_space
50
- end
51
- def switch_str #:nodoc:
52
- super + " #{@metavar}"
53
- end
54
- # append val to the parameter list
55
- def accumulate_opt(val)
56
- if @first_call
57
- @param_options[names.first] = [val].flatten
58
- else
59
- @param_options[names.first] << val
60
- end
61
- @first_call = false
62
- end
63
- end
@@ -1,21 +0,0 @@
1
- require 'opt_simple'
2
- require 'test/unit'
3
- require 'test_unit_extensions'
4
-
5
- class InternalsTest < Test::Unit::TestCase
6
- def setup
7
- @args = %w[infile1 -v infile2 -o out.txt infile3 -- -infile4 infile5]
8
- @os = OptSimple.new({},@args)
9
- @block = Proc.new {
10
- flag '-v'
11
- argument '-o'
12
- }
13
- end
14
-
15
- must "not freeze strings" do
16
- options = @os.parse_opts! &@block
17
- assert_nothing_raised do
18
- options[:o].gsub!('.txt','.out')
19
- end
20
- end
21
- end