authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2025-08-17 10:01:37-04:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2025-08-30 22:36:30-04:00
logf27e5649a5bf2969ba04e1415aaf1985bf070211
treeb696aa71ff614d9db8524e3704655c32b266b8a8
parent7b386cc93f9f8ca723720d0905f24506b8b9f852

exit(0) on --help


1 files changed, 8 insertions(+), 8 deletions(-)

lib/std/cli.zig+8-8
......@@ -112,7 +112,7 @@ pub const Error = error{
112112/// If `options.prog` is `null`, then the final path component of `argv[0]` is used by default.
113113///
114114/// If a parsing/validation error occurs or the `--help` arg is given,
115/// this function calls `std.process.exit` with an error status unless `options.exit` is set to `false`,
115/// this function calls `std.process.exit` with `1` and `0` respectively unless `options.exit` is set to `false`,
116116/// in which case parsing/validation errors return `error.Usage` and `--help` returns `error.Help`.
117117/// Allocator errors are always returned from the function.
118118///
......@@ -165,7 +165,7 @@ test parse {
165165///
166166/// If a parsing/validation error occurs or the `--help` arg is given,
167167/// this function returns `error.Usage` or `error.Help` respectively,
168/// unless `options.exit` is set to `true`, in which case `std.process.exit` is called with an error status instead.
168/// unless `options.exit` is set to `true`, in which case `std.process.exit` is called with `1` or `0` respectively.
169169/// Allocator errors are always returned from the function.
170170///
171171/// An `ArenaAllocator` is recommended to cleanup the memory allocated from this function;
......@@ -187,7 +187,7 @@ pub fn parseIter(comptime Args: type, arena: Allocator, iter: anytype, options:
187187///
188188/// If a parsing/validation error occurs or the `--help` arg is given,
189189/// this function returns `error.Usage` or `error.Help` respectively,
190/// unless `options.exit` is set to `true`, in which case `std.process.exit` is called with an error status instead.
190/// unless `options.exit` is set to `true`, in which case `std.process.exit` is called with `1` or `0` respectively.
191191/// Allocator errors are always returned from the function.
192192///
193193/// An `ArenaAllocator` is recommended to cleanup the memory allocated from this function;
......@@ -296,7 +296,7 @@ fn innerParse(comptime Args: type, allocator: Allocator, iter: anytype, prog: []
296296 printGeneratedHelp(writer, prog, named_info);
297297 }
298298 if (exit_on_error) {
299 std.process.exit(1);
299 std.process.exit(0);
300300 }
301301 return error.Help;
302302 }
......@@ -437,7 +437,7 @@ fn checkArgsType(comptime Args: type) void {
437437
438438 inline for (@typeInfo(args_fields[0].type).@"struct".fields) |field| {
439439 if (field.is_comptime) @compileError("comptime fields are not supported: " ++ field.name);
440 if (comptime mem.eql(u8, field.name, "help")) @compileError("A field named help is not allowed. to provide custom help formatting, give options.writer and handle error.Help");
440 if (comptime mem.eql(u8, field.name, "help")) @compileError("A field named help is not allowed. add a `pub const help = \"...\";` to your `Args` to provide a custom help string.");
441441 if (comptime mem.startsWith(u8, field.name, "no-")) @compileError("Field name starts with @\"no-\": " ++ field.name ++ ". Note: use a bool type field, and --<name> and --no-<name> will turn it on and off.");
442442 if (comptime mem.indexOfScalar(u8, field.name, '=') != null) @compileError("Field name contains @\"=\": " ++ field.name);
443443
......@@ -502,10 +502,10 @@ test @"error" {
502502 const args = try parseSlice(Args, arena.allocator(), &[_][]const u8{ "--output=o.txt", "i.txt" }, .{});
503503
504504 if (std.fs.path.isAbsolute(args.named.output)) {
505 return std.cli.@"error"("--output must not be absolute: {s}", .{args.named.output}, .{ .exit_on_error = false });
505 return std.cli.@"error"("--output must not be absolute: {s}", .{args.named.output}, .{ .exit = false });
506506 }
507507 if (args.positional.len > 1) {
508 return std.cli.@"error"("expected exactly 1 positional arg", .{}, .{ .exit_on_error = false });
508 return std.cli.@"error"("expected exactly 1 positional arg", .{}, .{ .exit = false });
509509 }
510510}
511511
......@@ -633,7 +633,7 @@ inline fn quoteIfEmpty(comptime s: []const u8) []const u8 {
633633}
634634
635635var failing_writer: Writer = .failing;
636const silent_options = Options{ .writer = &failing_writer };
636const silent_options = Options{ .writer = &failing_writer, .exit = false };
637637
638638test "usage errors" {
639639 var arena: std.heap.ArenaAllocator = .init(testing.allocator);