summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Rogers <tim@gocardless.com>2015-06-13 18:40:32 +0100
committerTim Rogers <tim@gocardless.com>2015-06-13 18:40:32 +0100
commitfd0e2d8bc3d1c647e6b051f48b8bedbe77934c0c (patch)
tree38105cadafc1b4f0609121b1d2f043af46ed4425
parent1a075c05d38be0df849119530378043994c05c91 (diff)
downloadslop-fd0e2d8bc3d1c647e6b051f48b8bedbe77934c0c.tar.gz
Better handling of option names with multiple words
-rw-r--r--README.md12
-rw-r--r--lib/slop/option.rb2
-rw-r--r--lib/slop/result.rb2
-rw-r--r--test/option_test.rb4
-rw-r--r--test/result_test.rb20
5 files changed, 27 insertions, 13 deletions
diff --git a/README.md b/README.md
index 7a46538..77045a4 100644
--- a/README.md
+++ b/README.md
@@ -22,19 +22,21 @@ opts = Slop.parse do |o|
o.integer '--port', 'custom port', default: 80
o.bool '-v', '--verbose', 'enable verbose mode'
o.bool '-q', '--quiet', 'suppress output (quiet mode)'
+ o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host'
o.on '--version', 'print the version' do
puts Slop::VERSION
exit
end
end
-ARGV #=> -v --host 192.168.0.1
+ARGV #=> -v --host 192.168.0.1 --check-ssl-certificate
-opts[:host] #=> 192.168.0.1
-opts.verbose? #=> true
-opts.quiet? #=> false
+opts[:host] #=> 192.168.0.1
+opts.verbose? #=> true
+opts.quiet? #=> false
+opts.check_ssl_certificate? #=> true
-opts.to_hash #=> { host: "192.168.0.1", port: 80, verbose: true, quiet: false }
+opts.to_hash #=> { host: "192.168.0.1", port: 80, verbose: true, quiet: false, check_ssl_certificate: true }
```
Option types
diff --git a/lib/slop/option.rb b/lib/slop/option.rb
index 4fd2ae0..2164f1d 100644
--- a/lib/slop/option.rb
+++ b/lib/slop/option.rb
@@ -102,7 +102,7 @@ module Slop
# Returns the last key as a symbol. Used in Options.to_hash.
def key
- (config[:key] || flags.last.sub(/\A--?/, '')).to_sym
+ (config[:key] || flags.last.sub(/\A--?/, '')).tr("-", "_").to_sym
end
# Returns true if this option should be displayed in help text.
diff --git a/lib/slop/result.rb b/lib/slop/result.rb
index eaad707..6f56248 100644
--- a/lib/slop/result.rb
+++ b/lib/slop/result.rb
@@ -33,7 +33,7 @@ module Slop
# Returns an Option if it exists. Ignores any prefixed hyphens.
def option(flag)
- cleaned = -> (f) { f.to_s.sub(/\A--?/, '') }
+ cleaned = -> (f) { f.to_s.sub(/\A--?/, '').tr('_', '-') }
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 399d5c5..78fd55e 100644
--- a/test/option_test.rb
+++ b/test/option_test.rb
@@ -17,6 +17,10 @@ describe Slop::Option do
assert_equal :foo, option(%w(-f --foo), nil).key
end
+ it "converts dashes to underscores to make multi-word options symbol-friendly" do
+ assert_equal :foo_bar, option(%w(-f --foo-bar), nil).key
+ end
+
it "can be overridden" do
assert_equal :bar, option(%w(-f --foo), nil, key: "bar").key
end
diff --git a/test/result_test.rb b/test/result_test.rb
index 760f460..0efdfff 100644
--- a/test/result_test.rb
+++ b/test/result_test.rb
@@ -12,16 +12,18 @@ end
describe Slop::Result do
before do
- @options = Slop::Options.new
- @verbose = @options.bool "-v", "--verbose"
- @name = @options.string "--name"
- @unused = @options.string "--unused"
- @result = @options.parse %w(foo -v --name lee argument)
+ @options = Slop::Options.new
+ @verbose = @options.bool "-v", "--verbose"
+ @name = @options.string "--name"
+ @unused = @options.string "--unused"
+ @long_option = @options.string "--long-option"
+ @result = @options.parse %w(foo -v --name lee --long-option bar argument)
end
it "increments option count" do
# test this here so it's more "full stack"
assert_equal 1, @verbose.count
+ assert_equal 1, @long_option.count
@result.parser.parse %w(-v --verbose)
assert_equal 2, @verbose.count
end
@@ -51,6 +53,9 @@ describe Slop::Result do
assert_equal "lee", @result["name"]
assert_equal "lee", @result[:name]
assert_equal "lee", @result["--name"]
+ assert_equal "bar", @result["long_option"]
+ assert_equal "bar", @result[:long_option]
+ assert_equal "bar", @result["--long-option"]
end
end
@@ -72,6 +77,7 @@ describe Slop::Result do
it "checks if options have been used" do
assert_equal true, @result.verbose?
assert_equal false, @result.unused?
+ assert_equal true, @result.long_option?
end
end
@@ -79,6 +85,7 @@ describe Slop::Result do
it "returns an option by flag" do
assert_equal @verbose, @result.option("--verbose")
assert_equal @verbose, @result.option("-v")
+ assert_equal @long_option, @result.option("--long-option")
end
it "ignores prefixed hyphens" do
@@ -93,7 +100,8 @@ describe Slop::Result do
describe "#to_hash" do
it "returns option keys and values" do
- assert_equal({ verbose: true, name: "lee", unused: nil }, @result.to_hash)
+ assert_equal({ verbose: true, name: "lee", unused: nil, long_option: "bar" },
+ @result.to_hash)
end
end
end