Class: Smol::CLI

Inherits:
Object
  • Object
show all
Includes:
ConfigDisplay, Output
Defined in:
lib/smol/cli.rb

Instance Method Summary collapse

Methods included from Output

#banner, #check_result, #debug, #desc, #failure, #header, #hint, #info, #label, #nl, #out, #success, #table, #verbose, #warning

Constructor Details

#initialize(app, prompt:, history: true) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
# File 'lib/smol/cli.rb', line 9

def initialize(app, prompt:, history: true)
  @app = app
  @prompt = prompt
  @history = history
end

Instance Method Details

#run(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smol/cli.rb', line 15

def run(args)
  if args.empty?
    if @app.repl
      REPL.new(@app, prompt: @prompt, history: @history, history_file: history_file_path).run
    else
      usage
      exit 1
    end
    return
  end

  unless @app.cli
    failure "CLI mode is disabled"
    hint "run without arguments for interactive mode" if @app.repl
    exit 1
  end

  cmd_name, *cmd_args = args

  case cmd_name
  when "help", "-h", "--help"
    usage
    exit 1
  when "config"
    show_config
    return
  when "config:set"
    set_config(cmd_args[0], cmd_args[1])
    return
  end

  klass = @app.find_command(cmd_name)

  if klass.nil?
    usage
    exit 1
  end

  positional, opts = klass.parse_options(cmd_args)
  result = klass.new.call(*positional, **opts)

  if result == true || result == false
    exit(result ? 0 : 1)
  end
end