-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.zig
More file actions
170 lines (152 loc) · 5.4 KB
/
Copy pathcli.zig
File metadata and controls
170 lines (152 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const std = @import("std");
pub const version = "0.1.0";
pub const default_config_path = "runmux.json";
pub const Command = enum {
run,
check,
init,
list,
help,
version,
};
pub const Options = struct {
command: Command = .run,
config_path: []const u8 = default_config_path,
profile_name: ?[]const u8 = null,
force: bool = false,
plain: bool = false,
log_dir: ?[]const u8 = null,
exit_on_critical_failure: bool = false,
theme_name: ?[]const u8 = null,
};
pub const ParseError = error{
MissingValue,
UnknownCommand,
UnknownOption,
TooManyCommands,
};
pub fn parse(args: []const [:0]const u8) ParseError!Options {
var result: Options = .{};
var command_seen = false;
var index: usize = 1;
while (index < args.len) : (index += 1) {
const arg = args[index];
if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
result.command = .help;
return result;
}
if (std.mem.eql(u8, arg, "--version")) {
result.command = .version;
return result;
}
if (std.mem.eql(u8, arg, "--config")) {
index += 1;
if (index >= args.len) return error.MissingValue;
result.config_path = args[index];
continue;
}
if (std.mem.eql(u8, arg, "--profile")) {
index += 1;
if (index >= args.len) return error.MissingValue;
result.profile_name = args[index];
continue;
}
if (std.mem.eql(u8, arg, "--force")) {
result.force = true;
continue;
}
if (std.mem.eql(u8, arg, "--plain")) {
result.plain = true;
continue;
}
if (std.mem.eql(u8, arg, "--log-dir")) {
index += 1;
if (index >= args.len) return error.MissingValue;
result.log_dir = args[index];
continue;
}
if (std.mem.eql(u8, arg, "--exit-on-critical-failure")) {
result.exit_on_critical_failure = true;
continue;
}
if (std.mem.eql(u8, arg, "--theme")) {
index += 1;
if (index >= args.len) return error.MissingValue;
result.theme_name = args[index];
continue;
}
if (std.mem.startsWith(u8, arg, "--")) return error.UnknownOption;
if (command_seen) return error.TooManyCommands;
command_seen = true;
result.command = parseCommand(arg) orelse return error.UnknownCommand;
}
return result;
}
fn parseCommand(arg: []const u8) ?Command {
if (std.mem.eql(u8, arg, "run")) return .run;
if (std.mem.eql(u8, arg, "check")) return .check;
if (std.mem.eql(u8, arg, "init")) return .init;
if (std.mem.eql(u8, arg, "list")) return .list;
return null;
}
pub const help_text =
\\runmux - lightweight TUI command runner
\\
\\Usage:
\\ runmux run [--config runmux.json] [--profile dev] [--plain] [--log-dir logs] [--exit-on-critical-failure] [--theme dark|light|mono]
\\ runmux check [--config runmux.json] [--profile dev]
\\ runmux init [--config runmux.json] [--force]
\\ runmux list [--config runmux.json] [--profile dev]
\\ runmux --help
\\ runmux --version
\\
\\TUI keys:
\\ Up/k, Down/j select Enter start/stop r restart
\\ a start all x stop all Tab log mode
\\ / search i stdin s/f/u filter
\\ p pause follow ? help q or Ctrl+C quit
\\
;
test "parse defaults to run" {
const args = [_][:0]const u8{"runmux"};
const opts = try parse(&args);
try std.testing.expectEqual(Command.run, opts.command);
try std.testing.expectEqualStrings(default_config_path, opts.config_path);
}
test "parse command and options" {
const args = [_][:0]const u8{ "runmux", "list", "--config", "x.json", "--profile", "dev" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.list, opts.command);
try std.testing.expectEqualStrings("x.json", opts.config_path);
try std.testing.expectEqualStrings("dev", opts.profile_name.?);
}
test "parse plain run option" {
const args = [_][:0]const u8{ "runmux", "run", "--plain" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.run, opts.command);
try std.testing.expect(opts.plain);
}
test "parse force init option" {
const args = [_][:0]const u8{ "runmux", "init", "--force" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.init, opts.command);
try std.testing.expect(opts.force);
}
test "parse log dir option" {
const args = [_][:0]const u8{ "runmux", "run", "--log-dir", "logs" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.run, opts.command);
try std.testing.expectEqualStrings("logs", opts.log_dir.?);
}
test "parse exit on critical failure option" {
const args = [_][:0]const u8{ "runmux", "run", "--exit-on-critical-failure" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.run, opts.command);
try std.testing.expect(opts.exit_on_critical_failure);
}
test "parse theme option" {
const args = [_][:0]const u8{ "runmux", "run", "--theme", "mono" };
const opts = try parse(&args);
try std.testing.expectEqual(Command.run, opts.command);
try std.testing.expectEqualStrings("mono", opts.theme_name.?);
}