Class: DPM::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/dpm/options.rb

Constant Summary collapse

PACKAGE_COMMANDS =
%w[tags status start stop restart configure].freeze
PACKAGE_REGEX =
/\A[\w.\-:]+\z/.freeze
UNPACKAGE_COMMANDS =
%w[list packages cleanup].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



11
12
13
14
# File 'lib/dpm/options.rb', line 11

def initialize(argv)
  self.argv = argv
  self.dry_run = false
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



9
10
11
# File 'lib/dpm/options.rb', line 9

def argv
  @argv
end

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/dpm/options.rb', line 9

def command
  @command
end

#dry_runObject

Returns the value of attribute dry_run.



9
10
11
# File 'lib/dpm/options.rb', line 9

def dry_run
  @dry_run
end

#packageObject

Returns the value of attribute package.



9
10
11
# File 'lib/dpm/options.rb', line 9

def package
  @package
end

#parserObject

Returns the value of attribute parser.



9
10
11
# File 'lib/dpm/options.rb', line 9

def parser
  @parser
end

#rawObject

Returns the value of attribute raw.



9
10
11
# File 'lib/dpm/options.rb', line 9

def raw
  @raw
end

Class Method Details

.parse!(argv) ⇒ Object



16
17
18
# File 'lib/dpm/options.rb', line 16

def self.parse!(argv)
  new(argv).parse!
end

Instance Method Details

#help_textObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dpm/options.rb', line 53

def help_text
  "    \#{parser}\n    COMMAND:\n        help                             Show the help\n        version                          Show the version\n        packages                         List supported packages\n        list                             List running packages\n        cleanup                          Cleanup useless data\n        tags PACKAGE                     List supported tags of a package\n        status PACKAGE                   Get the status of a package\n        start PACKAGE                    Start a package\n        stop PACKAGE                     Stop a package\n        restart PACKAGE                  Restart a package\n        configure PACKAGE                Configure a package in the user-level, including configure a new package\n\n    See more at https://github.com/songhuangcn/dpm\n  EOF\nend\n"

#parse!Object



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
# File 'lib/dpm/options.rb', line 20

def parse!
  OptionParser.new do |parser|
    self.parser = parser

    parser.banner = "Usage: dpm [options] [COMMAND] [PACKAGE]"
    parser.separator ""
    parser.separator "Options:"

    parser.on("-d", "--dry-run", "Don't actually run Docker command, just print") do |v|
      self.dry_run = v
    end

    parser.on("-r", "--raw", "Try to run a package even if it doesn't exist") do |v|
      self.raw = v
    end

    parser.on_tail("-h", "--help", "Show the help") do
      puts help_text
      exit
    end

    parser.on_tail("-v", "--version", "Show the version") do
      puts VERSION
      exit
    end

    parser.parse!(argv)
  end

  process_args!
  self
end

#process_args!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dpm/options.rb', line 73

def process_args!
  self.command, self.package = argv

  case command
  when "help"
    puts help_text
    exit
  when "version"
    puts VERSION
    exit
  when *PACKAGE_COMMANDS
    raise Error, "Command `#{command}` need a package" if package.blank?
    raise Error, "Package invalid, valid regex: #{PACKAGE_REGEX}" if !PACKAGE_REGEX.match?(package)
  else
    raise Error, "Unknown command `#{command}`, see `dpm help`" unless UNPACKAGE_COMMANDS.include?(command)
  end
end