Class: CommandoVar
- Inherits:
-
CommandoOpt
- Object
- CommandoOpt
- CommandoVar
- Defined in:
- lib/scriptroute/commando.rb
Overview
simple class for setting variables in command processing.
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
The default value of the variable.
-
#varname ⇒ Symbol
readonly
The name of the variable to set.
Attributes inherited from CommandoOpt
Instance Method Summary collapse
-
#initialize(tags, description, varname, default = nil) ⇒ CommandoVar
constructor
A new instance of CommandoVar.
-
#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.
-
#string_default ⇒ String
The default argument, if any, or an empty string if no arguments are taken.
-
#takes_argument? ⇒ Boolean
Whether the option takes a parameter, based on whether the default is true or false.
Methods inherited from CommandoOpt
Constructor Details
#initialize(tags, description, varname, default = nil) ⇒ CommandoVar
Returns a new instance of CommandoVar.
95 96 97 98 99 100 101 |
# File 'lib/scriptroute/commando.rb', line 95 def initialize(, description, varname, default=nil) # should probably ensure that this isn't running setuid. super(, description) @varname = varname @default = default set(@default) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the default value of the variable.
46 47 48 |
# File 'lib/scriptroute/commando.rb', line 46 def default @default end |
#varname ⇒ Symbol (readonly)
Returns 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.
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_default ⇒ String
Returns 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.
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 |