Module: Deployman::App::Optionparser

Defined in:
lib/deployman/app/optionparser.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



5
6
7
8
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
86
87
88
89
90
91
92
# File 'lib/deployman/app/optionparser.rb', line 5

def self.parse (args)

  # This hash will hold all of the options parsed from the command-line by OptionParser.
  options = {}

  optparse = OptionParser.new do |opts|

    # Set a banner, displayed at the top of the help screen.
      opts.banner = "Usage: deployman [option1] param1,param2 [optionN] paramN ...\r\n\r\n"

      # install
      options[:install] = false
    opts.on('--install OWNER,SOFTWARE,RELEASE,ZIP_URL,DEST', Array, "Install" ) do |setting|
      argumentLength = 5
      raise OptionParser::MissingArgument if setting.length < argumentLength
      raise OptionParser::NeedlessArgument if setting.length > argumentLength
        options[:install] = setting
    end

    # uninstall
    options[:uninstall] = false
    opts.on('--uninstall PATH', "Uninstall") do |setting|
      options[:uninstall] = setting
    end

    # auth
    options[:auth] = false
    opts.on('--auth USER,PASS', Array, "HTTP Authentication for Github API (works only in combination with --install)") do |setting|
      argumentLength = 2
      raise OptionParser::MissingArgument if setting.length < argumentLength
      raise OptionParser::NeedlessArgument if setting.length > argumentLength
      options[:auth] = setting
    end

    # config
    options[:config] = {}
    opts.on('--config VALUES', Array, "add custom config values (works only in combination with --install)\n\t\t\t\t\t VALUES = <key>,<value>,<key>,<value>,...") do |setting|
      raise OptionParser::MissingArgument if setting.length % 2 != 0
      # create key-value pairs from setting
      config = {}
      setting.each_slice(2) do |key, value|
        config.store(key, value)
      end
      options[:config] = config
    end

    # callback
    options[:callback] = false
    opts.on('--callback URL', "callbacks status of installation (running/finished/error) + stdout + stderr\n\t\t\t\t\t JSON-Format: { \"status\" => \"running\", \"stdout\" => <STDOUT>, \"stderr\" => <STDERR> }") do |setting|
      options[:callback] = setting
    end

    # createfiles from api
    options[:createfiles_from_api] = {}
    opts.on('--createfiles_from_api VALUES', Array, "creates files from given api-urls (works only in combination with --install),\n\t\t\t\t\t VALUES = <api_url1>,<api_url2>,...\n\t\t\t\t\t The api call has to return the file in following json-format:\n\t\t\t\t\t {\"path\":\"path/to/file.txt\",\"content\":\"content of file\"}") do |setting|
      options[:createfiles_from_api] = setting
    end

      # help
        opts.on( '-h', '--help', 'Display help screen' ) do
          puts opts
          puts "\r\n"
          exit
        end

  end

  # Parse the command-line. Remember there are two forms
  # of the parse method. The 'parse' method simply parses
  # ARGV, while the 'parse!' method parses ARGV and removes
  # any options found there, as well as any parameters for
  # the options. What's left is the list of files to resize.
  begin
    optparse.parse! args
    # return the parsed options
    return options
  rescue OptionParser::InvalidOption => e
    warn "Error! #{e}"
    exit
  rescue OptionParser::MissingArgument => e
    warn "Error! #{e}"
    exit
  rescue OptionParser::NeedlessArgument => e
    warn "Error! #{e}"
    exit 
  end

end