Class: Tane::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/tane/parser.rb

Class Method Summary collapse

Class Method Details

.global_option(name, *args) ⇒ Object



9
10
11
# File 'lib/tane/parser.rb', line 9

def global_option(name, *args)
  self.global_options << {:name => name, :args => args}
end

.global_optionsObject



5
6
7
# File 'lib/tane/parser.rb', line 5

def global_options
  @global_options ||= []
end

.parse(args) ⇒ Object



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
# File 'lib/tane/parser.rb', line 13

def parse(args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  options = OpenStruct.new
  options.scheme = "http"
  options.host = "localhost"
  options.port = 3000
  options.inplace = false
  options.encoding = "utf8"
  options.transfer_type = :auto
  options.verbose = false

  global_option :port,    '-p', '--port PORT',     Integer, "The port your local Cloudfuji app is running on"
  global_option :host,    '-n', '--host HOST',     String,  "The hostname where your local Cloudfuji app is running"
  global_option :scheme,  '-s', '--scheme SCHEME', String,  "Either http or https, whichever protocol your local Cloudfuji app is using"
  global_option :verbose, '-V', '--verbose',                "Output a lot of noise"

  opts = OptionParser.new do |opts|
    banner  = "Usage: tane command [options]\n"
    banner += Tane::Commands.command_list_and_help

    opts.banner = banner

    opts.separator ""
    opts.separator "Specific options:"

    # Mandatory argument.
    global_options.each do |option|
      opts.on(option[:name].to_s, *option[:args]) do |value|
        options.send("#{option[:name]}=", value)
      end
    end
    options.send("help_text=", opts.help())
    opts.parse!(args)
    return options
  end
end