Class: CommandoClosure

Inherits:
CommandoOpt show all
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

Attributes inherited from CommandoOpt

#description, #tags

Instance Method Summary collapse

Methods inherited from CommandoOpt

#help, #seek

Constructor Details

#initialize(tags, description, closure) ⇒ CommandoClosure

Returns a new instance of CommandoClosure.

Parameters:

  • tags (Array<String>, String)

    the options to recognize, either just a string “–option” or an array [ “-o”, “–option” ]

  • description (String)

    help text to describe this option.

  • 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.

Raises:

  • (ArgumentError)


154
155
156
157
158
159
160
161
162
# 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

Instance Attribute Details

#closureObject (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.

Parameters:

  • argument (String)

    the value that the variable should take, either from the default or from the command line.



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_defaultString

Returns the default value, mostly for compatibility with CommandoVar.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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