class CommandoClosure
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.
Attributes
Public Class Methods
@param tags [Array<String>,String] the options to
recognize, either just a string "--option" or an array [ "-o", "--option" ]
@param description [String] help text to describe this option. @param closure [Proc] the method to invoke if this option is
seen; the number of parameters to the Proc determines how many subsequent arguments are considered parameters for this option.
CommandoOpt::new
# File lib/scriptroute/commando.rb, line 154 def initialize(tags, 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(tags, description) @closure = closure end
Public Instance Methods
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.
@param argument [String] the value that the variable
should take, either from the default or from the command line.
# 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
@return [String] the default value, mostly for
compatibility with CommandoVar.
# 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
@return [Boolean] whether the option takes a parameter
# File lib/scriptroute/commando.rb, line 112 def takes_argument? (@closure.arity == -2) # means it takes one arg. end