everyday-cli-utils 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/everyday-cli-utils/option.rb +45 -0
- data/lib/everyday-cli-utils/version.rb +1 -1
- data/spec/everyday-cli-utils/option_spec.rb +156 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60052f772a0756a80b71446915644a962d1749fc
|
4
|
+
data.tar.gz: 7f44acf9585a64c409615ae159e222fb6ad3ab47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c685c69bc215ace083e665b6d5e75b9fc84daa64cbf2cafdd4a1ef33a509cf7204a9969d5d41ad1ae06dd3d90218fd3553c8e54682c6ad0c487d0e10467e0d28
|
7
|
+
data.tar.gz: 6b18808f2e00a4955d1f444af8e48bcbc2062a8b5e5971893f29746a808e37a23c1414e50b99b4b019d94d6584008ed5fccaa6a4d0afcf26db2f5868d29611b1
|
data/README.md
CHANGED
@@ -336,6 +336,27 @@ Add a parameterized option to the `OptionParser`. It will set the specified opt
|
|
336
336
|
* `settings`: additional optional options
|
337
337
|
* `type: <data type>`: specify a type (such as `Integer` or `Float`) for the option parser to try to parse the parameter to (default is `String` (no parsing))
|
338
338
|
* `append: true|false`: true to append the parameter to a list (for parameterized options that can occur multiple times), false or omitted to just set it to the parameter
|
339
|
+
|
340
|
+
### EverydayCliUtils::OptionUtil
|
341
|
+
|
342
|
+
As of version 0.6.0, there is a new way to use the option package. See the below example:
|
343
|
+
|
344
|
+
```ruby
|
345
|
+
class MyOptions
|
346
|
+
extend EverydayCliUtil::OptionUtil
|
347
|
+
|
348
|
+
option :opt1, ['-1', '--opt-1']
|
349
|
+
option_with_param :opt2, ['-2', '--opt-2 PARAM']
|
350
|
+
end
|
351
|
+
|
352
|
+
MyOptions.parse!
|
353
|
+
|
354
|
+
opts = MyOptions.options
|
355
|
+
```
|
356
|
+
|
357
|
+
The two methods shown above are the same as the ones in `EverydayCliUtils::Option`, but renamed to be shorter, and with the `opt_name` and `names` parameters switched around. Also, the options hash and `OptionParser` instance are handled internally, so you don't have to pass those in. There are read-only accessors for both of them.
|
358
|
+
|
359
|
+
Besides the different look, there are also improvements. `EverydayCliUtils::OptionUtil.default_settings(settings = {})` is a new method that you can use to set the default values of the settings that you can pass to `option` and `option_with_param`. Also, since this utility manages the options hash for you, in order to provide you with a way to override the defaults (`false` for boolean options, `nil` for non-appending parameter options, and `[]` for appending parameter options (don't change this)) by using `EverydayCliUtils::OptionUtil.default_options(options = {})`.
|
339
360
|
|
340
361
|
## Contributing
|
341
362
|
|
@@ -18,4 +18,49 @@ module EverydayCliUtils
|
|
18
18
|
}
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
module OptionUtil
|
23
|
+
attr_reader :options, :opts
|
24
|
+
|
25
|
+
def option(opt_name, names, settings = {})
|
26
|
+
@opts ||= OptionParser.new
|
27
|
+
@options ||= {}
|
28
|
+
@default_settings ||= {}
|
29
|
+
settings[:toggle] = @default_settings[:toggle] unless settings.has_key?(:toggle) || !@default_settings.has_key?(:toggle)
|
30
|
+
@options[opt_name] = false
|
31
|
+
@opts.on(*names) {
|
32
|
+
@options[opt_name] = !settings[:toggle] || !@options[opt_name]
|
33
|
+
yield if block_given?
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def option_with_param(opt_name, names, settings = {})
|
38
|
+
@opts ||= OptionParser.new
|
39
|
+
@options ||= {}
|
40
|
+
@default_settings ||= {}
|
41
|
+
settings[:append] = @default_settings[:append] unless settings.has_key?(:append) || !@default_settings.has_key?(:append)
|
42
|
+
settings[:type] = @default_settings[:type] unless settings.has_key?(:type) || !@default_settings.has_key?(:type)
|
43
|
+
@options[opt_name] = settings[:append] ? [] : nil
|
44
|
+
@opts.on(*names, settings[:type] || String) { |param|
|
45
|
+
if settings[:append]
|
46
|
+
@options[opt_name] << param
|
47
|
+
else
|
48
|
+
@options[opt_name] = param
|
49
|
+
end
|
50
|
+
yield if block_given?
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def default_settings(settings = {})
|
55
|
+
@default_settings = settings
|
56
|
+
end
|
57
|
+
|
58
|
+
def default_options(opts = {})
|
59
|
+
opts.each { |opt| @options[opt[0]] = opt[1] }
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse!(argv = ARGV)
|
63
|
+
@opts.parse!(argv)
|
64
|
+
end
|
65
|
+
end
|
21
66
|
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
require_relative '../../lib/everyday-cli-utils'
|
2
|
+
include EverydayCliUtils
|
3
|
+
import :option
|
4
|
+
|
5
|
+
class Option1
|
6
|
+
include EverydayCliUtils::OptionUtil
|
7
|
+
end
|
8
|
+
|
9
|
+
describe EverydayCliUtils::OptionUtil do
|
10
|
+
it 'supports adding boolean options' do
|
11
|
+
expected = { opt1: true }
|
12
|
+
clean = { opt1: false }
|
13
|
+
opt = Option1.new
|
14
|
+
opt.option :opt1, %w(-1 --opt-1)
|
15
|
+
opt.options.should eq clean
|
16
|
+
opt.default_options opt1: false
|
17
|
+
opt.options.should eq clean
|
18
|
+
opt.parse!(['-1'])
|
19
|
+
opt.options.should eq expected
|
20
|
+
opt.default_options opt1: false
|
21
|
+
opt.options.should eq clean
|
22
|
+
opt.parse!(['--opt-1'])
|
23
|
+
opt.options.should eq expected
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'supports adding boolean toggle options' do
|
27
|
+
expected = { opt1: false }
|
28
|
+
clean = { opt1: true }
|
29
|
+
opt = Option1.new
|
30
|
+
opt.option :opt1, %w(-1 --opt-1), toggle: true
|
31
|
+
opt.options.should eq expected
|
32
|
+
opt.default_options opt1: true
|
33
|
+
opt.options.should eq clean
|
34
|
+
opt.parse!(['-1'])
|
35
|
+
opt.options.should eq expected
|
36
|
+
opt.default_options opt1: true
|
37
|
+
opt.options.should eq clean
|
38
|
+
opt.parse!(['--opt-1'])
|
39
|
+
opt.options.should eq expected
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'supports adding an option with a parameter' do
|
43
|
+
expected = { opt1: 'hi' }
|
44
|
+
clean = { opt1: nil }
|
45
|
+
opt = Option1.new
|
46
|
+
opt.option_with_param :opt1, ['-1', '--opt-1 PARAM']
|
47
|
+
opt.options.should eq clean
|
48
|
+
opt.default_options opt1: nil
|
49
|
+
opt.options.should eq clean
|
50
|
+
opt.parse!(%w(-1 hi))
|
51
|
+
opt.options.should eq expected
|
52
|
+
opt.default_options opt1: nil
|
53
|
+
opt.options.should eq clean
|
54
|
+
opt.parse!(%w(--opt-1 hi))
|
55
|
+
opt.options.should eq expected
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'supports adding an option with a parameter and type' do
|
59
|
+
expected = { opt1: 1 }
|
60
|
+
clean = { opt1: nil }
|
61
|
+
opt = Option1.new
|
62
|
+
opt.option_with_param :opt1, ['-1', '--opt-1 PARAM'], type: Integer
|
63
|
+
opt.options.should eq clean
|
64
|
+
opt.default_options opt1: nil
|
65
|
+
opt.options.should eq clean
|
66
|
+
opt.parse!(%w(-1 1))
|
67
|
+
opt.options.should eq expected
|
68
|
+
opt.default_options opt1: nil
|
69
|
+
opt.options.should eq clean
|
70
|
+
opt.parse!(%w(--opt-1 1))
|
71
|
+
opt.options.should eq expected
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'supports adding an option with a parameter that store multiple instances' do
|
75
|
+
expected = { opt1: %w(hi bye) }
|
76
|
+
clean = { opt1: [] }
|
77
|
+
opt = Option1.new
|
78
|
+
opt.option_with_param :opt1, ['-1', '--opt-1 PARAM'], append: true
|
79
|
+
opt.options.should eq clean
|
80
|
+
opt.default_options opt1: []
|
81
|
+
opt.options.should eq clean
|
82
|
+
opt.parse!(%w(-1 hi -1 bye))
|
83
|
+
opt.options.should eq expected
|
84
|
+
opt.default_options opt1: []
|
85
|
+
opt.options.should eq clean
|
86
|
+
opt.parse!(%w(--opt-1 hi --opt-1 bye))
|
87
|
+
opt.options.should eq expected
|
88
|
+
opt.default_options opt1: []
|
89
|
+
opt.options.should eq clean
|
90
|
+
opt.parse!(%w(-1 hi --opt-1 bye))
|
91
|
+
opt.options.should eq expected
|
92
|
+
opt.default_options opt1: []
|
93
|
+
opt.options.should eq clean
|
94
|
+
opt.parse!(%w(--opt-1 hi -1 bye))
|
95
|
+
opt.options.should eq expected
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'supports setting the default for the toggle setting' do
|
99
|
+
expected = { opt1: false }
|
100
|
+
clean = { opt1: true }
|
101
|
+
opt = Option1.new
|
102
|
+
opt.default_settings toggle: true
|
103
|
+
opt.option :opt1, %w(-1 --opt-1)
|
104
|
+
opt.options.should eq expected
|
105
|
+
opt.default_options opt1: true
|
106
|
+
opt.options.should eq clean
|
107
|
+
opt.parse!(['-1'])
|
108
|
+
opt.options.should eq expected
|
109
|
+
opt.default_options opt1: true
|
110
|
+
opt.options.should eq clean
|
111
|
+
opt.parse!(['--opt-1'])
|
112
|
+
opt.options.should eq expected
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'supports setting the default for the type setting' do
|
116
|
+
expected = { opt1: 1 }
|
117
|
+
clean = { opt1: nil }
|
118
|
+
opt = Option1.new
|
119
|
+
opt.default_settings type: Integer
|
120
|
+
opt.option_with_param :opt1, ['-1', '--opt-1 PARAM']
|
121
|
+
opt.options.should eq clean
|
122
|
+
opt.default_options opt1: nil
|
123
|
+
opt.options.should eq clean
|
124
|
+
opt.parse!(%w(-1 1))
|
125
|
+
opt.options.should eq expected
|
126
|
+
opt.default_options opt1: nil
|
127
|
+
opt.options.should eq clean
|
128
|
+
opt.parse!(%w(--opt-1 1))
|
129
|
+
opt.options.should eq expected
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'supports adding an option with a parameter that store multiple instances' do
|
133
|
+
expected = { opt1: %w(hi bye) }
|
134
|
+
clean = { opt1: [] }
|
135
|
+
opt = Option1.new
|
136
|
+
opt.default_settings append: true
|
137
|
+
opt.option_with_param :opt1, ['-1', '--opt-1 PARAM']
|
138
|
+
opt.options.should eq clean
|
139
|
+
opt.default_options opt1: []
|
140
|
+
opt.options.should eq clean
|
141
|
+
opt.parse!(%w(-1 hi -1 bye))
|
142
|
+
opt.options.should eq expected
|
143
|
+
opt.default_options opt1: []
|
144
|
+
opt.options.should eq clean
|
145
|
+
opt.parse!(%w(--opt-1 hi --opt-1 bye))
|
146
|
+
opt.options.should eq expected
|
147
|
+
opt.default_options opt1: []
|
148
|
+
opt.options.should eq clean
|
149
|
+
opt.parse!(%w(-1 hi --opt-1 bye))
|
150
|
+
opt.options.should eq expected
|
151
|
+
opt.default_options opt1: []
|
152
|
+
opt.options.should eq clean
|
153
|
+
opt.parse!(%w(--opt-1 hi -1 bye))
|
154
|
+
opt.options.should eq expected
|
155
|
+
end
|
156
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: everyday-cli-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Henderson
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- spec/everyday-cli-utils/format_spec.rb
|
133
133
|
- spec/everyday-cli-utils/kmeans_spec.rb
|
134
134
|
- spec/everyday-cli-utils/maputil_spec.rb
|
135
|
+
- spec/everyday-cli-utils/option_spec.rb
|
135
136
|
- spec/spec_helper.rb
|
136
137
|
homepage: https://github.com/henderea/everyday-cli-utils
|
137
138
|
licenses:
|
@@ -161,5 +162,6 @@ test_files:
|
|
161
162
|
- spec/everyday-cli-utils/format_spec.rb
|
162
163
|
- spec/everyday-cli-utils/kmeans_spec.rb
|
163
164
|
- spec/everyday-cli-utils/maputil_spec.rb
|
165
|
+
- spec/everyday-cli-utils/option_spec.rb
|
164
166
|
- spec/spec_helper.rb
|
165
167
|
has_rdoc:
|