Class: CommandoClosure
- Inherits:
-
CommandoOpt
- Object
- CommandoOpt
- CommandoClosure
- Defined in:
- lib/scriptroute/commando.rb
Overview
instead of setting a variable, execute an anonymous function. if the function takes an arg, gobble the next command line argument. If an argument is taken, the function is responsible for converting the string to whatever type is desired, and validating the input – raising an exception if a failure occurs.
Instance Attribute Summary collapse
-
#closure ⇒ Object
readonly
Returns the value of attribute closure.
Attributes inherited from CommandoOpt
Instance Method Summary collapse
-
#initialize(tags, description, closure) ⇒ CommandoClosure
constructor
A new instance of CommandoClosure.
-
#set(argument) ⇒ Object
basic assignment, if it’s a string, we quote it, else the interpreter should deal with numbers, and complain if something should have been quoted.
-
#string_default ⇒ String
The default value, mostly for compatibility with CommandoVar.
-
#takes_argument? ⇒ Boolean
Whether the option takes a parameter.
Methods inherited from CommandoOpt
Constructor Details
#initialize(tags, description, closure) ⇒ CommandoClosure
Returns a new instance of CommandoClosure.
154 155 156 157 158 159 160 161 162 |
# File 'lib/scriptroute/commando.rb', line 154 def initialize(, description, closure) raise ArgumentError, "CommandoClosure takes a lambda as the third arg" unless( closure.is_a?(Proc) ) # ruby1.8 seemed to use -1/-2 here; ruby1.9 seems to use # 0/1 here. accept em all. raise ArgumentError, "Closure for #{tags} should take zero or one args, not #{closure.arity}" unless(closure.arity == -1 or closure.arity == -2 or closure.arity == 0 or closure.arity == 1) super(, description) @closure = closure end |
Instance Attribute Details
#closure ⇒ Object (readonly)
Returns the value of attribute closure.
110 111 112 |
# File 'lib/scriptroute/commando.rb', line 110 def closure @closure end |
Instance Method Details
#set(argument) ⇒ Object
basic assignment, if it’s a string, we quote it, else the interpreter should deal with numbers, and complain if something should have been quoted. This operation allows crazy s**t to happen, so don’t use this in setuid code.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/scriptroute/commando.rb', line 134 def set(argument) case @closure.arity when -1 @closure.call when -2 @closure.call argv[i] argv.delete_at(i) else raise "strange closure takes #{@closure.arity} args" end end |
#string_default ⇒ String
Returns the default value, mostly for compatibility with CommandoVar.
118 119 120 121 122 123 124 |
# File 'lib/scriptroute/commando.rb', line 118 def string_default if takes_argument? then "[x]" # return something to appease the help msg. else "" end end |
#takes_argument? ⇒ Boolean
Returns whether the option takes a parameter.
112 113 114 |
# File 'lib/scriptroute/commando.rb', line 112 def takes_argument? (@closure.arity == -2) # means it takes one arg. end |