Class: Daemon::Command::Options

Inherits:
Object
  • Object
show all
Includes:
UserInterface
Defined in:
lib/onapp/engine/command/options.rb

Overview

Parses ARGV and provides some options through attribute-accessors

Constant Summary

DEFAULT_DAEMONS_COUNT =
1

Instance Attribute Summary (collapse)

Attributes included from UserInterface

#err, #out

Instance Method Summary (collapse)

Methods included from UserInterface

#error, #say

Constructor Details

- (Options) initialize(argv, out = $stdout, err = $stderr)

Returns a new instance of Options



16
17
18
19
20
21
22
23
24
# File 'lib/onapp/engine/command/options.rb', line 16

def initialize(argv, out = $stdout, err = $stderr)
  @argv = argv.dup
  @out = out
  @err = err

  @forked = true
  @only_instances = false
  @forced = false
end

Instance Attribute Details

- (Object) command

Returns the value of attribute command



11
12
13
# File 'lib/onapp/engine/command/options.rb', line 11

def command
  @command
end

- (Object) forced

Returns the value of attribute forced



11
12
13
# File 'lib/onapp/engine/command/options.rb', line 11

def forced
  @forced
end

- (Object) forked

Returns the value of attribute forked



11
12
13
# File 'lib/onapp/engine/command/options.rb', line 11

def forked
  @forked
end

- (Object) only_instances

Returns the value of attribute only_instances



11
12
13
# File 'lib/onapp/engine/command/options.rb', line 11

def only_instances
  @only_instances
end

Instance Method Details

- (Object) parse!

Validates and parses ARGV



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
# File 'lib/onapp/engine/command/options.rb', line 29

def parse!
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} #{COMMANDS.join('|')}"
    opts.separator 'Options:'

    opts.on '-f', '--[no-]fork', 'Fork and detach' do |v|
      @forked = v
    end

    opts.on '-F', '--force', 'Force stop' do |v|
      @forced = v
    end

    opts.on '-o', '--only-instances', 'Stop only instances, don\'t kill master' do |v|
      @only_instances = v
    end

    opts.on_tail '-h', '--help', 'Show this message' do
      say parser
      return false
    end
  end

  parser.parse!(@argv)

  @command = @argv[0]

  if @argv.length == 0 || @argv.length > 1 || !Daemon::Command::COMMANDS.include?(@command)
    error "Wrong command: #{@argv.join(' ')}"
    error parser
    return false
  end

  true
rescue OptionParser::ParseError => ex
  error ex.message
  error parser
  return false
end