Skip to content

Commit

Permalink
Print help on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sam701 committed Jan 25, 2025
1 parent 9a24b55 commit 224fa36
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/app_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const parser = @import("parser.zig");
const Parser = parser.Parser;
const Printer = @import("Printer.zig");
const command = @import("command.zig");
const help = @import("./help.zig");

pub const AppRunner = struct {
// This arena and its allocator is intended to be used only for the value references
Expand Down Expand Up @@ -65,7 +66,11 @@ pub const AppRunner = struct {
return action;
} else |err| {
processError(err, cr.error_data orelse unreachable, app);
return ArgumentError;
if (app.help_config.print_help_on_error) {
_ = std.io.getStdOut().write("\n") catch unreachable;
try help.print_command_help(app, try cr.command_path.toOwnedSlice(), cr.global_options);
}
std.posix.exit(1);
}
}

Expand Down Expand Up @@ -119,5 +124,4 @@ fn printError(app: *const App, comptime fmt: []const u8, args: anytype) void {
p.format(": ", .{});
p.format(fmt, args);
p.write(&.{'\n'});
std.posix.exit(1);
}
3 changes: 3 additions & 0 deletions src/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub const HelpConfig = struct {
color_option: []const u8 = "32",
/// Color for error messages in help.
color_error: []const u8 = "31;1",

/// Whether to print last command help on errors like missing arguments or options.
print_help_on_error: bool = true,
};

/// Structure representing a command.
Expand Down

0 comments on commit 224fa36

Please sign in to comment.