lib/mercenary/command.rb in mercenary-0.1.0 vs lib/mercenary/command.rb in mercenary-0.2.0
- old
+ new
@@ -43,10 +43,29 @@
def description(desc = nil)
@description = desc if desc
@description
end
+ # Public: Sets the default command
+ #
+ # command_name - the command name to be executed in the event no args are
+ # present
+ #
+ # Returns the default command if there is one, `nil` otherwise
+ def default_command(command_name = nil)
+ if command_name
+ if commands.has_key?(command_name)
+ @default_command = commands[command_name] if command_name
+ @default_command
+ else
+ raise ArgumentError.new("'#{command_name}' couldn't be found in this command's list of commands.")
+ end
+ else
+ @default_command
+ end
+ end
+
# Public: Adds an option switch
#
# sym - the variable key which is used to identify the value of the switch
# at runtime in the options hash
#
@@ -136,9 +155,23 @@
def process_options(opts, config)
options.each do |o|
opts.on(*o) do |x|
config[map[o[0]]] = x
end
+ end
+ end
+
+ # Public: Execute all actions given the inputted args and options
+ #
+ # argv - (optional) command-line args (sans opts)
+ # config - (optional) the Hash configuration of string key to value
+ #
+ # Returns nothing
+ def execute(argv = [], config = {})
+ if actions.empty? && !default_command.nil?
+ default_command.execute
+ else
+ actions.each { |a| a.call(argv, config) }
end
end
# Public: Check if this command has a subcommand
#