Class: OKF::TUI::CLI

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

Overview

okf tui's front end, and the only layer that parses argv, prints, and chooses the exit code. Everything beneath it returns data.

There is one door: this gem ships no executable, and the plugin seam (lib/okf/plugin.rb) hands argv and the streams straight here. So this is the whole argument grammar, with nothing to keep a second one in step with.

The argument shape mirrors okf server: naming directories is an ad-hoc look at them and never enrols them in the registry. Registering stays an explicit act — here, the a key in the bundles view.

Output streams are injected (out:/err:) so the whole surface is driven in tests without a real terminal, the same contract OKF::CLI keeps.

Constant Summary collapse

USAGE =
<<~TEXT
  usage: okf tui [options] [DIR|@slug...]

      (no arguments)     every bundle in the registry
      DIR...             those bundles, ad-hoc — the registry is left alone
      @slug              a registered bundle; bare @ is the registry default
      @group             every bundle a registry group resolves to

  options:
      -v, --version      print the version
      -h, --help         print this message

  The registry is the project-local .okf-registry.json when one is on the
  path up from here, and $OKF_HOME (default ~/.okf) otherwise — whichever
  one every other `okf` verb run from here resolves to. OKF_NO_DISCOVERY=1
  forces the global one.
TEXT
OK =

Exit codes, the same contract the okf CLI keeps.

0
FAILURE =
1
USAGE_ERROR =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, out: $stdout, err: $stderr, input: $stdin) ⇒ CLI

Returns a new instance of CLI.



50
51
52
53
54
55
# File 'lib/okf/tui/cli.rb', line 50

def initialize(argv, out: $stdout, err: $stderr, input: $stdin)
  @argv = argv.dup
  @out = out
  @err = err
  @input = input
end

Class Method Details

.run(argv, out: $stdout, err: $stderr, input: $stdin) ⇒ Object



46
47
48
# File 'lib/okf/tui/cli.rb', line 46

def self.run(argv, out: $stdout, err: $stderr, input: $stdin)
  new(argv, out: out, err: err, input: input).run
end

Instance Method Details

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/okf/tui/cli.rb', line 57

def run
  refs = []

  until @argv.empty?
    argument = @argv.shift

    case argument
    when "-h", "--help" then return print_and_exit(USAGE, OK)
    when "-v", "--version" then return print_and_exit("okf-tui #{OKF::TUI::VERSION}\n", OK)
    else
      return usage_error("unknown option: #{argument}") if argument.start_with?("-")

      # Not checked here on purpose: a positional may be a directory or an
      # @ref, and only the registry can tell whether `@mkt` names anything.
      # Refs resolves them together, so one place decides and one place
      # reports.
      refs << argument
    end
  end

  start(refs)
end