Class: EnvSpec::CLI::Command
- Inherits:
-
Object
- Object
- EnvSpec::CLI::Command
- 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
Class Method Summary collapse
- .banner(value = nil) ⇒ Object
- .description(value = nil) ⇒ Object
- .name(value = nil) ⇒ Object
- .summary(value = nil) ⇒ Object
Instance Method Summary collapse
- #build_parser ⇒ Object
- #call(_rest, _opts) ⇒ Object
- #configure(_parser, _opts) ⇒ Object
- #default_opts ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #parser ⇒ Object
-
#run(argv) ⇒ Object
Subclasses override ‘call(argv, opts)`.
Constructor Details
#initialize ⇒ Command
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
.banner(value = nil) ⇒ Object
24 25 26 27 |
# File 'lib/envspec/cli/command.rb', line 24 def (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_parser ⇒ Object
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. = self.class. || "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
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_opts ⇒ Object
39 40 41 |
# File 'lib/envspec/cli/command.rb', line 39 def default_opts {} end |
#help ⇒ Object
65 66 67 |
# File 'lib/envspec/cli/command.rb', line 65 def help parser.help end |
#parser ⇒ Object
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 |