Module: NginxStage::Application

Defined in:
lib/nginx_stage/application.rb

Overview

The command line interface for NginxStage

Class Method Summary collapse

Class Method Details

.cmd_parser(command, generator) ⇒ OptionParser

Parses user-supplied arguments for a given command



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/nginx_stage/application.rb', line 81

def self.cmd_parser(command, generator)
  OptionParser.new do |opts|
    opts.banner = "Usage: nginx_stage #{command} [OPTIONS]"

    opts.separator ""
    opts.separator "Required options:"
    generator.options.select {|k,v| v[:required]}.each do |k, v|
      opts.on(*v[:opt_args]) do |input|
        options[k] = unescape(input)
      end
    end

    opts.separator ""
    opts.separator "General options:"
    generator.options.select {|k,v| !v[:required]}.each do |k, v|
      opts.on(*v[:opt_args]) do |input|
        options[k] = unescape(input)
      end
    end

    opts.separator ""
    opts.separator "Common options:"
    opts.on("-h", "--help", "# Show this help message") do
      puts opts
      exit
    end
    opts.on("-v", "--version", "# Show version") do
      puts "nginx_stage, version #{VERSION}"
      exit
    end

    opts.separator ""
    opts.separator generator.footer
    opts.separator ""
  end
end

.commandsHash

Available commands for the CLI



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nginx_stage/application.rb', line 15

def self.commands
  {
    'pun'         => NginxStage::PunConfigGenerator,
    'app'         => NginxStage::AppConfigGenerator,
    'app_reset'   => NginxStage::AppResetGenerator,
    'app_list'    => NginxStage::AppListGenerator,
    'app_clean'   => NginxStage::AppCleanGenerator,
    'nginx'       => NginxStage::NginxProcessGenerator,
    'nginx_show'  => NginxStage::NginxShowGenerator,
    'nginx_list'  => NginxStage::NginxListGenerator,
    'nginx_clean' => NginxStage::NginxCleanGenerator,
  }
end

.default_parserOptionParser

Parses user-supplied arguments



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
# File 'lib/nginx_stage/application.rb', line 50

def self.default_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: nginx_stage COMMAND [OPTIONS]"

    opts.separator ""
    opts.separator "Commands:"
    commands.each do |cmd, klass|
      opts.separator sprintf(" %-20s# %s", cmd, klass.desc)
    end

    opts.separator ""
    opts.separator "General options:"
    opts.on("-h", "--help", "# Show this help message") do
      puts opts
      exit
    end
    opts.on("-v", "--version", "# Show version") do
      puts "nginx_stage, version #{VERSION}"
      exit
    end

    opts.separator ""
    opts.separator "All commands can be run with -h (or --help) for more information."
    opts.separator ""
  end
end

.optionsHash

Options parsed from CLI



9
10
11
# File 'lib/nginx_stage/application.rb', line 9

def self.options
  @options ||= {}
end

.startvoid

This method returns an undefined value.

Starts the NginxStage workflow



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nginx_stage/application.rb', line 31

def self.start
  ARGV << "--help" if ARGV.empty?
  command = ARGV.first[0] != '-' ? ARGV.shift : nil

  generator = commands.fetch(command) do
    command ? raise(InvalidCommand, "invalid command: #{command}") : nil
  end

  parser = generator ? cmd_parser(command, generator) : default_parser
  parser.parse!( ARGV )
  generator.new(options).invoke if generator
rescue
  $stderr.puts "#{$!.to_s}"
  $stderr.puts "Run 'nginx_stage --help' to see a full list of available command line options."
  exit(false)
end