Class: DevTools::OptParse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ OptParse

Returns a new instance of OptParse.



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
# File 'lib/devtools/options.rb', line 10

def initialize(opts)
  program_path  = DevTools::PROGRAM
  program_files = []

  %W(~/.#{program_path} ./.#{program_path} ./#{program_path}.conf ./#{program_path}.rc).each do |path|
    program_files.push File.expand_path(path)
  end

  Config.setup do |config|
    config.const_name = 'Options'
    config.use_env    = true
  end

  Config.load_and_set_settings opts[:testing] ? String.new : program_files

  default_options(Options, opts[:defaults]) if opts[:defaults]

  Options.verbose = Options.verbose ? 1 : 0 unless Options.verbose.is_a?(Numeric)
  Options.debug   = true if Options.verbose >= 5

  @parser  ||= common_options opts[:testing]
  @version ||= sprintf "%s v%s (%s v%s)\n",
                       opts[:name] || DevTools::PROGRAM, opts[:version] || DevTools::VERSION,
                       "DevTools", DevTools::VERSION
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



8
9
10
# File 'lib/devtools/options.rb', line 8

def parser
  @parser
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/devtools/options.rb', line 8

def version
  @version
end

Instance Method Details

#common_options(unit_testing = false) ⇒ Object



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
# File 'lib/devtools/options.rb', line 36

def common_options(unit_testing = false)
  OptionParser.new do |opts|
    opts.on_tail("-h", "--help", "Show this message") do
      unless unit_testing
        puts version + "\n"
        puts opts
        exit
      end
    end

    # Config file
    opts.on_tail("-c", "--config FILE", "Specify an alternate configuration file") do |file|
      Options.add_source! file
      Options.reload!
    end

    # dry-run switch
    opts.on_tail("--dry-run", "Don't actually run commands") do
      Options.dryrun = true
    end

    # Verbose switch
    opts.on_tail("-q", "--quiet", "Run quietly") do
      Options.verbose = 0
    end

    opts.on_tail("-v", "--verbose", "Run verbosely (may be specified more than once)") do
      Options.verbose += 1
    end

    opts.on_tail("--version", "Show version") do
      unless unit_testing
        puts version
        exit
      end
    end
  end
end

#default_options(options, opts) ⇒ Object



75
76
77
78
79
# File 'lib/devtools/options.rb', line 75

def default_options(options, opts)
  opts.each do |key, value|
    options[key] = value unless options.keys.include?(key)
  end
end