Skip to content

Commit d6ebcee

Browse files
fix: parse all args before processing files, consistent stderr output
Addresses PR review feedback: - Collect file paths first, then process after checking --help/--version - This allows 'ansilust file.ans --version' to show version instead of failing - Use consistent stderr_file.writeAll instead of mixing with std.debug.print
1 parent e472922 commit d6ebcee

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/main.zig

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const build_options = @import("build_options");
44

55
const version = build_options.version;
66

7+
// Helper for managed ArrayList in Zig 0.15
8+
fn ArrayList(comptime T: type) type {
9+
return std.array_list.AlignedManaged(T, null);
10+
}
11+
712
fn printVersion() void {
813
const stdout_file = std.fs.File{ .handle = std.posix.STDOUT_FILENO };
914
stdout_file.writeAll("ansilust " ++ version ++ "\n") catch {};
@@ -63,25 +68,30 @@ pub fn main() !void {
6368
const stdout_file = std.fs.File{ .handle = std.posix.STDOUT_FILENO };
6469
const stderr_file = std.fs.File{ .handle = std.posix.STDERR_FILENO };
6570

66-
var file_count: usize = 0;
71+
var file_paths = ArrayList([]const u8).init(allocator);
72+
defer file_paths.deinit();
73+
6774
var show_help = false;
6875
var show_version = false;
6976

77+
// Parse all arguments first, collect file paths
7078
while (args.next()) |arg| {
7179
if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
7280
show_help = true;
7381
} else if (std.mem.eql(u8, arg, "-V") or std.mem.eql(u8, arg, "--version")) {
7482
show_version = true;
7583
} else if (std.mem.startsWith(u8, arg, "-")) {
76-
std.debug.print("error: unknown option '{s}'\n", .{arg});
84+
var buf: [256]u8 = undefined;
85+
const msg = std.fmt.bufPrint(&buf, "error: unknown option '{s}'\n", .{arg}) catch "error: unknown option\n";
86+
stderr_file.writeAll(msg) catch {};
7787
stderr_file.writeAll("Try 'ansilust --help' for more information.\n") catch {};
7888
std.process.exit(1);
7989
} else {
80-
try processFile(allocator, arg);
81-
file_count += 1;
90+
try file_paths.append(arg);
8291
}
8392
}
8493

94+
// Handle flags before processing files
8595
if (show_version) {
8696
printVersion();
8797
return;
@@ -92,8 +102,13 @@ pub fn main() !void {
92102
return;
93103
}
94104

95-
if (file_count == 0) {
105+
if (file_paths.items.len == 0) {
96106
printHelp(stderr_file);
97107
std.process.exit(1);
98108
}
109+
110+
// Process files after all argument parsing is complete
111+
for (file_paths.items) |path| {
112+
try processFile(allocator, path);
113+
}
99114
}

0 commit comments

Comments
 (0)