Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add enum to string arguments

  • Loading branch information...
commit 0150b86c172838c51abc4f7b7992ed8be051dca5 1 parent 5523b5f
Erik Michaels-Ober sferik authored
1  lib/thor/base.rb
View
@@ -508,6 +508,7 @@ def print_options(shell, options, group_name=nil)
list << item
list << [ "", "# Default: #{option.default}" ] if option.show_default?
+ list << [ "", "# Possible values: #{option.enum.join(', ')}" ] if option.enum
end
end
9 lib/thor/parser/argument.rb
View
@@ -2,7 +2,7 @@ class Thor
class Argument #:nodoc:
VALID_TYPES = [ :numeric, :hash, :array, :string ]
- attr_reader :name, :description, :required, :type, :default, :banner
+ attr_reader :name, :description, :enum, :required, :type, :default, :banner
alias :human_name :name
def initialize(name, options={})
@@ -19,6 +19,7 @@ def initialize(name, options={})
@type = (type || :string).to_sym
@default = options[:default]
@banner = options[:banner] || default_banner
+ @enum = options[:enum]
validate! # Trigger specific validations
end
@@ -43,7 +44,11 @@ def show_default?
protected
def validate!
- raise ArgumentError, "An argument cannot be required and have default value." if required? && !default.nil?
+ if required? && !default.nil?
+ raise ArgumentError, "An argument cannot be required and have default value."
+ elsif @enum && !@enum.is_a?(Array)
+ raise ArgumentError, "An argument cannot have an enum other than an array."
+ end
end
def valid_type?(type)
8 lib/thor/parser/arguments.rb
View
@@ -144,7 +144,13 @@ def parse_string(name)
if no_or_skip?(name)
nil
else
- shift
+ value = shift
+ if @switches.is_a?(Hash) && switch = @switches[name]
+ if switch.enum && !switch.enum.include?(value)
+ raise MalformattedArgumentError, "Expected '#{name}' to be one of #{switch.enum.join(', ')}; got #{value}"
+ end
+ end
+ value
end
end
5 spec/base_spec.rb
View
@@ -138,6 +138,11 @@ def hello
content.should =~ /Foo options\:/
content.should =~ /--last-name=LAST_NAME/
end
+
+ it "displays choices for enums" do
+ content = capture(:stdout) { Enum.help(Thor::Base.shell.new) }
+ content.should =~ /Possible values\: apple, banana/
+ end
end
describe "#namespace" do
10 spec/fixtures/enum.thor
View
@@ -0,0 +1,10 @@
+class Enum < Thor::Group
+ include Thor::Actions
+
+ desc "snack"
+ class_option "fruit", :aliases => "-f", :type => :string, :enum => %w(apple banana)
+ def snack
+ puts options['fruit']
+ end
+
+end
6 spec/parser/argument_spec.rb
View
@@ -25,6 +25,12 @@ def argument(name, options={})
argument(:task, :type => :string, :default => "bar", :required => true)
}.should raise_error(ArgumentError, "An argument cannot be required and have default value.")
end
+
+ it "raises an error if enum isn't an array" do
+ lambda {
+ argument(:task, :type => :string, :enum => "bar")
+ }.should raise_error(ArgumentError, "An argument cannot have an enum other than an array.")
+ end
end
describe "#usage" do
1  spec/spec_helper.rb
View
@@ -27,6 +27,7 @@
load File.join(File.dirname(__FILE__), "fixtures", "group.thor")
load File.join(File.dirname(__FILE__), "fixtures", "script.thor")
load File.join(File.dirname(__FILE__), "fixtures", "invoke.thor")
+load File.join(File.dirname(__FILE__), "fixtures", "enum.thor")
RSpec.configure do |config|
config.before do
Please sign in to comment.
Something went wrong with that request. Please try again.