Class: Joggle::CLI::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/joggle/cli/option-parser.rb

Overview

Option parser for Joggle command-line interface.

Constant Summary collapse

DEFAULTS =

Default configuration.

{
  # pull default jabber username and password from environment
  'jabber.user' => ENV['JOGGLE_USERNAME'],
  'jabber.pass' => ENV['JOGGLE_PASSWORD'],
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ OptionParser

Create new command-line option parser.



31
32
33
# File 'lib/joggle/cli/option-parser.rb', line 31

def initialize(app)
  @app = app
end

Class Method Details

.run(app, args) ⇒ Object

Create and run a new command-line option parser.



24
25
26
# File 'lib/joggle/cli/option-parser.rb', line 24

def self.run(app, args)
  new(app).run(args)
end

Instance Method Details

#run(args) ⇒ Object

Run command-line option parser.



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/joggle/cli/option-parser.rb', line 38

def run(args)
  ret = DEFAULTS.merge({})

  # create option parser
  o = ::OptionParser.new do |o|
    o.banner = "Usage: #@app [options]"
    o.separator " "

    # add command-line options
    o.separator "Options:"

    o.on('-A', '--allow USER', 'Allow Jabber subscription from USER.') do |v|
      add_allowed(ret, v)
    end

    o.on('-c', '--config FILE', 'Use configuration file FILE.') do |v|
      Joggle::ConfigParser.run(v) do |key, val|
        if key == 'engine.allow'
          add_allowed(ret, val)
        elsif key == 'engine.update.range'
          add_update_range(ret, val)
        else
          ret[key] = val
        end
      end
    end

    o.on('-D', '--daemon', 'Run as daemon (in background).') do |v|
      ret['cli.daemon'] = true
    end

    o.on('--foreground', 'Run in foreground (the default).') do |v|
      ret['cli.daemon'] = false
    end

    o.on('-L', '--log-level LEVEL', 'Set log level to LEVEL.') do |v|
      ret['runner.log.level'] = v
    end

    o.on('-l', '--log FILE', 'Log to FILE.') do |v|
      ret['runner.log.path'] = v
    end

    o.on('-p', '--password PASS', 'Jabber password (INSECURE!).') do |v|
      ret['jabber.pass'] = v
    end

    o.on('-s', '--store FILE', 'Use FILE as backing store.') do |v|
      ret['runner.store.path'] = v
    end

    o.on('-u', '--username USER', 'Jabber username.') do |v|
      ret['jabber.user'] = v
    end

    o.separator ' '

    o.on_tail('-v', '--version', 'Print version string.') do
      puts "Joggle %s, by %s" % [
        Joggle::VERSION,
        'Paul Duncan <[email protected]>',
      ]
      exit
    end

    o.on_tail('-h', '--help', 'Print help information.') do
      puts o
      exit
    end
  end

  # parse arguments
  o.parse(args)

  # return results
  ret
end