Class: Caty
Overview
Handles command line parsing.
Subclass this and add public methods. These methods will become tasks, which will be callable via
app method_name
Use the #global_options() and #task_options() methods to add global or task-specific options.
Use the ::map() method to create aliases for tasks.
Use the ::start!() method to start parsing.
Use ::default() to set a default task.
Use ::before() and ::after() to define Rails-style before and after filters.
Defined Under Namespace
Modules: HasDescription, HelpSystem, Helpers Classes: BooleanConverter, Converter, GlobalOption, IntegerConverter, NoSuchTaskError, Option, OptionArgumentError, OptionArray, OptionConstructor, StringConverter, Task, TaskHash
Instance Attribute Summary collapse
-
#global_options ⇒ Object
Returns the global options for this invocation as an OpenHash.
-
#task_options ⇒ Object
Returns the options for the called task as an OpenHash.
Class Method Summary collapse
-
.append(&block) ⇒ Object
Simply class_evals the given block on your Caty subtype, thus allowing you to add new tasks in different source files.
-
.start!(args = ARGV) ⇒ Object
Starts command line parsing.
Instance Attribute Details
#global_options ⇒ Object
Returns the global options for this invocation as an OpenHash.
47 48 49 |
# File 'lib/caty.rb', line 47 def @global_options end |
#task_options ⇒ Object
Returns the options for the called task as an OpenHash.
41 42 43 |
# File 'lib/caty.rb', line 41 def @task_options end |
Class Method Details
.append(&block) ⇒ Object
Simply class_evals the given block on your Caty subtype, thus allowing you to add new tasks in different source files.
class X < Caty
end
X.append do
def
puts 'bar task'
end
end
X.start!(%w{bar})
116 117 118 |
# File 'lib/caty.rb', line 116 def append( &block ) self.class_eval(&block) end |
.start!(args = ARGV) ⇒ Object
Starts command line parsing.
Subclass.start!( arguments_array )
Subclass.start!
Returns true on success, false when an ArgumentError was detected.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/caty.rb', line 60 def start!( args = ARGV ) initialize_instance begin caty = self.new caty. = @global_options.grep!(args) task_name = args.delete_at(0) || @default raise Caty::NoSuchTaskError, "You need to provide a task" if task_name.nil? task = @tasks.resolve(task_name.to_sym) if task.nil? raise Caty::NoSuchTaskError, "There is no task named `#{task_name}'" else caty. = task.parse!(args) caty.instance_exec(task_name.to_sym, &@before) unless @before.nil? task.execute(caty) caty.instance_exec(task_name.to_sym, &@after) unless @after.nil? end return true rescue Caty::NoSuchTaskError, Caty::OptionArgumentError => e $stdout.puts e. return false rescue ArgumentError => e # verify that this is actually the task throwing the error if is_task_argument_error(e.backtrace, task_name) $stdout.puts "Bad arguments for task #{task.name}." $stdout.puts "Usage: #{task.to_s}" return false else raise end end end |