Class: TUITD::CLI

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

Overview

Command-line interface for tui-td

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(argv = ARGV) ⇒ Object



8
9
10
# File 'lib/tui_td/cli.rb', line 8

def self.run(argv = ARGV)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



12
13
14
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tui_td/cli.rb', line 12

def run(argv)
  global_opts = {}
  command = nil
  command_opts = {}

  OptionParser.new do |opts|
    opts.banner = "Usage: tui-td <command> [options]"
    opts.separator ""
    opts.separator "Commands:"
    opts.separator "  serve              Start MCP server (JSON-RPC over stdio)"
    opts.separator "  capture <command>  Run once, capture and display state"
    opts.separator "  drive <command>    Drive a TUI interactively"
    opts.separator "  run <command>      Run a TUI app and show live output"
    opts.separator "  test <file.json>   Run JSON test file"
    opts.separator "  help [topic]       Show this help, or help test / help rspec"
    opts.separator ""
    opts.separator "Examples:"
    opts.separator "  tui-td capture \"ls -la\""
    opts.separator "  tui-td --screenshot out.png capture \"htop\" --timeout 5"
    opts.separator "  tui-td --html out.html capture \"glow README.md\""
    opts.separator "  tui-td -C /my/project capture \"make test\""
    opts.separator "  tui-td drive \"vim file.txt\" --rows 24 --cols 80"
    opts.separator "  tui-td test examples/echo_test.json"
    opts.separator "  tui-td serve"
    opts.separator ""
    opts.separator "Interactive commands (drive mode):"
    opts.separator "  state              Show terminal state as pretty JSON"
    opts.separator "  raw                Show raw ANSI output"
    opts.separator "  key <name>         Send keystroke (enter, tab, escape, up, down, left, right,"
    opts.separator "                     backspace, ctrl_c, ctrl_d)"
    opts.separator "  <text>             Send text to the TUI"
    opts.separator "  exit               Quit drive mode"
    opts.separator ""
    opts.separator "Global options:"

    opts.on("-r", "--rows N", Integer, "Terminal rows (default: 40)") do |r|
      global_opts[:rows] = r
    end
    opts.on("-c", "--cols N", Integer, "Terminal cols (default: 120)") do |c|
      global_opts[:cols] = c
    end
    opts.on("-t", "--timeout SECONDS", Integer, "Timeout in seconds (default: 30)") do |t|
      global_opts[:timeout] = t
    end
    opts.on("-C", "--chdir PATH", "Working directory for the command") do |d|
      global_opts[:chdir] = d
    end
    opts.on("--screenshot PATH", "Save screenshot (e.g., output.png)") do |p|
      global_opts[:screenshot] = p
    end
    opts.on("--html PATH", "Save HTML render (e.g., output.html)") do |p|
      global_opts[:html] = p
    end
    opts.on("--json", "Output state as compact JSON") do |_|
      global_opts[:format] = :json
    end
    opts.on("--pretty", "Output state as pretty JSON") do |_|
      global_opts[:format] = :pretty_json
    end
    opts.on("--text", "Output state as plain text table") do |_|
      global_opts[:format] = :text
    end
    opts.on("--version", "Show version") do
      puts "tui-td #{TUITD::VERSION}"
      exit 0
    end
    opts.on("-h", "--help", "Show help") do
      puts opts
      exit 0
    end
  end.permute!(argv)

  command = argv.shift
  command_opts[:args] = argv

  case command
  when "serve"
    cmd_serve(global_opts)
  when "run"
    cmd_run(command_opts, global_opts)
  when "drive"
    cmd_drive(command_opts, global_opts)
  when "capture"
    cmd_capture(command_opts, global_opts)
  when "test"
    cmd_test(command_opts, global_opts)
  when "help"
    topic = argv.shift
    case topic
    when "test"
      _help_test
    when "rspec"
      _help_rspec
    when nil
      _help_main
    else
      abort "Unknown help topic: #{topic.inspect}\nTry: tui-td help test, tui-td help rspec"
    end
  when nil
    _help_main
  else
    abort "Unknown command: #{command.inspect}\nUse tui-td --help for usage"
  end
end