Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/mixlib/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc.
# Copyright:: Copyright (c) 2008-2019 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -243,7 +243,7 @@ def parse_options(argv = ARGV)
end
if config[opt_key] && !opt_value[:in].include?(config[opt_key])
reqarg = opt_value[:short] || opt_value[:long]
puts "#{reqarg}: #{config[opt_key]} is not included in the list ['#{opt_value[:in].join("', '")}'] "
puts "#{reqarg}: #{config[opt_key]} is not one of the allowed values: #{friendly_opt_list(opt_value[:in])}"
puts @opt_parser
exit 2
end
Expand Down Expand Up @@ -313,14 +313,24 @@ def build_option_arguments(opt_setting)
if opt_setting.key?(:description)
description = opt_setting[:description].dup
description << " (required)" if opt_setting[:required]
description << " (valid options are: ['#{opt_setting[:in].join("', '")}'])" if opt_setting[:in]
description << " (valid options: #{friendly_opt_list(opt_setting[:in])})" if opt_setting[:in]
opt_setting[:description] = description
arguments << description
end

arguments
end

# @private
# @param opt_arry [Array]
#
# @return [String] a friendly quoted list of items complete with "or"
def friendly_opt_list(opt_array)
opts = opt_array.map { |x| "'#{x}'" }
return opts.join(" or ") if opts.size < 3
opts[0..-2].join(", ") + ", or " + opts[-1]
end

def self.included(receiver)
receiver.extend(Mixlib::CLI::ClassMethods)
receiver.extend(Mixlib::CLI::InheritMethods)
Expand Down
18 changes: 16 additions & 2 deletions spec/mixlib/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,25 @@
expect(@cli.config[:inclusion]).to eql("one")
end

it "changes description if :in key is specified" do
it "changes description if :in key is specified with a single value" do
TestCLI.option(:inclusion, short: "-i val", in: %w{one}, description: "desc", required: false)
@cli = TestCLI.new
@cli.parse_options(["-i", "one"])
expect(@cli.options[:inclusion][:description]).to eql("desc (valid options: 'one')")
end

it "changes description if :in key is specified with 2 values" do
TestCLI.option(:inclusion, short: "-i val", in: %w{one two}, description: "desc", required: false)
@cli = TestCLI.new
@cli.parse_options(["-i", "one"])
expect(@cli.options[:inclusion][:description]).to eql("desc (valid options are: ['one', 'two'])")
expect(@cli.options[:inclusion][:description]).to eql("desc (valid options: 'one' or 'two')")
end

it "changes description if :in key is specified with 3 values" do
TestCLI.option(:inclusion, short: "-i val", in: %w{one two three}, description: "desc", required: false)
@cli = TestCLI.new
@cli.parse_options(["-i", "one"])
expect(@cli.options[:inclusion][:description]).to eql("desc (valid options: 'one', 'two', or 'three')")
end

it "doesn't exit if a required option is specified" do
Expand Down