lib/rcoli/model.rb in rcoli-0.5.7 vs lib/rcoli/model.rb in rcoli-0.5.8
- old
+ new
@@ -6,22 +6,26 @@
module RCoLi
module CommandContainer
+ setter :force
+
+
attr_accessor :parent
+
def action(&block)
@action = block
end
def get_action
@action
end
- def command(name, &block)
- obj = Command.new(name)
+ def command(nam, &block)
+ obj = Command.new(nam)
obj.parent = self
block.call(obj)
commands << obj
end
@@ -64,10 +68,11 @@
else
if (cmd = find_command(arg))
result.command = cmd
cmd.put_default_values(result)
cmd.parse_args(args, result)
+ cmd.validate_options(result, :options)
elsif (commands.empty?)
result.arguments << arg
else
raise InvalidCommand, "'#{arg}' is not a valid #{@name} command"
end
@@ -81,13 +86,24 @@
option.keys.each{|key| result.send(target)[key] = option.value_of_default_value}
end
end
def find_command(name)
- commands.find{|command| command.value_of_name.eql? name}
+ result = commands.find{|command| command.value_of_name.to_s.eql? name}
+ return result
end
+ def validate_options(result, target)
+ if (result.command.value_of_force == true)
+ return
+ else
+ self.options.find_all{|o| o.is_a? Flag and o.value_of_required}.each do |o|
+ raise InvalidCommand, "Required option '#{o.to_s}' is missing" unless o.keys.find{|key| result.send(target)[key]}
+ end
+ end
+ end
+
private
def find_option(name)
options.find{|opt| opt.correspond?(name)}
end
@@ -109,10 +125,14 @@
def keys
[@s_name, @l_name].compact
end
+ def to_s
+ keys.join(', ')
+ end
+
def help_keys
result = []
result << "-#{@s_name}" if @s_name
result << "--#{@l_name}" if @l_name
result
@@ -134,28 +154,23 @@
include Option
setter :default_value
setter :arg_name
+ setter :required
end
class Command
setter :name
setter :summary
setter :description
setter :syntax
+ setter :skip_pre
+ setter :skip_post
- def solitaire
- @solitaire = true
- end
-
- def solitaire?
- return true == @solitaire
- end
-
def initialize(name)
@name = name
end
include CommandContainer
@@ -184,28 +199,29 @@
def execute(args, context)
result = ParsedArgs.new
put_default_values(result)
parse_args(args, result)
+ validate_options(result, :global_options)
if result.command
# command has to have the action block
action = result.command.get_action
- raise "Invalid configuration. Missing action block." unless action
+ raise ApplicationError, "Invalid configuration. Missing action block." unless action
# enable/disable logging level DEBUG
if (result.global_options['debug'])
context.instance_exec do
log.level = Logger::DEBUG
end
end
# execution of the pre block
- context.instance_exec(result.global_options, result.options, result.arguments, &@pre_action) if (@pre_action and !result.command.solitaire?)
+ context.instance_exec(result.global_options, result.options, result.arguments, &@pre_action) if (@pre_action and !result.command.value_of_skip_pre)
# execution of the main block
context.instance_exec(result.global_options, result.options, result.arguments, &action)
# execution of the post block
- context.instance_exec(result.global_options, result.options, result.arguments, &@post_action) if (@post_action and !result.command.solitaire?)
+ context.instance_exec(result.global_options, result.options, result.arguments, &@post_action) if (@post_action and !result.command.value_of_skip_post)
else
say "This feature is comming soon. Now you should execute '#{value_of_name} help'"
end
end
\ No newline at end of file