Module: CloudFlock::Interface::CLI::Opts
- Extended by:
- Opts
- Included in:
- Opts
- Defined in:
- lib/cloudflock/interface/cli/opts.rb,
lib/cloudflock/interface/cli/opts/servers.rb
Overview
Public: The CLI Opts module provides methods to abstract and simplify loading configuration information, parsing options and providing context to the application.
Constant Summary collapse
- CONFIG_LOCATION =
"~/.flockrc"
Instance Method Summary collapse
-
#argv_servers(opts, options) ⇒ Object
Internal: Extend the Opts module to provide options specific to the servers migration CLI utility.
-
#parse(function = '') ⇒ Object
Public: Open config files if applicable, overwriting default options with configuration passed files first, then with any options supplied via the command line.
-
#parse_argv(function) ⇒ Object
Internal: Parse and return options passed via the command line.
-
#parse_config_file(file) ⇒ Object
Internal: Open and parse a given config file.
Instance Method Details
#argv_servers(opts, options) ⇒ Object
Internal: Extend the Opts module to provide options specific to the servers migration CLI utility.
opts - OptionParser object to which to add options. options - Hash containing options flags and settings for the application.
Returns nothing.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cloudflock/interface/cli/opts/servers.rb', line 9 def argv_servers(opts, ) opts.on('-o', '--opencloud', 'Perform an Open Cloud Servers migration') do [:function] = :opencloud end opts.on('-s', '--servers', 'Perform a Cloud Servers migration') do [:function] = :servers end opts.on('-r', '--resume', 'Resume a migration') do [:resume] = true end end |
#parse(function = '') ⇒ Object
Public: Open config files if applicable, overwriting default options with configuration passed files first, then with any options supplied via the command line.
function - String or Symbol containing the name of the function to parse
arguments for, if applicable. (default: '')
Returns a Hash containing an option to value map.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cloudflock/interface/cli/opts.rb', line 17 def parse(function = '') = parse_config_file(CONFIG_LOCATION) argv = parse_argv(function) if argv[:config_file].kind_of?(String) .merge(parse_config_file(argv[:config_file])) end .merge(argv) end |
#parse_argv(function) ⇒ Object
Internal: Parse and return options passed via the command line.
function - String or Symbol containing the name of the function to parse
arguments for. This will cause the OptionParser to search for a
argv_function name for the given name of the function.
Returns a Hash containing an option to value map.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cloudflock/interface/cli/opts.rb', line 60 def parse_argv(function) = {} optparse = OptionParser.new do |opts| opts.on('-v', '--verbose', 'Be verbose') do [:verbose] = true end opts.on('-c', '--config FILE', 'Load configuration saved from previous' + ' session (useful with -r)') do |file| unless File.exists?(File.(file)) puts "Configuration file #{file} does not exist! Exiting." exit end [:config_file] = File.(file) end # Pull in extra options if applicable function = ("argv_" + function.to_s).to_sym if CloudFlock::Interface::CLI::Opts.respond_to?(function) CloudFlock::Interface::CLI::Opts.send(function, opts, ) end end optparse.parse! end |
#parse_config_file(file) ⇒ Object
Internal: Open and parse a given config file.
file - String containing path to a configuration file which will be parsed.
Returns a Hash containing option-value mappings.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cloudflock/interface/cli/opts.rb', line 33 def parse_config_file(file) = {} return if file.nil? config_string = "" if File.exists?(File.(file)) config_string = File.open(File.(file)).read end config_string.each_line do |line| line.gsub!(/#.*/, "").strip! next if line.empty? opt,value = line.split(/\s*/, 2) [opt.to_sym] = value end end |