opt 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NDY2ZTNiM2Q0MjhkYTMyMWU3MDA1MzFjY2M2YjhmNTIzNWY3OGQ4OA==
5
- data.tar.gz: !binary |-
6
- YzVmYjdmN2MwMDUxMGRmZTIyNzU4NGExOWIxM2E3NzI0ZGJjZGEwMw==
2
+ SHA1:
3
+ metadata.gz: 427adb3a36001c85e6d8dc0a5b5dd64086c93465
4
+ data.tar.gz: 125c5a7574ee58a627f94067ce0e11fa1920a808
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDUzY2FhOWQ3ZTkzODM0ZDcyNGJlNDQyMjZkZjBmYTNlN2Y5NWU1MjA4Nzky
10
- ODgzM2Y0N2UxMWNlY2YzMjBlNTM0YjQ4ZmUzODYzMmY0NjFjYzM5MDEzYjQ3
11
- Y2M1MDBhNmFhNjU1YjViNTgzODgxY2I4YjlhM2M4MTgwYjE4MmM=
12
- data.tar.gz: !binary |-
13
- ZTA3ZjNkMzE1NTg4NGQxMGQ0ZGQ3MzA0YzkxMjAzODIxZjRhY2Q1MzE2Yjk0
14
- MTIwOTNjZWIxODY1Y2FhYmZmZjg2MDBhNzI3NGYwM2IyZDE4NmRlMGQyYjlj
15
- Mzc4NTU3ODJiNzEwMmUwMmY3ZTFiNmNjZmVmZDRlZjA2YWIzNmQ=
6
+ metadata.gz: 6964a05a1355768605a19576c307af86704e09cbe9e398af0128ee3bc85e7922f537a0c6b8fc17c0673938b62a9d59ead0ca87244896068387632cdc1200e995
7
+ data.tar.gz: 9b40f785232014d8d1a1e99367a572096dde47c87de536b6c3ec14ececbac592afc09a6a7ab1576181cc686d423a70bea389710152e5aff3f141b99fe36d2a07
data/lib/opt.rb CHANGED
@@ -11,8 +11,9 @@ module Opt
11
11
  end
12
12
 
13
13
  module Constants
14
- Inf = 1.0 / 0.0
15
- Infinity = 1.0 / 0.0
14
+ Inf = 1.0 / 0
15
+ Infinity = 1.0 / 0
16
+ Identity = ->(x){ x }
16
17
  end
17
18
 
18
19
  include Constants
data/lib/opt/command.rb CHANGED
@@ -74,8 +74,8 @@ module Opt
74
74
  # @api public
75
75
  # @see Option.new
76
76
  #
77
- def option(definition = nil, opts = {})
78
- option = Option.new(definition, opts)
77
+ def option(definition = nil, opts = {}, &block)
78
+ option = Option.new(definition, opts, &block)
79
79
 
80
80
  if commands.any?
81
81
  raise ArgumentError.new \
@@ -160,7 +160,7 @@ module Opt
160
160
  result = Result.new
161
161
  result.merge! defaults
162
162
 
163
- parse_argv! parse_tokens(argv), result
163
+ parse_argv! parse_tokens(argv.dup), result
164
164
 
165
165
  result
166
166
  end
data/lib/opt/option.rb CHANGED
@@ -47,11 +47,16 @@ module Opt
47
47
  #
48
48
  attr_reader :nargs
49
49
 
50
- def initialize(definition, options = {})
50
+ # Block for processing arguments.
51
+ #
52
+ attr_reader :block
53
+
54
+ def initialize(definition, options = {}, &block)
51
55
  @options = options
52
56
  @default = options.fetch(:default, nil)
53
57
  @value = options.fetch(:value, true)
54
58
  @nargs = Option.parse_nargs options.fetch(:nargs, 0)
59
+ @block = block || Opt::Identity
55
60
 
56
61
  if definition.to_s =~ /\A[[:word:]]+\z/
57
62
  @switches = Set.new
@@ -124,9 +129,9 @@ module Opt
124
129
 
125
130
  if nargs.include?(args.size)
126
131
  if nargs == (1..1)
127
- result[name] = args.first
132
+ result[name] = block.call args.first
128
133
  else
129
- result[name] = args
134
+ result[name] = block.call args
130
135
  end
131
136
  else
132
137
  # raise Opt::MissingArgument
data/lib/opt/version.rb CHANGED
@@ -2,7 +2,7 @@ module Opt
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- PATCH = 0
5
+ PATCH = 1
6
6
  STAGE = nil
7
7
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
8
8
 
@@ -33,9 +33,10 @@ def t(type, value)
33
33
  end
34
34
 
35
35
  describe Opt::Option do
36
- let(:option) { described_class.new(defin, {name: :test}.merge(opts)) }
36
+ let(:option) { described_class.new(defin, {name: :test}.merge(opts), &block) }
37
37
  let(:defin) { '' }
38
38
  let(:opts) { {} }
39
+ let(:block) { Opt::Identity }
39
40
  subject { option }
40
41
 
41
42
  describe '.parse_nargs' do
data/spec/opt_spec.rb CHANGED
@@ -57,6 +57,15 @@ describe Opt do
57
57
  expect(result.level).to eq %w(5 6)
58
58
  end
59
59
 
60
+ it 'should parse options with argument & block' do
61
+ opt.option '--level, -l', nargs: 2..3 do |levels|
62
+ levels.map(&method(:Integer))
63
+ end
64
+
65
+ result = opt.parse %w(--level 5 6)
66
+ expect(result.level).to eq [5, 6]
67
+ end
68
+
60
69
  it 'should parse flags' do
61
70
  pending
62
71
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-15 00:00:00.000000000 Z
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.5'
27
27
  description: An option parsing library. Optional.
@@ -58,12 +58,12 @@ require_paths:
58
58
  - lib
59
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - ! '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  required_rubygems_version: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  requirements: []