everyday-cli-utils 0.7.0 → 1.0.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 +10 -0
- data/lib/everyday-cli-utils/option.rb +44 -16
- data/lib/everyday-cli-utils/version.rb +1 -1
- data/spec/everyday-cli-utils/option_spec.rb +30 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7897a11ac454ad40fbac503cbee4929c032052f2
|
4
|
+
data.tar.gz: 65d659f085bef676655c2c4c208c67cb63b8d55d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86d3c40d13be8545c2542fc76fb83179ead683b6d5524afb0c337c7c9d221a1b3bf021d7f09552aa665db831ba92c2b631c1028858072b6176e8e1198b0c3194
|
7
|
+
data.tar.gz: 3b574b15dc95951a68f5241c7cf6aec251c514656f068589f47dce4e8b37f8325477e4ed0a2490e88bb82f082ec387f1696ea1cb18e5b99bc08b32b428bb9282
|
data/README.md
CHANGED
@@ -345,6 +345,12 @@ As of version 0.6.0, there is a new way to use the option package. See the belo
|
|
345
345
|
class MyOptions
|
346
346
|
extend EverydayCliUtil::OptionUtil
|
347
347
|
|
348
|
+
banner 'test app' # <-- version 1.0.0 and up
|
349
|
+
|
350
|
+
defaults_option 'defaults.yaml', ['-d', '--set-defaults'] # <-- version 0.7.0 and up
|
351
|
+
|
352
|
+
help_option ['-h', '--help'], desc: 'display this help' # <-- version 1.0.0 and up
|
353
|
+
|
348
354
|
option :opt1, ['-1', '--opt-1']
|
349
355
|
option_with_param :opt2, ['-2', '--opt-2 PARAM']
|
350
356
|
end
|
@@ -358,6 +364,10 @@ The two methods shown above are the same as the ones in `EverydayCliUtils::Optio
|
|
358
364
|
|
359
365
|
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 = {})`.
|
360
366
|
|
367
|
+
As of version 0.7.0, there is now built-in handling for setting and retrieving default options. Use `EverydayCliUtils::OptionUtil.defaults_option`, which takes the file name (relative or absolute, it passes through `File.expand_path` before being used) as the first parameter and the list of option flag names as the second parameter. It will automatically load the file if it exists, and if the user specifies one of the flags you pass to this method, after parsing the options, it will automatically store them in the place you specified. Unless you specify the hash option `exit_on_save: false`, it will exit after it saves the options.
|
368
|
+
|
369
|
+
As of version 1.0.0, there is now support for the help display in `OptionParser`. You can now provide a `desc:` hash option to the option creating methods (even pre-made ones like `defaults_option` and `help_option`). You can set the banner with the `EverydayCliUtils::OptionUtil.banner` method, which takes the banner string as its parameter. You can get the help string with `EverydayCliUtils::OptionUtil.help` or `EverydayCliUtils::OptionUtil.to_s`, or you can handle it with `EverydayCliUtils::OptionUtil.help_option`, which takes an array of the names and an optional `desc:` hash option. When the user specifies one of those options, the utility will automatically print out the help and exit (unless you specify the hash option `exit_on_print: false`).
|
370
|
+
|
361
371
|
## Contributing
|
362
372
|
|
363
373
|
1. Fork it ( http://github.com/henderea/everyday-cli-utils/fork )
|
@@ -26,10 +26,11 @@ module EverydayCliUtils
|
|
26
26
|
attr_reader :options, :opts
|
27
27
|
|
28
28
|
def option(opt_name, names, settings = {})
|
29
|
-
@opts
|
30
|
-
@options
|
31
|
-
@default_settings
|
32
|
-
settings[:toggle]
|
29
|
+
@opts ||= OptionParser.new
|
30
|
+
@options ||= {}
|
31
|
+
@default_settings ||= {}
|
32
|
+
settings[:toggle] = @default_settings[:toggle] unless settings.has_key?(:toggle) || !@default_settings.has_key?(:toggle)
|
33
|
+
names << settings[:desc] if settings.has_key?(:desc)
|
33
34
|
@options[opt_name] = false
|
34
35
|
@opts.on(*names) {
|
35
36
|
@options[opt_name] = !settings[:toggle] || !@options[opt_name]
|
@@ -38,30 +39,37 @@ module EverydayCliUtils
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def option_with_param(opt_name, names, settings = {})
|
41
|
-
@opts
|
42
|
-
@options
|
43
|
-
@default_settings
|
44
|
-
settings[:append]
|
45
|
-
settings[:type]
|
42
|
+
@opts ||= OptionParser.new
|
43
|
+
@options ||= {}
|
44
|
+
@default_settings ||= {}
|
45
|
+
settings[:append] = @default_settings[:append] unless settings.has_key?(:append) || !@default_settings.has_key?(:append)
|
46
|
+
settings[:type] = @default_settings[:type] unless settings.has_key?(:type) || !@default_settings.has_key?(:type)
|
47
|
+
names[0] << ' PARAM' unless names.any? { |v| v.include?(' ') }
|
48
|
+
names << settings[:desc] if settings.has_key?(:desc)
|
46
49
|
@options[opt_name] = settings[:append] ? [] : nil
|
47
50
|
@opts.on(*names, settings[:type] || String) { |param|
|
48
|
-
|
49
|
-
@options[opt_name] << param
|
50
|
-
else
|
51
|
-
@options[opt_name] = param
|
52
|
-
end
|
51
|
+
settings[:append] ? @options[opt_name] << param : @options[opt_name] = param
|
53
52
|
yield if block_given?
|
54
53
|
}
|
55
54
|
end
|
56
55
|
|
57
|
-
def defaults_option(file_path, names,
|
56
|
+
def defaults_option(file_path, names, settings = {})
|
58
57
|
@opts ||= OptionParser.new
|
59
58
|
@set_defaults = false
|
60
59
|
@defaults_file = File.expand_path(file_path)
|
61
|
-
@exit_on_save = exit_on_save
|
60
|
+
@exit_on_save = !settings.has_key?(:exit_on_save) || settings[:exit_on_save]
|
61
|
+
names << settings[:desc] if settings.has_key?(:desc)
|
62
62
|
@opts.on(*names) { @set_defaults = true }
|
63
63
|
end
|
64
64
|
|
65
|
+
def help_option(names, settings = {})
|
66
|
+
@opts ||= OptionParser.new
|
67
|
+
@display_help = false
|
68
|
+
@exit_on_print = !settings.has_key?(:exit_on_print) || settings[:exit_on_print]
|
69
|
+
names << settings[:desc] if settings.has_key?(:desc)
|
70
|
+
@opts.on(*names) { @display_help = true }
|
71
|
+
end
|
72
|
+
|
65
73
|
def default_settings(settings = {})
|
66
74
|
@default_settings = settings
|
67
75
|
end
|
@@ -70,9 +78,29 @@ module EverydayCliUtils
|
|
70
78
|
opts.each { |opt| @options[opt[0]] = opt[1] }
|
71
79
|
end
|
72
80
|
|
81
|
+
def banner(banner)
|
82
|
+
@opts ||= OptionParser.new
|
83
|
+
@opts.banner = banner
|
84
|
+
end
|
85
|
+
|
86
|
+
def help
|
87
|
+
@opts ||= OptionParser.new
|
88
|
+
@opts.help
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_s
|
92
|
+
@opts ||= OptionParser.new
|
93
|
+
@opts.to_s
|
94
|
+
end
|
95
|
+
|
73
96
|
def parse!(argv = ARGV)
|
97
|
+
@opts ||= OptionParser.new
|
74
98
|
default_options YAML::load_file(@defaults_file) unless @defaults_file.nil? || !File.exist?(@defaults_file)
|
75
99
|
@opts.parse!(argv)
|
100
|
+
if @display_help
|
101
|
+
puts help
|
102
|
+
exit 0 if @exit_on_print
|
103
|
+
end
|
76
104
|
if @set_defaults
|
77
105
|
IO.write(@defaults_file, @options.to_yaml)
|
78
106
|
if @exit_on_save
|
@@ -55,6 +55,22 @@ describe EverydayCliUtils::OptionUtil do
|
|
55
55
|
opt.options.should eq expected
|
56
56
|
end
|
57
57
|
|
58
|
+
it 'adds the parameter into a name if it is missing from all' do
|
59
|
+
expected = { opt1: 'hi' }
|
60
|
+
clean = { opt1: nil }
|
61
|
+
opt = Option1.new
|
62
|
+
opt.option_with_param :opt1, %w(-1 --opt-1)
|
63
|
+
opt.options.should eq clean
|
64
|
+
opt.default_options opt1: nil
|
65
|
+
opt.options.should eq clean
|
66
|
+
opt.parse!(%w(-1 hi))
|
67
|
+
opt.options.should eq expected
|
68
|
+
opt.default_options opt1: nil
|
69
|
+
opt.options.should eq clean
|
70
|
+
opt.parse!(%w(--opt-1 hi))
|
71
|
+
opt.options.should eq expected
|
72
|
+
end
|
73
|
+
|
58
74
|
it 'supports adding an option with a parameter and type' do
|
59
75
|
expected = { opt1: 1 }
|
60
76
|
clean = { opt1: nil }
|
@@ -153,4 +169,18 @@ describe EverydayCliUtils::OptionUtil do
|
|
153
169
|
opt.parse!(%w(--opt-1 hi -1 bye))
|
154
170
|
opt.options.should eq expected
|
155
171
|
end
|
172
|
+
|
173
|
+
it 'supports setting a banner and description' do
|
174
|
+
opt = Option1.new
|
175
|
+
opt.banner 'option1'
|
176
|
+
opt.option :opt1, %w(-1 --opt-1), desc: 'option #1'
|
177
|
+
opt.option_with_param :opt2, %w(-2 --opt-2), desc: 'option #2 (takes parameter)'
|
178
|
+
opt.defaults_option 'defaults.yaml', %w(-0 --set-defaults), desc: 'set defaults'
|
179
|
+
expected = 'option1
|
180
|
+
-1, --opt-1 option #1
|
181
|
+
-2, --opt-2 PARAM option #2 (takes parameter)
|
182
|
+
-0, --set-defaults set defaults
|
183
|
+
'
|
184
|
+
opt.to_s.should eq expected
|
185
|
+
end
|
156
186
|
end
|