lib/command_line/arguments.rb in wvanbergen-request-log-analyzer-0.2.2 vs lib/command_line/arguments.rb in wvanbergen-request-log-analyzer-0.3.0
- old
+ new
@@ -44,11 +44,11 @@
# Handle argument flags for the application
# <tt>flag</tt> A flag symbol like :fast
# Options
# * <tt>:expects</tt> Expects a value after the flag
- def flag(flag, options)
+ def flag(flag, options = {})
options[:expects] = String unless options.has_key?(:expects)
argument = Flag.new(flag, options)
@flag_definitions[argument.to_argument] = argument
@flag_definitions[argument.to_alias] = argument if argument.has_alias?
return argument
@@ -83,11 +83,18 @@
if @flag_definitions.has_key?(arg)
flag = @flag_definitions[arg]
if flag.expects_argument?
if @arguments.length > (i + 1) && @arguments[i + 1]
- @flags[flag.name] = @arguments[i + 1]
+
+ if flag.multiple?
+ @flags[flag.name] ||= []
+ @flags[flag.name] << @arguments[i + 1]
+ else
+ @flags[flag.name] = @arguments[i + 1]
+ end
+
i += 1
else
raise CommandLine::FlagExpectsArgument.new(arg)
end
@@ -113,9 +120,15 @@
end
# Check if the parsed arguments meet their requirements.
# Raises CommandLineexception on error.
def check_parsed_arguments!
+
+ @flag_definitions.each do |flag, definition|
+ @flags[definition.name] ||= [] if definition.multiple? && !definition.default?
+ @flags[definition.name] ||= definition.default if definition.default?
+ end
+
if @begins_with_command && @command.nil?
raise CommandLine::CommandMissing.new
end
if @required_files && @files.length < @required_files
\ No newline at end of file