summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-05-13 19:55:35 -0700
committerGitHub <noreply@github.com>2019-05-13 19:55:35 -0700
commite4e22454f74f30cd748eeedef0c2310f04cf92d2 (patch)
tree60688e2a7af84fd5e298420d78deb1a26ab57559 /lib
parent5601bd1811b6182b93681bd30c286e69fc52597f (diff)
parent32ee9105705e3484ddcb2953ed8675c50d5035e7 (diff)
downloadmixlib-cli-e4e22454f74f30cd748eeedef0c2310f04cf92d2.tar.gz
Merge pull request #60 from chef/better_in
Print out human readable lists of allowed CLI options
Diffstat (limited to 'lib')
-rw-r--r--lib/mixlib/cli.rb16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/mixlib/cli.rb b/lib/mixlib/cli.rb
index cfc642d..f3598e6 100644
--- a/lib/mixlib/cli.rb
+++ b/lib/mixlib/cli.rb
@@ -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");
@@ -243,7 +243,7 @@ module Mixlib
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
@@ -313,7 +313,7 @@ module Mixlib
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
@@ -321,6 +321,16 @@ module Mixlib
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)