Class: Prescient::CLI

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

Overview

Command-line interface for common Prescient operations.

Defined Under Namespace

Classes: UsageError

Constant Summary collapse

FORMATS =

Supported output formats.

Returns:

  • (Array<String>)

    Output format names

['text', 'json'].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, input:, output:, errors:) ⇒ CLI

Initialize a CLI runner with injectable streams.

Parameters:

  • arguments (Array<String>)

    Command-line arguments

  • input (IO)

    Input stream used for stdin prompts

  • output (IO)

    Output stream for command results

  • errors (IO)

    Output stream for diagnostics



38
39
40
41
42
43
# File 'lib/prescient/cli.rb', line 38

def initialize(arguments, input:, output:, errors:)
  @arguments = arguments.dup
  @input = input
  @output = output
  @errors = errors
end

Class Method Details

.run(arguments, input: $stdin, output: $stdout, errors: $stderr) ⇒ Integer

Run the CLI and return a process exit status.

Parameters:

  • arguments (Array<String>)

    Command-line arguments

  • input (IO) (defaults to: $stdin)

    Input stream used for stdin prompts

  • output (IO) (defaults to: $stdout)

    Output stream for command results

  • errors (IO) (defaults to: $stderr)

    Output stream for diagnostics

Returns:

  • (Integer)

    Process exit status



22
23
24
25
26
27
28
29
30
# File 'lib/prescient/cli.rb', line 22

def self.run(arguments, input: $stdin, output: $stdout, errors: $stderr)
  new(arguments, input:, output:, errors:).run
rescue UsageError, OptionParser::ParseError => e
  errors.puts "prescient: #{e.message}"
  2
rescue Prescient::Error => e
  errors.puts "prescient: #{e.message}"
  1
end

Instance Method Details

#runObject



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

def run
  command = @arguments.shift
  return print_help(2) unless command

  case command
  when 'providers' then providers
  when 'health' then health
  when 'generate' then generate
  when 'embed' then embed
  when 'config' then config
  when 'help', '--help', '-h' then print_help(0)
  else
    raise UsageError, "unknown command #{command.inspect}; run 'prescient help'"
  end
end