yawpa 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -17
- data/.rspec +1 -0
- data/Gemfile.lock +46 -0
- data/README.md +105 -49
- data/Rakefile.rb +18 -0
- data/lib/yawpa/version.rb +1 -1
- data/lib/yawpa.rb +67 -38
- data/spec/spec_helper.rb +5 -3
- data/spec/yawpa_spec.rb +66 -49
- data/yawpa.gemspec +6 -1
- metadata +98 -6
- checksums.yaml +0 -15
- data/Rakefile +0 -13
data/.gitignore
CHANGED
@@ -1,17 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
1
|
+
/.yardoc/
|
2
|
+
/coverage/
|
3
|
+
/doc/
|
4
|
+
/pkg/
|
5
|
+
/rdoc/
|
data/.rspec
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yawpa (1.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
docile (1.1.5)
|
11
|
+
json (1.8.1)
|
12
|
+
multi_json (1.10.1)
|
13
|
+
rake (10.3.2)
|
14
|
+
rdoc (4.1.1)
|
15
|
+
json (~> 1.4)
|
16
|
+
redcarpet (3.1.2)
|
17
|
+
rspec (3.0.0)
|
18
|
+
rspec-core (~> 3.0.0)
|
19
|
+
rspec-expectations (~> 3.0.0)
|
20
|
+
rspec-mocks (~> 3.0.0)
|
21
|
+
rspec-core (3.0.2)
|
22
|
+
rspec-support (~> 3.0.0)
|
23
|
+
rspec-expectations (3.0.2)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.0.0)
|
26
|
+
rspec-mocks (3.0.2)
|
27
|
+
rspec-support (~> 3.0.0)
|
28
|
+
rspec-support (3.0.2)
|
29
|
+
simplecov (0.8.2)
|
30
|
+
docile (~> 1.1.0)
|
31
|
+
multi_json
|
32
|
+
simplecov-html (~> 0.8.0)
|
33
|
+
simplecov-html (0.8.0)
|
34
|
+
yard (0.8.7.4)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
rake
|
41
|
+
rdoc
|
42
|
+
redcarpet
|
43
|
+
rspec
|
44
|
+
simplecov
|
45
|
+
yard
|
46
|
+
yawpa!
|
data/README.md
CHANGED
@@ -2,64 +2,120 @@
|
|
2
2
|
|
3
3
|
Yet Another Way to Parse Arguments is an argument-parsing library for Ruby.
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/yawpa.png)](http://badge.fury.io/rb/yawpa)
|
6
|
+
|
5
7
|
## Features
|
6
8
|
|
7
9
|
- POSIX or non-POSIX mode (supports subcommands using POSIX mode)
|
8
10
|
- Options can require an arbitrary number of parameters
|
9
11
|
- Options can be defined with a range specifying the allowed number of parameters
|
10
12
|
|
11
|
-
## Installation
|
12
|
-
|
13
|
-
Add this line to your application's Gemfile:
|
14
|
-
|
15
|
-
gem 'yawpa'
|
16
|
-
|
17
|
-
And then execute:
|
18
|
-
|
19
|
-
$ bundle
|
20
|
-
|
21
|
-
Or install it yourself as:
|
22
|
-
|
23
|
-
$ gem install yawpa
|
24
|
-
|
25
13
|
## Example 1
|
26
14
|
|
27
|
-
|
15
|
+
```ruby
|
16
|
+
require "yawpa"
|
28
17
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
18
|
+
options = {
|
19
|
+
version: {},
|
20
|
+
verbose: {short: "v"},
|
21
|
+
get: {nargs: 1},
|
22
|
+
set: {nargs: 2},
|
23
|
+
}
|
24
|
+
opts, args = Yawpa.parse(ARGV, options)
|
25
|
+
opts.each_pair do |opt, val|
|
26
|
+
end
|
27
|
+
```
|
38
28
|
|
39
29
|
## Example 2
|
40
30
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
31
|
+
```ruby
|
32
|
+
require "yawpa"
|
33
|
+
|
34
|
+
options = {
|
35
|
+
version: {},
|
36
|
+
help: {short: "h"},
|
37
|
+
}
|
38
|
+
opts, args = Yawpa.parse(ARGV, options, posix_order: true)
|
39
|
+
if opts[:version]
|
40
|
+
puts "my app, version 1.2.3"
|
41
|
+
end
|
42
|
+
if args[0] == "subcommand"
|
43
|
+
subcommand_options = {
|
44
|
+
"server": {nargs: (1..2), short: "s"},
|
45
|
+
"dst": {nargs: 1, short: "d"},
|
46
|
+
}
|
47
|
+
opts, args = Yawpa.parse(args, subcommand_options)
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## Using Yawpa.parse()
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
opts, args = Yawpa.parse(params, options, flags = {})
|
55
|
+
```
|
56
|
+
|
57
|
+
Parse input parameters looking for options according to rules given in flags
|
58
|
+
|
59
|
+
- `params` is the list of program parameters to parse.
|
60
|
+
- `options` is a hash containing the long option names as keys, and hashes
|
61
|
+
containing special flags for the options as values (example below).
|
62
|
+
Possible values:
|
63
|
+
- `nil`: No special flags for this option (equivalent to `{}`)
|
64
|
+
- `:boolean`: The option is a toggleable boolean option (equivalent to
|
65
|
+
`{boolean: true}`)
|
66
|
+
- `Hash`: Possible option flags:
|
67
|
+
- `:short`: specify a short option letter to associate with the long option
|
68
|
+
- `:nargs`: specify an exact number or range of possible numbers of
|
69
|
+
arguments to the option
|
70
|
+
- `:boolean`: if true, specify that the option is a toggleable boolean
|
71
|
+
option and allow a prefix of "no" to turn it off.
|
72
|
+
- `flags` is optional. It supports the following keys:
|
73
|
+
- `:posix_order`: Stop processing parameters when a non-option is seen.
|
74
|
+
Set this to `true` if you want to implement subcommands.
|
75
|
+
|
76
|
+
An ArgumentParsingException will be raised if an unknown option is observed
|
77
|
+
or insufficient arguments are present for an option.
|
78
|
+
|
79
|
+
### Example `options`
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
{
|
83
|
+
version: nil,
|
84
|
+
verbose: {short: 'v'},
|
85
|
+
server: {nargs: (1..2)},
|
86
|
+
username: {nargs: 1},
|
87
|
+
password: {nargs: 1},
|
88
|
+
color: :boolean,
|
89
|
+
}
|
90
|
+
```
|
91
|
+
|
92
|
+
The keys of the `options` hash can be either strings or symbols.
|
93
|
+
|
94
|
+
Possible option flags:
|
95
|
+
|
96
|
+
- `:short`: specify a short option letter to associate with the long option
|
97
|
+
- `:nargs`: specify an exact number or range of possible numbers of
|
98
|
+
arguments to the option
|
99
|
+
- `:boolean`: if true, specify that the option is a toggleable boolean
|
100
|
+
option and allow a prefix of "no" to turn it off.
|
101
|
+
|
102
|
+
### Return values
|
103
|
+
|
104
|
+
The returned `opts` value will be a hash with the observed options as
|
105
|
+
keys and any option arguments as values.
|
106
|
+
The returned `args` will be an array of the unprocessed parameters (if
|
107
|
+
`:posix_order` was passed in `flags`, this array might contain further
|
108
|
+
options that were not processed after observing a non-option parameters).
|
109
|
+
|
110
|
+
## Release Notes
|
111
|
+
|
112
|
+
### v1.1.0
|
113
|
+
|
114
|
+
- Add `:boolean` option flag.
|
115
|
+
- Support `nil` or `:boolean` as shortcut option configuration values.
|
116
|
+
- Update documentation to YARD.
|
117
|
+
- Update specs to RSpec 3.
|
118
|
+
|
119
|
+
### v1.0.0
|
120
|
+
|
121
|
+
- Initial Release
|
data/Rakefile.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler"
|
2
|
+
begin
|
3
|
+
Bundler.setup(:default, :development)
|
4
|
+
rescue Bundler::BundlerError => e
|
5
|
+
raise LoadError.new("Unable to setup Bundler; you might need to `bundle install`: #{e.message}")
|
6
|
+
end
|
7
|
+
require "bundler/gem_tasks"
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
require "yard"
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new('spec')
|
12
|
+
|
13
|
+
task :default => :spec
|
14
|
+
|
15
|
+
YARD::Rake::YardocTask.new do |yard|
|
16
|
+
yard.options = ["--title", "Yet Another Way to Parse Arguments"]
|
17
|
+
yard.files = ["lib/**/*.rb"]
|
18
|
+
end
|
data/lib/yawpa/version.rb
CHANGED
data/lib/yawpa.rb
CHANGED
@@ -7,58 +7,71 @@ require "yawpa/version"
|
|
7
7
|
# it just provides a simple functional interface for parsing options,
|
8
8
|
# supporting subcommands and arbitrary numbers of arguments for each option.
|
9
9
|
#
|
10
|
-
#
|
11
|
-
#
|
10
|
+
# Features:
|
12
11
|
# - POSIX or non-POSIX mode (supports subcommands using POSIX mode)
|
13
12
|
# - Options can require an arbitrary number of parameters
|
14
|
-
# - Options can be defined with a range specifying the allowed number of
|
13
|
+
# - Options can be defined with a range specifying the allowed number of
|
14
|
+
# parameters
|
15
15
|
module Yawpa
|
16
|
-
# Exception class raised when an unknown option is observed
|
16
|
+
# Exception class raised when an unknown option is observed.
|
17
17
|
class ArgumentParsingException < Exception; end
|
18
18
|
|
19
|
-
|
20
|
-
#
|
19
|
+
# Parse input parameters looking for options according to rules given in
|
20
|
+
# flags.
|
21
|
+
# Syntax:
|
21
22
|
# opts, args = parse(params, options, flags = {})
|
22
23
|
#
|
23
|
-
# Parse input parameters looking for options according to rules given in flags
|
24
|
-
#
|
25
|
-
# - +params+ is the list of program parameters to parse.
|
26
|
-
# - +options+ is a hash containing the long option names as keys, and hashes
|
27
|
-
# containing special flags for the options as values (example below).
|
28
|
-
# - +flags+ is optional. It supports the following keys:
|
29
|
-
# - +:posix_order+: Stop processing parameters when a non-option is seen.
|
30
|
-
# Set this to +true+ if you want to implement subcommands.
|
31
|
-
#
|
32
24
|
# An ArgumentParsingException will be raised if an unknown option is observed
|
33
25
|
# or insufficient arguments are present for an option.
|
34
26
|
#
|
35
|
-
#
|
27
|
+
# Example +options+:
|
36
28
|
#
|
37
29
|
# {
|
38
|
-
# version:
|
30
|
+
# version: nil,
|
39
31
|
# verbose: {short: 'v'},
|
40
32
|
# server: {nargs: (1..2)},
|
41
33
|
# username: {nargs: 1},
|
42
34
|
# password: {nargs: 1},
|
35
|
+
# color: :boolean,
|
43
36
|
# }
|
44
37
|
#
|
45
|
-
# The keys of the +options+
|
38
|
+
# The keys of the +options+ Hash can be either strings or symbols.
|
46
39
|
#
|
47
|
-
# Options that have no special flags should have an empty hash as the value.
|
48
40
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
41
|
+
# @param params [Array]
|
42
|
+
# List of program parameters to parse.
|
43
|
+
# @param options [Hash]
|
44
|
+
# Hash containing the long option names as keys, and values containing
|
45
|
+
# special flags for the options as values (examples above).
|
46
|
+
# Possible values:
|
47
|
+
# +nil+:: No special flags for this option (equivalent to +{}+)
|
48
|
+
# +:boolean+::
|
49
|
+
# The option is a toggleable boolean option (equivalent to
|
50
|
+
# +{boolean: true}+)
|
51
|
+
# Hash::
|
52
|
+
# Possible option flags:
|
53
|
+
# - +:short+: specify a short option letter to associate with the long option
|
54
|
+
# - +:nargs+: specify an exact number or range of possible numbers of
|
55
|
+
# arguments to the option
|
56
|
+
# - +:boolean+: if true, specify that the option is a toggleable boolean
|
57
|
+
# option and allow a prefix of "no" to turn it off.
|
58
|
+
# @param flags [Hash]
|
59
|
+
# Optional flags dictating how {.parse} should do its job.
|
60
|
+
# @option flags [Boolean] :posix_order
|
61
|
+
# Stop processing parameters when a non-option argument is seen.
|
62
|
+
# Set this to +true+ if you want to implement subcommands.
|
53
63
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
|
64
|
+
# @return [Array]
|
65
|
+
# Two-element array containing +opts+ and +args+ return values.
|
66
|
+
# +opts+::
|
67
|
+
# The returned +opts+ value will be a Hash with the observed
|
68
|
+
# options as keys and any option arguments as values.
|
69
|
+
# +args+::
|
70
|
+
# The returned +args+ will be an Array of the unprocessed
|
71
|
+
# parameters (if +:posix_order+ was passed in +flags+, this array might
|
72
|
+
# contain further options that were not processed after observing a
|
73
|
+
# non-option parameters).
|
74
|
+
def self.parse(params, options, flags = {})
|
62
75
|
options = _massage_options(options)
|
63
76
|
opts = {}
|
64
77
|
args = []
|
@@ -67,12 +80,22 @@ module Yawpa
|
|
67
80
|
param = params[i]
|
68
81
|
if param =~ /^--([^=]+)(?:=(.+))?$/
|
69
82
|
param_name, val = $1, $2
|
83
|
+
bool_val = true
|
70
84
|
if options[param_name].nil?
|
71
|
-
|
85
|
+
if param_name =~ /^no(.*)$/
|
86
|
+
test_param_name = $1
|
87
|
+
if options[test_param_name]
|
88
|
+
param_name = test_param_name
|
89
|
+
bool_val = false
|
90
|
+
end
|
91
|
+
end
|
72
92
|
end
|
73
93
|
opt_config = options[param_name]
|
94
|
+
raise ArgumentParsingException.new("Unknown option '#{param_name}'") unless opt_config
|
74
95
|
param_key = opt_config[:key]
|
75
|
-
if opt_config[:
|
96
|
+
if opt_config[:boolean]
|
97
|
+
opts[param_key] = bool_val
|
98
|
+
elsif opt_config[:nargs].last == 0
|
76
99
|
opts[param_key] = true
|
77
100
|
else
|
78
101
|
opts[param_key] = []
|
@@ -107,7 +130,7 @@ module Yawpa
|
|
107
130
|
|
108
131
|
# Condense 1-element arrays of option values to just the element itself
|
109
132
|
opts.each_key do |k|
|
110
|
-
if opts[k].
|
133
|
+
if opts[k].is_a?(Array) and opts[k].length == 1
|
111
134
|
opts[k] = opts[k].first
|
112
135
|
end
|
113
136
|
end
|
@@ -116,7 +139,7 @@ module Yawpa
|
|
116
139
|
end
|
117
140
|
|
118
141
|
# Internal helper method to gather arguments for an option
|
119
|
-
def _gather(nargs, start_idx, params, initial, param_key, result)
|
142
|
+
def self._gather(nargs, start_idx, params, initial, param_key, result)
|
120
143
|
n_gathered = 0
|
121
144
|
if initial and initial != ''
|
122
145
|
result << initial
|
@@ -137,26 +160,32 @@ module Yawpa
|
|
137
160
|
end
|
138
161
|
num_indices_used
|
139
162
|
end
|
163
|
+
private_class_method :_gather
|
140
164
|
|
141
165
|
# Internal helper method to format the options in a consistent format
|
142
|
-
def _massage_options(options)
|
166
|
+
def self._massage_options(options)
|
143
167
|
{}.tap do |newopts|
|
144
168
|
options.each_pair do |k, v|
|
169
|
+
v = {} if v.nil?
|
170
|
+
v = {boolean: true} if v == :boolean
|
145
171
|
newkey = k.to_s
|
146
172
|
newopts[newkey] = {key: k}
|
147
173
|
nargs = v[:nargs] || 0
|
148
|
-
nargs = (nargs..nargs) if nargs.
|
174
|
+
nargs = (nargs..nargs) if nargs.is_a?(Fixnum)
|
149
175
|
newopts[newkey][:nargs] = nargs
|
150
176
|
newopts[newkey][:short] = v[:short] || ''
|
177
|
+
newopts[newkey][:boolean] = v[:boolean]
|
151
178
|
end
|
152
179
|
end
|
153
180
|
end
|
181
|
+
private_class_method :_massage_options
|
154
182
|
|
155
183
|
# Internal helper method to find an option configuration by short name
|
156
|
-
def _find_opt_config_by_short_name(options, short_name)
|
184
|
+
def self._find_opt_config_by_short_name(options, short_name)
|
157
185
|
options.each_pair do |k, v|
|
158
186
|
return v if v[:short] == short_name
|
159
187
|
end
|
160
188
|
nil
|
161
189
|
end
|
190
|
+
private_class_method :_find_opt_config_by_short_name
|
162
191
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/yawpa_spec.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
1
|
describe Yawpa do
|
4
|
-
describe
|
2
|
+
describe ".parse" do
|
5
3
|
it "returns everything as arguments when no options present" do
|
6
4
|
options = { }
|
7
5
|
params = ['one', 'two', 'three', 'four']
|
8
6
|
opts, args = Yawpa.parse(params, options)
|
9
|
-
opts.
|
10
|
-
args.
|
7
|
+
expect(opts).to eq({})
|
8
|
+
expect(args).to eq(params)
|
11
9
|
end
|
12
10
|
|
13
11
|
it "raises an exception when an invalid option is passed" do
|
@@ -19,15 +17,15 @@ describe Yawpa do
|
|
19
17
|
it "returns boolean options which are set" do
|
20
18
|
options = {
|
21
19
|
one: {},
|
22
|
-
two:
|
20
|
+
two: nil,
|
23
21
|
three: {},
|
24
22
|
}
|
25
23
|
params = ['--one', 'arg', '--two', 'arg2']
|
26
24
|
opts, args = Yawpa.parse(params, options)
|
27
|
-
opts.include?(:one).
|
28
|
-
opts.include?(:two).
|
29
|
-
opts.include?(:three).
|
30
|
-
args.
|
25
|
+
expect(opts.include?(:one)).to be_truthy
|
26
|
+
expect(opts.include?(:two)).to be_truthy
|
27
|
+
expect(opts.include?(:three)).to be_falsey
|
28
|
+
expect(args).to eq(['arg', 'arg2'])
|
31
29
|
end
|
32
30
|
|
33
31
|
it "returns an option's value when nargs = 1" do
|
@@ -36,8 +34,8 @@ describe Yawpa do
|
|
36
34
|
}
|
37
35
|
params = ['--opt', 'val', 'arg']
|
38
36
|
opts, args = Yawpa.parse(params, options)
|
39
|
-
opts[:opt].
|
40
|
-
args.
|
37
|
+
expect(opts[:opt]).to eq('val')
|
38
|
+
expect(args).to eq(['arg'])
|
41
39
|
end
|
42
40
|
|
43
41
|
it "returns an option's values when nargs = 2" do
|
@@ -46,8 +44,8 @@ describe Yawpa do
|
|
46
44
|
}
|
47
45
|
params = ['--opt', 'val1', 'val2']
|
48
46
|
opts, args = Yawpa.parse(params, options)
|
49
|
-
opts[:opt].
|
50
|
-
args.
|
47
|
+
expect(opts[:opt]).to eq(['val1', 'val2'])
|
48
|
+
expect(args).to be_empty
|
51
49
|
end
|
52
50
|
|
53
51
|
it "raises an exception when not enough arguments for an option are given" do
|
@@ -64,8 +62,8 @@ describe Yawpa do
|
|
64
62
|
}
|
65
63
|
params = ['--opt=thevalue', 'arg']
|
66
64
|
opts, args = Yawpa.parse(params, options)
|
67
|
-
opts[:opt].
|
68
|
-
args.
|
65
|
+
expect(opts[:opt]).to eq('thevalue')
|
66
|
+
expect(args).to eq(['arg'])
|
69
67
|
end
|
70
68
|
|
71
69
|
it "uses --opt=val for the first option argument when nargs > 1" do
|
@@ -74,8 +72,8 @@ describe Yawpa do
|
|
74
72
|
}
|
75
73
|
params = ['--opt=val1', 'val2', 'arg']
|
76
74
|
opts, args = Yawpa.parse(params, options)
|
77
|
-
opts[:opt].
|
78
|
-
args.
|
75
|
+
expect(opts[:opt]).to eq(['val1', 'val2'])
|
76
|
+
expect(args).to eq(['arg'])
|
79
77
|
end
|
80
78
|
|
81
79
|
it "returns the last set value when an option is passed twice" do
|
@@ -84,8 +82,8 @@ describe Yawpa do
|
|
84
82
|
}
|
85
83
|
params = ['--opt', 'val1', 'arg1', '--opt', 'val2', 'arg2']
|
86
84
|
opts, args = Yawpa.parse(params, options)
|
87
|
-
opts[:opt].
|
88
|
-
args.
|
85
|
+
expect(opts[:opt]).to eq('val2')
|
86
|
+
expect(args).to eq(['arg1', 'arg2'])
|
89
87
|
end
|
90
88
|
|
91
89
|
it "accepts strings as keys for option configuration" do
|
@@ -94,8 +92,8 @@ describe Yawpa do
|
|
94
92
|
}
|
95
93
|
params = ['xxx', '--crazy-option', 'yyy', 'zzz']
|
96
94
|
opts, args = Yawpa.parse(params, options)
|
97
|
-
opts['crazy-option'].
|
98
|
-
args.
|
95
|
+
expect(opts['crazy-option']).to eq('yyy')
|
96
|
+
expect(args).to eq(['xxx', 'zzz'])
|
99
97
|
end
|
100
98
|
|
101
99
|
it "accepts short options corresponding to a long option" do
|
@@ -104,8 +102,8 @@ describe Yawpa do
|
|
104
102
|
}
|
105
103
|
params = ['-o', 'qqq']
|
106
104
|
opts, args = Yawpa.parse(params, options)
|
107
|
-
opts[:option].
|
108
|
-
args.
|
105
|
+
expect(opts[:option]).to be_truthy
|
106
|
+
expect(args).to eq(['qqq'])
|
109
107
|
end
|
110
108
|
|
111
109
|
it "returns option argument at next position for a short option" do
|
@@ -114,8 +112,8 @@ describe Yawpa do
|
|
114
112
|
}
|
115
113
|
params = ['-o', 'val', 'rrr']
|
116
114
|
opts, args = Yawpa.parse(params, options)
|
117
|
-
opts[:option].
|
118
|
-
args.
|
115
|
+
expect(opts[:option]).to eq('val')
|
116
|
+
expect(args).to eq(['rrr'])
|
119
117
|
end
|
120
118
|
|
121
119
|
it "returns option argument immediately following short option" do
|
@@ -124,8 +122,8 @@ describe Yawpa do
|
|
124
122
|
}
|
125
123
|
params = ['-oval', 'rrr']
|
126
124
|
opts, args = Yawpa.parse(params, options)
|
127
|
-
opts[:option].
|
128
|
-
args.
|
125
|
+
expect(opts[:option]).to eq('val')
|
126
|
+
expect(args).to eq(['rrr'])
|
129
127
|
end
|
130
128
|
|
131
129
|
it "handles globbed-together short options" do
|
@@ -137,11 +135,11 @@ describe Yawpa do
|
|
137
135
|
}
|
138
136
|
params = ['-abc', 'xyz']
|
139
137
|
opts, args = Yawpa.parse(params, options)
|
140
|
-
opts[:a].
|
141
|
-
opts[:b].
|
142
|
-
opts[:c].
|
143
|
-
opts[:d].
|
144
|
-
args.
|
138
|
+
expect(opts[:a]).to be_truthy
|
139
|
+
expect(opts[:b]).to be_truthy
|
140
|
+
expect(opts[:c]).to be_truthy
|
141
|
+
expect(opts[:d]).to be_nil
|
142
|
+
expect(args).to eq(['xyz'])
|
145
143
|
end
|
146
144
|
|
147
145
|
it "handles globbed-together short options with values following" do
|
@@ -153,11 +151,11 @@ describe Yawpa do
|
|
153
151
|
}
|
154
152
|
params = ['-abcfoo', 'bar']
|
155
153
|
opts, args = Yawpa.parse(params, options)
|
156
|
-
opts[:a].
|
157
|
-
opts[:b].
|
158
|
-
opts[:c].
|
159
|
-
opts[:d].
|
160
|
-
args.
|
154
|
+
expect(opts[:a]).to be_truthy
|
155
|
+
expect(opts[:b]).to be_truthy
|
156
|
+
expect(opts[:c]).to eq('foo')
|
157
|
+
expect(opts[:d]).to be_nil
|
158
|
+
expect(args).to eq(['bar'])
|
161
159
|
end
|
162
160
|
|
163
161
|
it "handles globbed-together short options with multiple values following" do
|
@@ -169,11 +167,11 @@ describe Yawpa do
|
|
169
167
|
}
|
170
168
|
params = ['-abcfoo', 'bar', 'baz']
|
171
169
|
opts, args = Yawpa.parse(params, options)
|
172
|
-
opts[:a].
|
173
|
-
opts[:b].
|
174
|
-
opts[:c].
|
175
|
-
opts[:d].
|
176
|
-
args.
|
170
|
+
expect(opts[:a]).to be_truthy
|
171
|
+
expect(opts[:b]).to be_truthy
|
172
|
+
expect(opts[:c]).to eq(['foo', 'bar', 'baz'])
|
173
|
+
expect(opts[:d]).to be_nil
|
174
|
+
expect(args).to be_empty
|
177
175
|
end
|
178
176
|
|
179
177
|
it "raises an error on an unknown short option" do
|
@@ -198,20 +196,39 @@ describe Yawpa do
|
|
198
196
|
}
|
199
197
|
params = ['--option', 'VALUE', '-o', 'NEW_VALUE']
|
200
198
|
opts, args = Yawpa.parse(params, options)
|
201
|
-
opts[:option].
|
202
|
-
args.
|
199
|
+
expect(opts[:option]).to eq('NEW_VALUE')
|
200
|
+
expect(args).to be_empty
|
203
201
|
end
|
204
202
|
|
205
203
|
it "ignores options after arguments in posix_order mode" do
|
206
204
|
options = {
|
207
205
|
one: {},
|
208
|
-
two:
|
206
|
+
two: nil,
|
209
207
|
}
|
210
208
|
params = ['--one', 'arg', '--two']
|
211
209
|
opts, args = Yawpa.parse(params, options, posix_order: true)
|
212
|
-
opts[:one].
|
213
|
-
opts[:two].
|
214
|
-
args.
|
210
|
+
expect(opts[:one]).to be_truthy
|
211
|
+
expect(opts[:two]).to be_falsey
|
212
|
+
expect(args).to eq(['arg', '--two'])
|
213
|
+
end
|
214
|
+
|
215
|
+
it "supports :boolean option flag" do
|
216
|
+
options = {
|
217
|
+
push: :boolean,
|
218
|
+
pull: {boolean: true},
|
219
|
+
}
|
220
|
+
|
221
|
+
opts, args = Yawpa.parse(%w[hi], options)
|
222
|
+
expect(opts).to eq({})
|
223
|
+
expect(args).to eq(%w[hi])
|
224
|
+
|
225
|
+
opts, args = Yawpa.parse(%w[--push one two], options)
|
226
|
+
expect(opts).to eq(push: true)
|
227
|
+
expect(args).to eq(%w[one two])
|
228
|
+
|
229
|
+
opts, args = Yawpa.parse(%w[arg --nopush --pull], options)
|
230
|
+
expect(opts).to eq(push: false, pull: true)
|
231
|
+
expect(args).to eq(%w[arg])
|
215
232
|
end
|
216
233
|
end
|
217
234
|
end
|
data/yawpa.gemspec
CHANGED
@@ -15,5 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Yawpa::VERSION
|
17
17
|
|
18
|
-
gem.add_development_dependency
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
gem.add_development_dependency "simplecov"
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "rdoc"
|
22
|
+
gem.add_development_dependency "yard"
|
23
|
+
gem.add_development_dependency "redcarpet"
|
19
24
|
end
|
metadata
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yawpa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Josh Holtrop
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2014-06-25 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rspec
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
@@ -20,6 +22,87 @@ dependencies:
|
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: simplecov
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rdoc
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: yard
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: redcarpet
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
23
106
|
requirements:
|
24
107
|
- - ! '>='
|
25
108
|
- !ruby/object:Gem::Version
|
@@ -35,9 +118,10 @@ files:
|
|
35
118
|
- .gitignore
|
36
119
|
- .rspec
|
37
120
|
- Gemfile
|
121
|
+
- Gemfile.lock
|
38
122
|
- LICENSE
|
39
123
|
- README.md
|
40
|
-
- Rakefile
|
124
|
+
- Rakefile.rb
|
41
125
|
- lib/yawpa.rb
|
42
126
|
- lib/yawpa/version.rb
|
43
127
|
- spec/spec_helper.rb
|
@@ -45,27 +129,35 @@ files:
|
|
45
129
|
- yawpa.gemspec
|
46
130
|
homepage: ''
|
47
131
|
licenses: []
|
48
|
-
metadata: {}
|
49
132
|
post_install_message:
|
50
133
|
rdoc_options: []
|
51
134
|
require_paths:
|
52
135
|
- lib
|
53
136
|
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
54
138
|
requirements:
|
55
139
|
- - ! '>='
|
56
140
|
- !ruby/object:Gem::Version
|
57
141
|
version: '0'
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
hash: 77772525
|
58
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
59
147
|
requirements:
|
60
148
|
- - ! '>='
|
61
149
|
- !ruby/object:Gem::Version
|
62
150
|
version: '0'
|
151
|
+
segments:
|
152
|
+
- 0
|
153
|
+
hash: 77772525
|
63
154
|
requirements: []
|
64
155
|
rubyforge_project:
|
65
|
-
rubygems_version:
|
156
|
+
rubygems_version: 1.8.23.2
|
66
157
|
signing_key:
|
67
|
-
specification_version:
|
158
|
+
specification_version: 3
|
68
159
|
summary: Yet Another Way to Parse Arguments
|
69
160
|
test_files:
|
70
161
|
- spec/spec_helper.rb
|
71
162
|
- spec/yawpa_spec.rb
|
163
|
+
has_rdoc:
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
NDQxOWYxMWMwMjA3NjU5NjlkOTQ4MTFkM2Y3OTZkNTI5MGVjNzJiZg==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YzQ1NGE3MDM1MjJkNjRiMjM0MTJjMDFiNmRmNmVhODJiZWNmMjVlMQ==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
MTdjNTBkMTM5NDY5MmNjMTYwY2VmNmU4NjRhMjM0MjVjYTM3YjM3MTY0OWY3
|
10
|
-
MjY4M2U1NTI5ZTZlZmM3N2E2MDU2YmE3MTBiYzEyZWQxODA5NzEwOTA2YjM3
|
11
|
-
ZDMwNzQzNmJhOTRhN2FlZGZjNDFiNDYyYWM0OTQ4NmNlMjZhNTk=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
OWExNTUyNmE4ODM1YTFiNWMwNTg4YjdmMTU1MWJiOTM2ZmUxNmFiMjc3ZjI1
|
14
|
-
YWQxMWU1ZTU2MjZjMTA0NTc2MmFjOWJkOWVhMjk4ZDdlMmQ3ZDk0ZDI2M2Vl
|
15
|
-
MzI0NmQ5YmEyNTA3NGIxMjdhYjE4ZTYxZjIzYTgwMDI4YmUxY2I=
|
data/Rakefile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require 'rspec/core/rake_task'
|
3
|
-
require "rdoc/task"
|
4
|
-
|
5
|
-
RSpec::Core::RakeTask.new('spec')
|
6
|
-
|
7
|
-
task :default => :spec
|
8
|
-
|
9
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'Yet Another Way to Parse Arguments'
|
12
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
13
|
-
end
|