Class: CommandoVar

Inherits:
CommandoOpt show all
Defined in:
lib/scriptroute/commando.rb

Overview

simple class for setting variables in command processing.

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, varname, default = nil) ⇒ CommandoVar

Returns a new instance of CommandoVar.

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.

  • varname (Symbol)

    a global symbol to set to the value, e.g., “:$Option”

  • default (Object) (defaults to: nil)

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



95
96
97
98
99
100
101
# 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

Instance Attribute Details

#defaultObject (readonly)

Returns the default value of the variable.

Returns:

  • (Object)

    the default value of the variable



46
47
48
# File 'lib/scriptroute/commando.rb', line 46

def default
  @default
end

#varnameSymbol (readonly)

Returns the name of the variable to set.

Returns:

  • (Symbol)

    the name of the variable to set



44
45
46
# File 'lib/scriptroute/commando.rb', line 44

def varname
  @varname
end

Instance Method Details

#set(argument) ⇒ Fixnum, String

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.

Returns:

  • (Fixnum, String)

    likely the argument as interpreted by eval. Not likely to be useful.



75
76
77
78
79
80
81
82
# 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_defaultString

Returns the default argument, if any, or an empty string if no arguments are taken.

Returns:

  • (String)

    the default argument, if any, or an empty string if no arguments are taken.



57
58
59
60
61
62
63
# File 'lib/scriptroute/commando.rb', line 57

def string_default
  if takes_argument? then 
    @default.to_s
  else
    ""
  end
end

#takes_argument?Boolean

Returns whether the option takes a parameter, based on whether the default is true or false.

Returns:

  • (Boolean)

    whether the option takes a parameter, based on whether the default is true or false.



50
51
52
53
54
# 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