Class: ParametersBase

Inherits:
OptionParser
  • Object
show all
Includes:
ParameterExceptions
Defined in:
lib/ec2/amitools/parameters_base.rb

Constant Summary collapse

USER_CERT_PATH_DESCRIPTION =

Descriptions for common parameters:

"The path to the user's PEM encoded RSA public key certificate file."
USER_PK_PATH_DESCRIPTION =
"The path to the user's PEM encoded RSA private key file."
USER_DESCRIPTION =
"The user's AWS access key ID."
PASS_DESCRIPTION =
"The user's AWS secret access key."
USER_ACCOUNT_DESCRIPTION =
"The user's EC2 user ID (Note: AWS account number, NOT Access Key ID)."
HELP_DESCRIPTION =
"Display this help message and exit."
MANUAL_DESCRIPTION =
"Display the user manual and exit."
DEBUG_DESCRIPTION =
"Display debug messages."
VERSION_DESCRIPTION =
"Display the version and copyright notice and then exit."
BATCH_DESCRIPTION =
"Run in batch mode. No interactive prompts."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, name = nil) ⇒ ParametersBase

Returns a new instance of ParametersBase.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ec2/amitools/parameters_base.rb', line 146

def initialize(argv, name=nil)
  super(argv)

  # Mandatory parameters.
  separator("")
  separator("MANDATORY PARAMETERS")
  mandatory_params()
  
  # Optional parameters.
  separator("")
  separator("OPTIONAL PARAMETERS")
  common_params()
  optional_params()

  # Parse the command line parameters.
  parse!(argv)

  unless early_exit?
    validate_params()
    set_defaults()
  end
end

Instance Attribute Details

#batch_modeObject

Returns the value of attribute batch_mode.



31
32
33
# File 'lib/ec2/amitools/parameters_base.rb', line 31

def batch_mode
  @batch_mode
end

#debugObject

Returns the value of attribute debug.



31
32
33
# File 'lib/ec2/amitools/parameters_base.rb', line 31

def debug
  @debug
end

#manualObject

Returns the value of attribute manual.



31
32
33
# File 'lib/ec2/amitools/parameters_base.rb', line 31

def manual
  @manual
end

#show_helpObject

Returns the value of attribute show_help.



31
32
33
# File 'lib/ec2/amitools/parameters_base.rb', line 31

def show_help
  @show_help
end

#versionObject

Returns the value of attribute version.



31
32
33
# File 'lib/ec2/amitools/parameters_base.rb', line 31

def version
  @version
end

Instance Method Details

#assert_directory_exists(path, param) ⇒ Object



101
102
103
104
105
# File 'lib/ec2/amitools/parameters_base.rb', line 101

def assert_directory_exists(path, param)
  unless (File::exist?(path) and File::directory?(path))
    raise InvalidValue.new(param, path, "Directory does not exist or is not a directory.")
  end
end

#assert_exists(path, param) ⇒ Object

——————————————————————————# Validation utility methods ——————————————————————————#



77
78
79
80
81
# File 'lib/ec2/amitools/parameters_base.rb', line 77

def assert_exists(path, param)
  unless File::exist?(path)
    raise InvalidValue.new(param, path, "File or directory does not exist.")
  end
end

#assert_file_executable(path, param) ⇒ Object



95
96
97
98
99
# File 'lib/ec2/amitools/parameters_base.rb', line 95

def assert_file_executable(path, param)
  unless (File::executable?(path) and File::file?(path))
    raise InvalidValue.new(param, path, "File not executable.")
  end
end

#assert_file_exists(path, param) ⇒ Object



89
90
91
92
93
# File 'lib/ec2/amitools/parameters_base.rb', line 89

def assert_file_exists(path, param)
  unless (File::exist?(path) and File::file?(path))
    raise InvalidValue.new(param, path, "File does not exist or is not a file.")
  end
end

#assert_glob_expands(path, param) ⇒ Object



83
84
85
86
87
# File 'lib/ec2/amitools/parameters_base.rb', line 83

def assert_glob_expands(path, param)
  if Dir::glob(path).empty?
    raise InvalidValue.new(param, path, "File or directory does not exist.")
  end
end

#assert_good_key(key, param) ⇒ Object



113
114
115
116
117
# File 'lib/ec2/amitools/parameters_base.rb', line 113

def assert_good_key(key, param)
  if key.include?("/")
    raise InvalidValue.new(param, key, "'/' character not allowed.")
  end
end

#assert_option_in(option, choices, param) ⇒ Object



107
108
109
110
111
# File 'lib/ec2/amitools/parameters_base.rb', line 107

def assert_option_in(option, choices, param)
  unless choices.include?(option)
    raise InvalidValue.new(param, option)
  end
end

#common_paramsObject

——————————————————————————# Parameters common to all tools ——————————————————————————#



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ec2/amitools/parameters_base.rb', line 123

def common_params()
  on('-h', '--help', HELP_DESCRIPTION) do
    @show_help = true
  end
  
  on('--version', VERSION_DESCRIPTION) do
    @version = true
  end
  
  on('--manual', MANUAL_DESCRIPTION) do
    @manual = true
  end
  
  on('--batch', BATCH_DESCRIPTION) do
    @batch_mode = true
  end
  
  on('--debug', DEBUG_DESCRIPTION) do
    @debug = true
  end    
end

#early_exit?Boolean

——————————————————————————# Useful utility methods ——————————————————————————#

Returns:

  • (Boolean)


61
62
63
# File 'lib/ec2/amitools/parameters_base.rb', line 61

def early_exit?()
  @show_help or @manual or @version
end

#interactive?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ec2/amitools/parameters_base.rb', line 65

def interactive?()
  not (early_exit? or @batch_mode)
end

#mandatory_paramsObject

——————————————————————————# Methods to override in subclasses ——————————————————————————#



41
42
43
# File 'lib/ec2/amitools/parameters_base.rb', line 41

def mandatory_params()
  # Override this for mandatory parameters
end

#optional_paramsObject



45
46
47
# File 'lib/ec2/amitools/parameters_base.rb', line 45

def optional_params()
  # Override this for optional parameters
end

#set_defaultsObject



53
54
55
# File 'lib/ec2/amitools/parameters_base.rb', line 53

def set_defaults()
  # Override this for parameter validation
end

#validate_paramsObject



49
50
51
# File 'lib/ec2/amitools/parameters_base.rb', line 49

def validate_params()
  # Override this for parameter validation
end


69
70
71
# File 'lib/ec2/amitools/parameters_base.rb', line 69

def version_copyright_string()
  EC2Version::version_copyright_string()
end