Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 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 @@ -49,8 +50,7 @@ pub const AppRunner = struct {
return self.allocSlice(command.Command, args);
}

pub const ArgumentError = error.ArgumentError;
pub const Error = Allocator.Error || error{ArgumentError};
pub const Error = Allocator.Error;

/// `getAction` returns the action function that should be called by the main app.
pub fn getAction(self: *Self, app: *const App) Error!command.ExecFn {
Expand All @@ -65,7 +65,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 +123,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
Loading