Class: DuperVisor::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dupervisor/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



11
12
13
14
# File 'lib/dupervisor/cli.rb', line 11

def initialize(args)
  self.args    = args
  self.config = Config.new
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



9
10
11
# File 'lib/dupervisor/cli.rb', line 9

def args
  @args
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/dupervisor/cli.rb', line 9

def config
  @config
end

#parserObject

Returns the value of attribute parser.



9
10
11
# File 'lib/dupervisor/cli.rb', line 9

def parser
  @parser
end

Instance Method Details

#example_banner(opts) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dupervisor/cli.rb', line 58

def example_banner(opts)
  opts.separator ''
  opts.separator 'Examples:'.bold.blue
  opts.separator ''
  opts.separator '    # guess input format, convert YAML format to an INI file'
  opts.separator '    cat config.yml | dv --ini > config.ini'.green
  opts.separator ''
  opts.separator '    # guess input format, convert INI format to a JSON file '
  opts.separator '    dv config.ini --json -o config.json'.green

  opts.separator ''
  opts.separator 'Common options:'.bold.blue
end

#parseObject



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
# File 'lib/dupervisor/cli.rb', line 16

def parse
  self.parser = OptionParser.new do |opts|
    usage_banner(opts)

    opts.on('--ini', 'Generate an INI file') do
      config.to = :ini
    end
    opts.on('--yaml', 'Generate a YAML file') do
      config.to = :yaml
    end
    opts.on('--json', 'Generate a JSON file') do
      config.to = :json
    end

    opts.on('-o', '--output [FILE]',
            'File to write, if not supplied write to STDOUT') do |file|
      config.output = file
    end

    opts.on('-v', '--verbose',
            'Print extra debugging info') do
      config.verbose = true
    end
    example_banner(opts)

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail('--version', 'Show version') do
      puts DuperVisor::VERSION
      exit
    end
  end
  parser.parse!(args)
  config
end

#usage_banner(opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dupervisor/cli.rb', line 72

def usage_banner(opts)
  opts.banner = 'Usage: '.bold.blue + 'dv [source-file] [options] '.bold.green
  opts.separator ''
  opts.separator '       Convert between several hierarchical configuration'
  opts.separator '       file formats, such as ' + 'ini, yaml, json'.bold.green
  opts.separator ''
  opts.separator '       Automatically guesses the source format based either on'
  opts.separator '       the file extension, or by attempting to parse it for STDIN'
  opts.separator ''
  opts.separator 'Specific options:'.bold.blue
end