Browse files

Support strict_args_position! for backwards compatibility

  • Loading branch information...
1 parent cd6c411 commit de6b9a4aae9c00fc22ef69679255079ba3d9117e @josevalim josevalim committed Jun 18, 2012
Showing with 39 additions and 4 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +20 −4 lib/thor/base.rb
  3. +15 −0 spec/thor_spec.rb
View
4 CHANGELOG.rdoc
@@ -1,3 +1,7 @@
+== 0.15.3, release 2012-06-18
+* Support strict_args_position! for backwards compatibility
+* Escape Dir glob characters in paths
+
== 0.15.2, released 2012-05-07
* Added print_in_columns
* Exposed terminal_width as a public API
View
24 lib/thor/base.rb
@@ -70,11 +70,12 @@ def initialize(args=[], options={}, config={})
# arguments declared using #argument (this is primarily used
# by Thor::Group). Tis will leave us with the remaining
# positional arguments.
- thor_args = Thor::Arguments.new(self.class.arguments)
- thor_args.parse(args + opts.remaining).each { |k,v| send("#{k}=", v) }
- args = thor_args.remaining
+ to_parse = args
+ to_parse += opts.remaining unless self.class.strict_args_position?(config)
- @args = args
+ thor_args = Thor::Arguments.new(self.class.arguments)
+ thor_args.parse(to_parse).each { |k,v| send("#{k}=", v) }
+ @args = thor_args.remaining
end
class << self
@@ -141,6 +142,21 @@ def check_unknown_options?(config) #:nodoc:
!!check_unknown_options
end
+ # If you want only strict string args (useful when cascading thor classes),
+ # call strict_args_position! This is disabled by default to allow dynamic
+ # invocations.
+ def strict_args_position!
+ @strict_args_position = true
+ end
+
+ def strict_args_position #:nodoc:
+ @strict_args_position ||= from_superclass(:strict_args_position, false)
+ end
+
+ def strict_args_position?(config) #:nodoc:
+ !!strict_args_position
+ end
+
# Adds an argument to the class and creates an attr_accessor for it.
#
# Arguments are different from options in several aspects. The first one
View
15 spec/thor_spec.rb
@@ -382,6 +382,21 @@ def unknown(*args)
end
klass.start(["unknown", "foo", "--bar", "baz", "bat", "--bam"]).should == ["foo", "--bar", "baz", "bat", "--bam"]
+ klass.start(["unknown", "--bar", "baz"]).should == ["--bar", "baz"]
+ end
+
+ it "does not pass through unknown options with strict args" do
+ klass = Class.new(Thor) do
+ strict_args_position!
+
+ desc "unknown", "passing unknown options"
+ def unknown(*args)
+ args
+ end
+ end
+
+ klass.start(["unknown", "--bar", "baz"]).should == []
+ klass.start(["unknown", "foo", "--bar", "baz"]).should == ["foo"]
end
end
end

0 comments on commit de6b9a4

Please sign in to comment.