Class: OKF::CLI::Command

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

Overview

What every command inherits: the injected streams, and the shared surface a verb leans on — ref resolution, the flags several of them offer, the JSON emitters, the list-view printers.

A command answers four questions about itself (.id, .group, .help_rows, .hidden?) and one about a run (#call, returning an exit status). That is the whole contract, and it is the same one a plugin implements — there is no second, lesser interface for an addon, because a seam only the base gem can use is not a seam.

Privacy is the boundary, the one idea worth taking from Thor without taking Thor: #call is the entire public surface, so a helper added below can never become a verb by accident.

Constant Summary collapse

DUCK_TYPE =

What .register checks before admitting a command. Checked at registration rather than at dispatch, so a malformed addon fails where it is installed instead of the first time a user types its verb.

%i[id group help_rows hidden? new].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out:, err:, runner: nil, input: nil) ⇒ Command

runner: is the server's injected boot seam and input: the terminal a full-screen command needs. Both live here rather than on the two commands that want them, so construction is uniform: a plugin is built exactly the way a built-in is, and the CLI needs to know nothing about which is which.



57
58
59
60
61
62
63
64
# File 'lib/okf/cli/command.rb', line 57

def initialize(out:, err:, runner: nil, input: nil)
  @out = out
  @err = err
  @runner = runner
  @input = input
  @pretty = false
  @ref_slugs = {}
end

Class Method Details

.groupObject

Where the verb sits in the map okf help prints. CLI::GROUPS fixes the order; anything else — which is what a plugin gets by default — falls to the end, under its own heading.



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

def group
  :extension
end

.help_rowsObject

[ [ left-column, description ], … ] — one row per line of the map. A list rather than a pair because registry is an umbrella: five subcommands under one verb, each of which has to be findable alone.



42
43
44
# File 'lib/okf/cli/command.rb', line 42

def help_rows
  []
end

.hidden?Boolean

A command that works but is not advertised.

Returns:

  • (Boolean)


47
48
49
# File 'lib/okf/cli/command.rb', line 47

def hidden?
  false
end

.idObject

The verb this answers to, as a Symbol. The registry is keyed on it.

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/okf/cli/command.rb', line 28

def id
  raise NotImplementedError, "#{self}.id must name the verb it answers to"
end

Instance Method Details

#call(argv) ⇒ Object

The run. Returns the exit status; it never calls exit, and never writes anywhere but the injected streams.

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/okf/cli/command.rb', line 68

def call(argv)
  raise NotImplementedError, "#{self.class} must implement #call(argv) and return an exit status"
end