class CommandoVar

simple class for setting variables in command processing.

Attributes

default[R]

@return [Object] the default value of the variable

varname[R]

@return [Symbol] the name of the variable to set

Public Class Methods

new(tags, description, varname, default=nil) click to toggle source

@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 varname [Symbol] a global symbol to set to the

value, e.g., ":$Option"

@param default [Object] a default value for the option,

if not set, useful for the help description.  If the default
is not a string, the assignment evals the variable unquoted.
(That is, integer arguments should work as numbers without
a need to convert.)
Calls superclass method CommandoOpt::new
# File lib/scriptroute/commando.rb, line 95
def initialize(tags, description, varname, default=nil) 
  # should probably ensure that this isn't running setuid.
  super(tags, description)
  @varname = varname
  @default = default
  set(@default)
end

Public Instance Methods

set(argument) click to toggle source

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.

@return [Fixnum,String] likely the argument as

interpreted by eval.  Not likely to be useful.
# File lib/scriptroute/commando.rb, line 75
def set(argument)
  Kernel.eval( if(@default.is_a?(String)) then
                 "#{@varname} = \"#{argument}\""
               else
                "#{@varname} = #{argument}"
               end
              )
end
string_default() click to toggle source

@return [String] the default argument, if any, or an

empty string if no arguments are taken.
# File lib/scriptroute/commando.rb, line 57
def string_default
  if takes_argument? then 
    @default.to_s
  else
    ""
  end
end
takes_argument?() click to toggle source

@return [Boolean] whether the option takes a parameter,

based on whether the default is true or false.
# File lib/scriptroute/commando.rb, line 50
def takes_argument?
  # not really right, it should check the arity of the
  # closure if present.
  !(@default == true || @default == false)
end