summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent B <lbnetid+gh@gmail.com>2017-05-01 15:29:39 +0200
committerLee Jarvis <leejarvis@fastmail.com>2017-05-01 19:58:36 +0100
commit70e1de88583b934e583aad2b7c2bbffda4a4ea57 (patch)
tree02e8648ee6ac55b630910854e6e16b6475905295
parent725ab19327d548d931d8711298fdd5612d4d779e (diff)
downloadslop-lbriais-feature/add_config_for_underscore_switch.tar.gz
Add config option underscore_flagslbriais-feature/add_config_for_underscore_switch
The default behaviour of translation flags-with-dashes is to underscore them so they're symbol friendly. This config option allows us to avoid this translation and simply return the flags verbatim: opts = Slop.parse(underscore_flags: false) do |o| o.on "--foo-bar" end opts.to_hash #=> { :"foo-bar" => true } closes #204
-rw-r--r--lib/slop/option.rb10
-rw-r--r--lib/slop/options.rb7
-rw-r--r--lib/slop/result.rb6
-rw-r--r--test/option_test.rb4
4 files changed, 22 insertions, 5 deletions
diff --git a/lib/slop/option.rb b/lib/slop/option.rb
index 3e414f9..b8533ae 100644
--- a/lib/slop/option.rb
+++ b/lib/slop/option.rb
@@ -3,6 +3,7 @@ module Slop
DEFAULT_CONFIG = {
help: true,
tail: false,
+ underscore_flags: true,
}
# An Array of flags this option matches.
@@ -107,7 +108,14 @@ module Slop
# Returns the last key as a symbol. Used in Options.to_hash.
def key
- (config[:key] || flags.last.sub(/\A--?/, '')).tr("-", "_").to_sym
+ key = config[:key] || flags.last.sub(/\A--?/, '')
+ key = key.tr '-', '_' if underscore_flags?
+ key.to_sym
+ end
+
+ # Returns true if this option should be displayed with dashes transformed into underscores.
+ def underscore_flags?
+ config[:underscore_flags]
end
# Returns true if this option should be displayed in help text.
diff --git a/lib/slop/options.rb b/lib/slop/options.rb
index b739c5e..94fc5dd 100644
--- a/lib/slop/options.rb
+++ b/lib/slop/options.rb
@@ -3,9 +3,10 @@ module Slop
include Enumerable
DEFAULT_CONFIG = {
- suppress_errors: false,
- type: "null",
- banner: true,
+ suppress_errors: false,
+ type: "null",
+ banner: true,
+ underscore_flags: true,
}
# The Array of Option instances we've created.
diff --git a/lib/slop/result.rb b/lib/slop/result.rb
index 6f56248..469d05c 100644
--- a/lib/slop/result.rb
+++ b/lib/slop/result.rb
@@ -33,7 +33,11 @@ module Slop
# Returns an Option if it exists. Ignores any prefixed hyphens.
def option(flag)
- cleaned = -> (f) { f.to_s.sub(/\A--?/, '').tr('_', '-') }
+ cleaned = -> (f) do
+ key = f.to_s.sub(/\A--?/, '')
+ key = key.tr '-', '_' if parser.config[:underscore_flags]
+ key.to_sym
+ end
options.find do |o|
o.flags.any? { |f| cleaned.(f) == cleaned.(flag) }
end
diff --git a/test/option_test.rb b/test/option_test.rb
index 78fd55e..a8db657 100644
--- a/test/option_test.rb
+++ b/test/option_test.rb
@@ -21,6 +21,10 @@ describe Slop::Option do
assert_equal :foo_bar, option(%w(-f --foo-bar), nil).key
end
+ it "when specified, it won't convert dashes to underscores to make multi-word options symbol-friendly" do
+ assert_equal :'foo-bar', option(%w(-f --foo-bar), nil, underscore_flags: false).key
+ end
+
it "can be overridden" do
assert_equal :bar, option(%w(-f --foo), nil, key: "bar").key
end