Class: Vmreverter::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vmreverter/options.rb

Class Method Summary collapse

Class Method Details

.optionsObject



4
5
6
# File 'lib/vmreverter/options.rb', line 4

def self.options
  return @options
end

.parse_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/vmreverter/options.rb', line 9

def self.parse_args
  return @options if @options

  @no_args = ARGV.empty? ? true : false

  @defaults = {}
  @options = {}
  @options_from_file = {}

  optparse = OptionParser.new do|opts|
    # Set a banner
    opts.banner = "Usage: #{File.basename($0)} [options...]"

    @defaults[:auth] = File.join(ENV['HOME'], '.fog')
    opts.on '-a', '--auth FILE',
            'Use authentication FILE',
            "Default: #{@defaults[:auth]}" do |file|
      @options[:auth] = file
    end

    @defaults[:config] = nil
    opts.on '-c', '--config FILE',
            'Use configuration FILE', 
            "Default: #{@defaults[:config]}" do |file|
      @options[:config] = file
    end

    @defaults[:options_file] = nil
    opts.on '-o', '--options-file FILE',
            'Read options from FILE',
            'This should evaluate to a ruby hash.',
            'CLI optons are given precedence.' do |file|
      @options_from_file = parse_options_file file
    end

    @defaults[:quiet] = false
    opts.on '-q', '--[no-]quiet',
            'Do not log output to STDOUT',
            '(default: false)' do |bool|
      @options[:quiet] = bool
    end

    @defaults[:color] = true
    opts.on '--[no-]color',
            'Do not display color in log output',
            '(default: true)' do |bool|
      @options[:color] = bool
    end

    @defaults[:debug] = false
    opts.on '--[no-]debug',
            'Enable full debugging',
            '(default: false)' do |bool|
      @options[:debug] = bool
    end


    opts.on_tail("-h","--help","Display this screen") do
      puts opts
      exit
    end
  end

  optparse.parse!

  # We have use the @no_args var because OptParse consumes ARGV as it parses
  # so we have to check the value of ARGV at the begining of the method,
  # let the options be set, then output usage.
  puts optparse if @no_args

  # merge in the options that we read from the file
  @options = @options_from_file.merge(@options)
  # merge in defaults
  @options = @defaults.merge(@options)

  @options
end

.parse_options_file(options_file_path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vmreverter/options.rb', line 87

def self.parse_options_file(options_file_path)
  options_file_path = File.expand_path(options_file_path)
  unless File.exists?(options_file_path)
    raise ArgumentError, "Specified options file '#{options_file_path}' does not exist!"
  end
  # This eval will allow the specified options file to have access to our
  #  scope.  It is important that the variable 'options_file_path' is
  #  accessible, because some existing options files (e.g. puppetdb) rely on
  #  that variable to determine their own location (for use in 'require's, etc.)
  result = eval(File.read(options_file_path))
  unless result.is_a? Hash
    raise ArgumentError, "Options file '#{options_file_path}' must return a hash!"
  end

  result
end