mixlib-cli 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mixlib/cli/version.rb +1 -1
- metadata +3 -12
- data/Gemfile +0 -28
- data/README.md +0 -120
- data/Rakefile +0 -36
- data/mixlib-cli.gemspec +0 -19
- data/spec/mixlib/cli_spec.rb +0 -365
- data/spec/spec_helper.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8556d57649acdcf0a9517490a377b3d708f7b758eae75df36c999c1c239f4164
|
4
|
+
data.tar.gz: b41e11347024e232fef22d684ec5c075e0f0686d70ffe99804e369ef67027324
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1ab2c63cf759d9f1576f8e9445d0e6155ca12a14ea9151b9105451bcb97bc87ad315b11956aa47fb56359b63301adde102a7d795d31aceacd5957cea08b2bbd
|
7
|
+
data.tar.gz: 15cf78f55c55a547597741030de5f61f3b774878c6c09afe0cb0657a0a54b87e9ba4f47d4a1eb4a02719be3465b5f3b61f45baca83ab336330a9189fa8e2a4ca
|
data/lib/mixlib/cli/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixlib-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chef Software, Inc.
|
@@ -14,22 +14,13 @@ description: A simple mixin for CLI interfaces, including option parsing
|
|
14
14
|
email: info@chef.io
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
|
-
extra_rdoc_files:
|
18
|
-
- README.md
|
19
|
-
- LICENSE
|
20
|
-
- NOTICE
|
17
|
+
extra_rdoc_files: []
|
21
18
|
files:
|
22
|
-
- Gemfile
|
23
19
|
- LICENSE
|
24
20
|
- NOTICE
|
25
|
-
- README.md
|
26
|
-
- Rakefile
|
27
21
|
- lib/mixlib/cli.rb
|
28
22
|
- lib/mixlib/cli/version.rb
|
29
|
-
|
30
|
-
- spec/mixlib/cli_spec.rb
|
31
|
-
- spec/spec_helper.rb
|
32
|
-
homepage: https://www.chef.io
|
23
|
+
homepage: https://www.github.com/mixlib-cli
|
33
24
|
licenses:
|
34
25
|
- Apache-2.0
|
35
26
|
metadata: {}
|
data/Gemfile
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
source "https://rubygems.org"
|
2
|
-
|
3
|
-
gemspec
|
4
|
-
|
5
|
-
group :docs do
|
6
|
-
gem "yard"
|
7
|
-
gem "redcarpet"
|
8
|
-
gem "github-markup"
|
9
|
-
end
|
10
|
-
|
11
|
-
group :test do
|
12
|
-
gem "chefstyle", git: "https://github.com/chef/chefstyle.git", branch: "master"
|
13
|
-
gem "rspec", "~> 3.0"
|
14
|
-
gem "rake"
|
15
|
-
end
|
16
|
-
|
17
|
-
group :development do
|
18
|
-
gem "pry"
|
19
|
-
gem "pry-byebug"
|
20
|
-
gem "pry-stack_explorer"
|
21
|
-
gem "rb-readline"
|
22
|
-
end
|
23
|
-
|
24
|
-
instance_eval(ENV["GEMFILE_MOD"]) if ENV["GEMFILE_MOD"]
|
25
|
-
|
26
|
-
# If you want to load debugging tools into the bundle exec sandbox,
|
27
|
-
# add these additional dependencies into Gemfile.local
|
28
|
-
eval_gemfile(__FILE__ + ".local") if File.exist?(__FILE__ + ".local")
|
data/README.md
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
# Mixlib::CLI
|
2
|
-
|
3
|
-
[](https://travis-ci.org/chef/mixlib-cli) [](https://badge.fury.io/rb/mixlib-cli)
|
4
|
-
|
5
|
-
Mixlib::CLI provides a class-based command line option parsing object, like the one used in Chef, Ohai and Relish. To use in your project:
|
6
|
-
|
7
|
-
```ruby
|
8
|
-
require "mixlib/cli"
|
9
|
-
|
10
|
-
class MyCLI
|
11
|
-
include Mixlib::CLI
|
12
|
-
|
13
|
-
option :config_file,
|
14
|
-
short: "-c CONFIG",
|
15
|
-
long: "--config CONFIG",
|
16
|
-
default: "config.rb",
|
17
|
-
description: "The configuration file to use"
|
18
|
-
|
19
|
-
option :log_level,
|
20
|
-
short: "-l LEVEL",
|
21
|
-
long: "--log_level LEVEL",
|
22
|
-
description: "Set the log level (debug, info, warn, error, fatal)",
|
23
|
-
required: true,
|
24
|
-
in: [:debug, :info, :warn, :error, :fatal],
|
25
|
-
proc: Proc.new { |l| l.to_sym }
|
26
|
-
|
27
|
-
option :help,
|
28
|
-
short: "-h",
|
29
|
-
long: "--help",
|
30
|
-
description: "Show this message",
|
31
|
-
on: :tail,
|
32
|
-
boolean: true,
|
33
|
-
show_options: true,
|
34
|
-
exit: 0
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
# ARGV = [ '-c', 'foo.rb', '-l', 'debug' ]
|
39
|
-
cli = MyCLI.new
|
40
|
-
cli.parse_options
|
41
|
-
cli.config[:config_file] # 'foo.rb'
|
42
|
-
cli.config[:log_level] # :debug
|
43
|
-
```
|
44
|
-
|
45
|
-
If you are using this in conjunction with Mixlib::Config, you can do something like this (building on the above definition):
|
46
|
-
|
47
|
-
```ruby
|
48
|
-
class MyConfig
|
49
|
-
extend(Mixlib::Config)
|
50
|
-
|
51
|
-
log_level :info
|
52
|
-
config_file "default.rb"
|
53
|
-
end
|
54
|
-
|
55
|
-
class MyCLI
|
56
|
-
def run(argv = ARGV)
|
57
|
-
parse_options(argv)
|
58
|
-
MyConfig.merge!(config)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
c = MyCLI.new
|
63
|
-
# ARGV = [ '-l', 'debug' ]
|
64
|
-
c.run
|
65
|
-
MyConfig[:log_level] # :debug
|
66
|
-
```
|
67
|
-
|
68
|
-
Available arguments to 'option':
|
69
|
-
|
70
|
-
- `:short`: The short option, just like from optparse. Example: "-l LEVEL"
|
71
|
-
- `:long`: The long option, just like from optparse. Example: "--level LEVEL"
|
72
|
-
- `:description`: The description for this item, just like from optparse.
|
73
|
-
- `:default`: A default value for this option
|
74
|
-
- `:required`: Prints a message informing the user of the missing requirement, and exits. Default is false.
|
75
|
-
- `:on`: Set to :tail to appear at the end, or `:head`: to appear at the top.
|
76
|
-
- `:boolean:`: If this option takes no arguments, set this to true.
|
77
|
-
- `:show_options`: If you want the option list printed when this option is called, set this to true.
|
78
|
-
- `:exit`: Exit your program with the exit code when this option is specified. Example: 0
|
79
|
-
- `:proc`: If set, the configuration value will be set to the return value of this proc.
|
80
|
-
- `:in`: An array containing the list of accepted values
|
81
|
-
|
82
|
-
If you need access to the leftover options that aren't captured in the config, you can get at them through +cli_arguments+ (referring to the above definition of MyCLI).
|
83
|
-
|
84
|
-
```ruby
|
85
|
-
# ARGV = [ '-c', 'foo.rb', '-l', 'debug', 'file1', 'file2', 'file3' ]
|
86
|
-
cli = MyCLI.new
|
87
|
-
cli.parse_options
|
88
|
-
cli.cli_arguments # [ 'file1', 'file2', 'file3' ]
|
89
|
-
```
|
90
|
-
|
91
|
-
## Documentation
|
92
|
-
|
93
|
-
All documentation is written using YARD. You can generate a by running:
|
94
|
-
|
95
|
-
```
|
96
|
-
rake docs
|
97
|
-
```
|
98
|
-
|
99
|
-
## Contributing
|
100
|
-
|
101
|
-
For information on contributing to this project please see our [Contributing Documentation](https://github.com/chef/chef/blob/master/CONTRIBUTING.md)
|
102
|
-
|
103
|
-
## License & Copyright
|
104
|
-
|
105
|
-
- Copyright:: Copyright (c) 2008-2018 Chef Software, Inc.
|
106
|
-
- License:: Apache License, Version 2.0
|
107
|
-
|
108
|
-
```text
|
109
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
110
|
-
you may not use this file except in compliance with the License.
|
111
|
-
You may obtain a copy of the License at
|
112
|
-
|
113
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
114
|
-
|
115
|
-
Unless required by applicable law or agreed to in writing, software
|
116
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
117
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
118
|
-
See the License for the specific language governing permissions and
|
119
|
-
limitations under the License.
|
120
|
-
```
|
data/Rakefile
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
task default: [:style, :spec]
|
5
|
-
|
6
|
-
Bundler::GemHelper.install_tasks
|
7
|
-
|
8
|
-
desc "Run specs"
|
9
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
-
spec.pattern = "spec/**/*_spec.rb"
|
11
|
-
end
|
12
|
-
|
13
|
-
begin
|
14
|
-
require "chefstyle"
|
15
|
-
require "rubocop/rake_task"
|
16
|
-
RuboCop::RakeTask.new(:style) do |task|
|
17
|
-
task.options += ["--display-cop-names", "--no-color"]
|
18
|
-
end
|
19
|
-
rescue LoadError
|
20
|
-
puts "chefstyle/rubocop is not available. bundle install first to make sure all dependencies are installed."
|
21
|
-
end
|
22
|
-
|
23
|
-
begin
|
24
|
-
require "yard"
|
25
|
-
YARD::Rake::YardocTask.new(:docs)
|
26
|
-
rescue LoadError
|
27
|
-
puts "yard is not available. bundle install first to make sure all dependencies are installed."
|
28
|
-
end
|
29
|
-
|
30
|
-
task :console do
|
31
|
-
require "irb"
|
32
|
-
require "irb/completion"
|
33
|
-
require "mixlib/cli"
|
34
|
-
ARGV.clear
|
35
|
-
IRB.start
|
36
|
-
end
|
data/mixlib-cli.gemspec
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__) + "/lib")
|
2
|
-
require "mixlib/cli/version"
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "mixlib-cli"
|
6
|
-
s.version = Mixlib::CLI::VERSION
|
7
|
-
s.extra_rdoc_files = ["README.md", "LICENSE", "NOTICE"]
|
8
|
-
s.summary = "A simple mixin for CLI interfaces, including option parsing"
|
9
|
-
s.description = s.summary
|
10
|
-
s.author = "Chef Software, Inc."
|
11
|
-
s.email = "info@chef.io"
|
12
|
-
s.homepage = "https://www.chef.io"
|
13
|
-
s.license = "Apache-2.0"
|
14
|
-
s.required_ruby_version = ">= 2.5"
|
15
|
-
|
16
|
-
s.require_path = "lib"
|
17
|
-
s.files = %w{LICENSE README.md Gemfile Rakefile NOTICE} + Dir.glob("*.gemspec") +
|
18
|
-
Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) }
|
19
|
-
end
|
data/spec/mixlib/cli_spec.rb
DELETED
@@ -1,365 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Author:: Adam Jacob (<adam@chef.io>)
|
3
|
-
# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
|
4
|
-
# License:: Apache License, Version 2.0
|
5
|
-
#
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
-
# you may not use this file except in compliance with the License.
|
8
|
-
# You may obtain a copy of the License at
|
9
|
-
#
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
-
#
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
-
# See the License for the specific language governing permissions and
|
16
|
-
# limitations under the License.
|
17
|
-
#
|
18
|
-
|
19
|
-
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
|
20
|
-
|
21
|
-
describe Mixlib::CLI do
|
22
|
-
after(:each) do
|
23
|
-
TestCLI.options = {}
|
24
|
-
TestCLI.banner("Usage: #{$0} (options)")
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "class method" do
|
28
|
-
describe "option" do
|
29
|
-
it "allows you to set a config option with a hash" do
|
30
|
-
expect(TestCLI.option(:config_file, short: "-c CONFIG")).to eql({ short: "-c CONFIG" })
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "options" do
|
35
|
-
it "returns the current options hash" do
|
36
|
-
TestCLI.option(:config_file, short: "-c CONFIG")
|
37
|
-
expect(TestCLI.options).to eql({ config_file: { short: "-c CONFIG" } })
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "options=" do
|
42
|
-
it "allows you to set the full options with a single hash" do
|
43
|
-
TestCLI.options = { config_file: { short: "-c CONFIG" } }
|
44
|
-
expect(TestCLI.options).to eql({ config_file: { short: "-c CONFIG" } })
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe "banner" do
|
49
|
-
it "has a default value" do
|
50
|
-
expect(TestCLI.banner).to match(/^Usage: (.+) \(options\)$/)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "allows you to set the banner" do
|
54
|
-
TestCLI.banner("Usage: foo")
|
55
|
-
expect(TestCLI.banner).to eql("Usage: foo")
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context "when configured with default single-config-hash behavior" do
|
61
|
-
|
62
|
-
before(:each) do
|
63
|
-
@cli = TestCLI.new
|
64
|
-
end
|
65
|
-
|
66
|
-
describe "initialize" do
|
67
|
-
it "sets the banner to the class defined banner" do
|
68
|
-
expect(@cli.banner).to eql(TestCLI.banner)
|
69
|
-
end
|
70
|
-
|
71
|
-
it "sets the options to the class defined options, plus defaults" do
|
72
|
-
TestCLI.option(:config_file, short: "-l LOG")
|
73
|
-
cli = TestCLI.new
|
74
|
-
expect(cli.options).to eql({
|
75
|
-
config_file: {
|
76
|
-
short: "-l LOG",
|
77
|
-
on: :on,
|
78
|
-
boolean: false,
|
79
|
-
required: false,
|
80
|
-
proc: nil,
|
81
|
-
show_options: false,
|
82
|
-
exit: nil,
|
83
|
-
in: nil,
|
84
|
-
},
|
85
|
-
})
|
86
|
-
end
|
87
|
-
|
88
|
-
it "sets the default config value for any options that include it" do
|
89
|
-
TestCLI.option(:config_file, short: "-l LOG", default: :debug)
|
90
|
-
@cli = TestCLI.new
|
91
|
-
expect(@cli.config[:config_file]).to eql(:debug)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe "opt_parser" do
|
96
|
-
|
97
|
-
it "sets the banner in opt_parse" do
|
98
|
-
expect(@cli.opt_parser.banner).to eql(@cli.banner)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "presents the arguments in the banner" do
|
102
|
-
TestCLI.option(:config_file, short: "-l LOG")
|
103
|
-
@cli = TestCLI.new
|
104
|
-
expect(@cli.opt_parser.to_s).to match(/-l LOG/)
|
105
|
-
end
|
106
|
-
|
107
|
-
it "honors :on => :tail options in the banner" do
|
108
|
-
TestCLI.option(:config_file, short: "-l LOG")
|
109
|
-
TestCLI.option(:help, short: "-h", boolean: true, on: :tail)
|
110
|
-
@cli = TestCLI.new
|
111
|
-
expect(@cli.opt_parser.to_s.split("\n").last).to match(/-h/)
|
112
|
-
end
|
113
|
-
|
114
|
-
it "honors :on => :head options in the banner" do
|
115
|
-
TestCLI.option(:config_file, short: "-l LOG")
|
116
|
-
TestCLI.option(:help, short: "-h", boolean: true, on: :head)
|
117
|
-
@cli = TestCLI.new
|
118
|
-
expect(@cli.opt_parser.to_s.split("\n")[1]).to match(/-h/)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "presents the arguments in alphabetical order in the banner" do
|
122
|
-
TestCLI.option(:alpha, short: "-a ALPHA")
|
123
|
-
TestCLI.option(:beta, short: "-b BETA")
|
124
|
-
TestCLI.option(:zeta, short: "-z ZETA")
|
125
|
-
@cli = TestCLI.new
|
126
|
-
output_lines = @cli.opt_parser.to_s.split("\n")
|
127
|
-
expect(output_lines[1]).to match(/-a ALPHA/)
|
128
|
-
expect(output_lines[2]).to match(/-b BETA/)
|
129
|
-
expect(output_lines[3]).to match(/-z ZETA/)
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
|
134
|
-
describe "parse_options" do
|
135
|
-
it "sets the corresponding config value for non-boolean arguments" do
|
136
|
-
TestCLI.option(:config_file, short: "-c CONFIG")
|
137
|
-
@cli = TestCLI.new
|
138
|
-
@cli.parse_options([ "-c", "foo.rb" ])
|
139
|
-
expect(@cli.config[:config_file]).to eql("foo.rb")
|
140
|
-
end
|
141
|
-
|
142
|
-
it "sets the corresponding config value according to a supplied proc" do
|
143
|
-
TestCLI.option(:number,
|
144
|
-
short: "-n NUMBER",
|
145
|
-
proc: Proc.new { |config| config.to_i + 2 }
|
146
|
-
)
|
147
|
-
@cli = TestCLI.new
|
148
|
-
@cli.parse_options([ "-n", "2" ])
|
149
|
-
expect(@cli.config[:number]).to eql(4)
|
150
|
-
end
|
151
|
-
|
152
|
-
it "passes the existing value to two-argument procs" do
|
153
|
-
TestCLI.option(:number,
|
154
|
-
short: "-n NUMBER",
|
155
|
-
proc: Proc.new { |value, existing| existing ||= []; existing << value; existing }
|
156
|
-
)
|
157
|
-
@cli = TestCLI.new
|
158
|
-
@cli.parse_options([ "-n", "2", "-n", "3" ])
|
159
|
-
expect(@cli.config[:number]).to eql(%w{2 3})
|
160
|
-
end
|
161
|
-
|
162
|
-
it "sets the corresponding config value to true for boolean arguments" do
|
163
|
-
TestCLI.option(:i_am_boolean, short: "-i", boolean: true)
|
164
|
-
@cli = TestCLI.new
|
165
|
-
@cli.parse_options([ "-i" ])
|
166
|
-
expect(@cli.config[:i_am_boolean]).to be true
|
167
|
-
end
|
168
|
-
|
169
|
-
it "sets the corresponding config value to false when a boolean is prefixed with --no" do
|
170
|
-
TestCLI.option(:i_am_boolean, long: "--[no-]bool", boolean: true)
|
171
|
-
@cli = TestCLI.new
|
172
|
-
@cli.parse_options([ "--no-bool" ])
|
173
|
-
expect(@cli.config[:i_am_boolean]).to be false
|
174
|
-
end
|
175
|
-
|
176
|
-
it "exits if a config option has :exit set" do
|
177
|
-
TestCLI.option(:i_am_exit, short: "-x", boolean: true, exit: 0)
|
178
|
-
@cli = TestCLI.new
|
179
|
-
expect(lambda { @cli.parse_options(["-x"]) }).to raise_error(SystemExit)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "exits if a required option is missing" do
|
183
|
-
TestCLI.option(:require_me, short: "-r", boolean: true, required: true)
|
184
|
-
@cli = TestCLI.new
|
185
|
-
expect(lambda { @cli.parse_options([]) }).to raise_error(SystemExit)
|
186
|
-
end
|
187
|
-
|
188
|
-
it "exits if option is not included in the list and required" do
|
189
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: true)
|
190
|
-
@cli = TestCLI.new
|
191
|
-
expect(lambda { @cli.parse_options(["-i", "three"]) }).to raise_error(SystemExit)
|
192
|
-
end
|
193
|
-
|
194
|
-
it "exits if option is not included in the list and not required" do
|
195
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: false)
|
196
|
-
@cli = TestCLI.new
|
197
|
-
expect(lambda { @cli.parse_options(["-i", "three"]) }).to raise_error(SystemExit)
|
198
|
-
end
|
199
|
-
|
200
|
-
it "doesn't exit if option is nil and not required" do
|
201
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: false)
|
202
|
-
@cli = TestCLI.new
|
203
|
-
expect do
|
204
|
-
expect(@cli.parse_options([])).to eql []
|
205
|
-
end.to_not raise_error
|
206
|
-
end
|
207
|
-
|
208
|
-
it "exit if option is nil and required" do
|
209
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: true)
|
210
|
-
@cli = TestCLI.new
|
211
|
-
expect(lambda { @cli.parse_options([]) }).to raise_error(SystemExit)
|
212
|
-
end
|
213
|
-
|
214
|
-
it "raises ArgumentError if options key :in is not an array" do
|
215
|
-
TestCLI.option(:inclusion, short: "-i val", in: "foo", required: true)
|
216
|
-
@cli = TestCLI.new
|
217
|
-
expect(lambda { @cli.parse_options(["-i", "three"]) }).to raise_error(ArgumentError)
|
218
|
-
end
|
219
|
-
|
220
|
-
it "doesn't exit if option is included in the list" do
|
221
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, required: true)
|
222
|
-
@cli = TestCLI.new
|
223
|
-
@cli.parse_options(["-i", "one"])
|
224
|
-
expect(@cli.config[:inclusion]).to eql("one")
|
225
|
-
end
|
226
|
-
|
227
|
-
it "changes description if :in key is specified" do
|
228
|
-
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, description: "desc", required: false)
|
229
|
-
@cli = TestCLI.new
|
230
|
-
@cli.parse_options(["-i", "one"])
|
231
|
-
expect(@cli.options[:inclusion][:description]).to eql("desc (included in ['one', 'two'])")
|
232
|
-
end
|
233
|
-
|
234
|
-
it "doesn't exit if a required option is specified" do
|
235
|
-
TestCLI.option(:require_me, short: "-r", boolean: true, required: true)
|
236
|
-
@cli = TestCLI.new
|
237
|
-
@cli.parse_options(["-r"])
|
238
|
-
expect(@cli.config[:require_me]).to be true
|
239
|
-
end
|
240
|
-
|
241
|
-
it "doesn't exit if a required boolean option is specified and false" do
|
242
|
-
TestCLI.option(:require_me, long: "--[no-]req", boolean: true, required: true)
|
243
|
-
@cli = TestCLI.new
|
244
|
-
@cli.parse_options(["--no-req"])
|
245
|
-
expect(@cli.config[:require_me]).to be false
|
246
|
-
end
|
247
|
-
|
248
|
-
it "doesn't exit if a required option is specified and empty" do
|
249
|
-
TestCLI.option(:require_me, short: "-r VALUE", required: true)
|
250
|
-
@cli = TestCLI.new
|
251
|
-
@cli.parse_options(["-r", ""])
|
252
|
-
expect(@cli.config[:require_me]).to eql("")
|
253
|
-
end
|
254
|
-
|
255
|
-
it "preserves all of the commandline arguments, ARGV" do
|
256
|
-
TestCLI.option(:config_file, short: "-c CONFIG")
|
257
|
-
@cli = TestCLI.new
|
258
|
-
argv_old = ARGV.dup
|
259
|
-
ARGV.replace ["-c", "foo.rb"]
|
260
|
-
@cli.parse_options()
|
261
|
-
expect(ARGV).to eql(["-c", "foo.rb"])
|
262
|
-
ARGV.replace argv_old
|
263
|
-
end
|
264
|
-
|
265
|
-
it "preserves and return any un-parsed elements" do
|
266
|
-
TestCLI.option(:party, short: "-p LOCATION")
|
267
|
-
@cli = TestCLI.new
|
268
|
-
expect(@cli.parse_options([ "easy", "-p", "opscode", "hard" ])).to eql(%w{easy hard})
|
269
|
-
expect(@cli.cli_arguments).to eql(%w{easy hard})
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
context "when configured to separate default options" do
|
275
|
-
before do
|
276
|
-
TestCLI.use_separate_default_options true
|
277
|
-
TestCLI.option(:defaulter, short: "-D SOMETHING", default: "this is the default")
|
278
|
-
@cli = TestCLI.new
|
279
|
-
end
|
280
|
-
|
281
|
-
it "sets default values on the `default` hash" do
|
282
|
-
@cli.parse_options([])
|
283
|
-
expect(@cli.default_config[:defaulter]).to eql("this is the default")
|
284
|
-
expect(@cli.config[:defaulter]).to be_nil
|
285
|
-
end
|
286
|
-
|
287
|
-
it "sets parsed values on the `config` hash" do
|
288
|
-
@cli.parse_options(%w{-D not-default})
|
289
|
-
expect(@cli.default_config[:defaulter]).to eql("this is the default")
|
290
|
-
expect(@cli.config[:defaulter]).to eql("not-default")
|
291
|
-
end
|
292
|
-
|
293
|
-
end
|
294
|
-
|
295
|
-
context "when subclassed" do
|
296
|
-
before do
|
297
|
-
TestCLI.options = { arg1: { boolean: true } }
|
298
|
-
end
|
299
|
-
|
300
|
-
it "retains previously defined options from parent" do
|
301
|
-
class T1 < TestCLI
|
302
|
-
option :arg2, boolean: true
|
303
|
-
end
|
304
|
-
expect(T1.options[:arg1]).to be_a(Hash)
|
305
|
-
expect(T1.options[:arg2]).to be_a(Hash)
|
306
|
-
expect(TestCLI.options[:arg2]).to be_nil
|
307
|
-
end
|
308
|
-
|
309
|
-
it "isn't able to modify parent classes options" do
|
310
|
-
class T2 < TestCLI
|
311
|
-
option :arg2, boolean: true
|
312
|
-
end
|
313
|
-
T2.options[:arg1][:boolean] = false
|
314
|
-
expect(T2.options[:arg1][:boolean]).to be false
|
315
|
-
expect(TestCLI.options[:arg1][:boolean]).to be true
|
316
|
-
end
|
317
|
-
|
318
|
-
it "passes its options onto child" do
|
319
|
-
class T3 < TestCLI
|
320
|
-
option :arg2, boolean: true
|
321
|
-
end
|
322
|
-
class T4 < T3
|
323
|
-
option :arg3, boolean: true
|
324
|
-
end
|
325
|
-
3.times do |i|
|
326
|
-
expect(T4.options["arg#{i + 1}".to_sym]).to be_a(Hash)
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
it "also works with an option that's an array" do
|
331
|
-
class T5 < TestCLI
|
332
|
-
option :arg2, default: []
|
333
|
-
end
|
334
|
-
|
335
|
-
class T6 < T5
|
336
|
-
end
|
337
|
-
|
338
|
-
expect(T6.options[:arg2]).to be_a(Hash)
|
339
|
-
end
|
340
|
-
|
341
|
-
end
|
342
|
-
|
343
|
-
end
|
344
|
-
|
345
|
-
# option :config_file,
|
346
|
-
# :short => "-c CONFIG",
|
347
|
-
# :long => "--config CONFIG",
|
348
|
-
# :default => 'config.rb',
|
349
|
-
# :description => "The configuration file to use"
|
350
|
-
#
|
351
|
-
# option :log_level,
|
352
|
-
# :short => "-l LEVEL",
|
353
|
-
# :long => "--log_level LEVEL",
|
354
|
-
# :description => "Set the log level (debug, info, warn, error, fatal)",
|
355
|
-
# :required => true,
|
356
|
-
# :proc => nil
|
357
|
-
#
|
358
|
-
# option :help,
|
359
|
-
# :short => "-h",
|
360
|
-
# :long => "--help",
|
361
|
-
# :description => "Show this message",
|
362
|
-
# :on => :tail,
|
363
|
-
# :boolean => true,
|
364
|
-
# :show_options => true,
|
365
|
-
# :exit => 0
|
data/spec/spec_helper.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
$TESTING = true
|
2
|
-
$:.push File.join(File.dirname(__FILE__), "..", "lib")
|
3
|
-
|
4
|
-
require "mixlib/cli"
|
5
|
-
|
6
|
-
class TestCLI
|
7
|
-
include Mixlib::CLI
|
8
|
-
end
|
9
|
-
|
10
|
-
RSpec.configure do |config|
|
11
|
-
# Use documentation format
|
12
|
-
config.formatter = "doc"
|
13
|
-
|
14
|
-
# Use color in STDOUT
|
15
|
-
config.color = true
|
16
|
-
|
17
|
-
# Use color not only in STDOUT but also in pagers and files
|
18
|
-
config.tty = true
|
19
|
-
|
20
|
-
# run the examples in random order
|
21
|
-
config.order = :rand
|
22
|
-
|
23
|
-
config.filter_run focus: true
|
24
|
-
config.run_all_when_everything_filtered = true
|
25
|
-
config.warnings = true
|
26
|
-
end
|