Skip to content

Commit

Permalink
refactor(App): Storing ArgMatches is redundant
Browse files Browse the repository at this point in the history
Signed-off-by: prajwalch <[email protected]>
  • Loading branch information
prajwalch committed Sep 9, 2024
1 parent a8b9749 commit d24c810
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Once you have finished adding all the arguments and subcommands, call `App.parse
to start parsing the arguments given to the current process. This function internally utilizes
[`std.process.argsAlloc`](https://ziglang.org/documentation/master/std/#A;std:process.argsAlloc)
to obtain the raw arguments. Alternatively, you can use `App.parseFrom()` and pass your own raw
arguments, which can be useful during testing. Both functions return a constant pointer to
arguments, which can be useful during testing. Both functions returns
[`ArgMatches`](https://prajwalch.github.io/yazap/#A;lib:ArgMatches).

```zig
Expand Down
13 changes: 3 additions & 10 deletions src/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ command: Command,
///
/// It is not intended for direct access, use `ArgMatches` instead.
parse_result: ?ParseResult = null,
/// Public container to query parse result.
///
/// It cab be access directly but for the most part `parseFrom` or `parseProcess`
/// returns it.
arg_matches: ?ArgMatches = null,
/// Raw buffer containing command line arguments.
process_args: ?[]const [:0]u8 = null,

Expand Down Expand Up @@ -58,7 +53,6 @@ pub fn deinit(self: *App) void {
}
self.command.deinit();
self.parse_result = null;
self.arg_matches = null;
}

/// Creates a new `Command` with given name and optional description.
Expand Down Expand Up @@ -105,7 +99,7 @@ pub fn rootCommand(self: *App) *Command {
///
/// const matches = try app.parseProcess();
/// ```
pub fn parseProcess(self: *App) YazapError!(*const ArgMatches) {
pub fn parseProcess(self: *App) YazapError!ArgMatches {
self.process_args = try std.process.argsAlloc(self.allocator);
return self.parseFrom(self.process_args.?[1..]);
}
Expand All @@ -124,7 +118,7 @@ pub fn parseProcess(self: *App) YazapError!(*const ArgMatches) {
///
/// const matches = try app.parseFrom(&.{ "arg1", "--some-option" "subcmd" });
/// ```
pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!(*const ArgMatches) {
pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!ArgMatches {
var parser = Parser.init(self.allocator, argv, self.rootCommand());
var result = parser.parse() catch |err| {
// Don't clutter the test result with error messages.
Expand All @@ -143,8 +137,7 @@ pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!(*const ArgM
}

self.parse_result = result;
self.arg_matches = ArgMatches{ .parse_result = &self.parse_result.? };
return &self.arg_matches.?;
return ArgMatches{ .parse_result = &self.parse_result.? };
}

/// Displays the overall usage and description of the application.
Expand Down

0 comments on commit d24c810

Please sign in to comment.