Class: EnvSpec::CLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/envspec/cli/command.rb

Overview

Base class for a single CLI subcommand.

Subclasses declare:

- name     (string, e.g. "lint")
- summary  (one-line description for global help)
- configure(parser, opts) — define options on the OptionParser
- call(argv, opts) — execute; return exit code

Direct Known Subclasses

Check, Init, Lint

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



35
36
37
# File 'lib/envspec/cli/command.rb', line 35

def initialize
  @opts = default_opts
end

Class Method Details



24
25
26
27
# File 'lib/envspec/cli/command.rb', line 24

def banner(value = nil)
  @banner = value if value
  @banner
end

.description(value = nil) ⇒ Object



29
30
31
32
# File 'lib/envspec/cli/command.rb', line 29

def description(value = nil)
  @description = value if value
  @description
end

.name(value = nil) ⇒ Object



14
15
16
17
# File 'lib/envspec/cli/command.rb', line 14

def name(value = nil)
  @name = value if value
  @name
end

.summary(value = nil) ⇒ Object



19
20
21
22
# File 'lib/envspec/cli/command.rb', line 19

def summary(value = nil)
  @summary = value if value
  @summary
end

Instance Method Details

#build_parserObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/envspec/cli/command.rb', line 47

def build_parser
  OptionParser.new do |o|
    o.banner = self.class.banner || "Usage: envspec #{self.class.name}"
    if self.class.description
      o.separator ""
      self.class.description.each_line { |ln| o.separator ln.chomp }
    end
    o.separator ""
    o.separator "Options:"
    configure(o, @opts)
    o.on("-h", "--help", "Show help for this command") { puts o; exit 0 }
  end
end

#call(_rest, _opts) ⇒ Object

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/envspec/cli/command.rb', line 75

def call(_rest, _opts)
  raise NotImplementedError
end

#configure(_parser, _opts) ⇒ Object



61
62
63
# File 'lib/envspec/cli/command.rb', line 61

def configure(_parser, _opts)
  # subclass override
end

#default_optsObject



39
40
41
# File 'lib/envspec/cli/command.rb', line 39

def default_opts
  {}
end

#helpObject



65
66
67
# File 'lib/envspec/cli/command.rb', line 65

def help
  parser.help
end

#parserObject



43
44
45
# File 'lib/envspec/cli/command.rb', line 43

def parser
  @parser ||= build_parser
end

#run(argv) ⇒ Object

Subclasses override ‘call(argv, opts)`. The dispatcher invokes `run(argv)`.



70
71
72
73
# File 'lib/envspec/cli/command.rb', line 70

def run(argv)
  rest = parser.parse(argv)
  call(rest, @opts)
end