Skip to content

Instantly share code, notes, and snippets.

@ccbcreg
Created September 17, 2010 20:56
Show Gist options
  • Save ccbcreg/584931 to your computer and use it in GitHub Desktop.
Save ccbcreg/584931 to your computer and use it in GitHub Desktop.
---
lib/thor/parser/options.rb | 8 +++++++-
spec/parser/options_spec.rb | 18 ++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/lib/thor/parser/options.rb b/lib/thor/parser/options.rb
index 2315b13..d17f8e1 100644
--- a/lib/thor/parser/options.rb
+++ b/lib/thor/parser/options.rb
@@ -50,7 +50,7 @@ class Thor
end
def parse(args)
- @pile = args.dup
+ @pile = except_underscores(args.dup)
while peek
if current_is_switch?
@@ -169,6 +169,12 @@ class Thor
@non_assigned_required.delete(option)
send(:"parse_#{option.type}", switch)
end
+
+ # Except underscores in commandline switches
+ def except_underscores(args)
+ args.map {|x| x =~ /^--/ ? x.gsub('_', '-') : x }
+ end
+
end
end
diff --git a/spec/parser/options_spec.rb b/spec/parser/options_spec.rb
index ecf2020..e15337a 100644
--- a/spec/parser/options_spec.rb
+++ b/spec/parser/options_spec.rb
@@ -47,6 +47,11 @@ describe Thor::Options do
it "accepts hashes" do
Thor::Options.to_switches(:count => {:a => :b}).must == "--count a:b"
end
+
+ it "accepts underscored options" do
+ Thor::Options.to_switches(:under_score_option => "foo bar").must == '--under_score_option "foo bar"'
+ end
+
end
describe "#parse" do
@@ -115,6 +120,19 @@ describe Thor::Options do
parse("--asdf---asdf", "baz", "--foo", "--asdf---dsf--asdf").must == {"foo" => "--asdf---dsf--asdf"}
check_unknown!
end
+
+ it "excepts underscores in commandline args hash for boolean" do
+ create :foo_bar => :boolean
+ parse("--foo_bar")["foo_bar"].must == true
+ parse("--no_foo_bar")["foo_bar"].must == false
+ end
+
+ it "excepts underscores in commandline args hash for strings" do
+ create :foo_bar => :string, :baz_foo => :string
+ parse("--foo_bar", "baz")["foo_bar"].must == "baz"
+ parse("--baz_foo", "foo bar")["baz_foo"].must == "foo bar"
+ end
+
describe "with no input" do
it "and no switches returns an empty hash" do
--
1.6.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment