Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of option names with multiple words #169

Merged
merged 1 commit into from
Jun 14, 2015
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/slop/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def flag

# 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.
Expand Down
2 changes: 1 addition & 1 deletion lib/slop/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def []=(flag, value)

# 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
Expand Down
4 changes: 4 additions & 0 deletions test/option_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def option(*args)
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
Expand Down
20 changes: 14 additions & 6 deletions test/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ def finish(result)

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
Expand Down Expand Up @@ -51,6 +53,9 @@ def finish(result)
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

Expand All @@ -72,13 +77,15 @@ def finish(result)
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

describe "#option" 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
Expand All @@ -93,7 +100,8 @@ def finish(result)

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