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
18 changes: 14 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const yazap = b.addModule("yazap", .{ .root_source_file = b.path("src/lib.zig") });
const yazap = b.addModule("yazap", .{
.root_source_file = b.path("src/lib.zig"),
.target = b.graph.host,
});

testStep(b);
examplesStep(b, yazap);
}

fn testStep(b: *std.Build) void {
// Test file information.
const tests = b.addTest(.{ .root_source_file = b.path("src/test.zig") });
const test_module = b.addModule("test", .{
.root_source_file = b.path("src/test.zig"),
.target = b.graph.host,
});
const tests = b.addTest(.{ .root_module = test_module });
// This runs the unit tests.
const runner = b.addRunArtifact(tests);

Expand All @@ -35,11 +42,14 @@ fn examplesStep(b: *std.Build, yazap: *std.Build.Module) void {
const example_name = std.fs.path.stem(example_file_path.getDisplayName());

// Binary information of an example.
const executable = b.addExecutable(.{
.name = example_name,
const example_exe_module = b.addModule(example_name, .{
.root_source_file = example_file_path,
.target = b.graph.host,
});
const executable = b.addExecutable(.{
.name = example_name,
.root_module = example_exe_module,
});
// Add yazap as a dependency.
executable.root_module.addImport("yazap", yazap);

Expand Down
14 changes: 9 additions & 5 deletions src/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const HelpMessageWriter = @import("HelpMessageWriter.zig");
const Parser = @import("parser/Parser.zig");
const ParseResult = @import("parser/ParseResult.zig");
const YazapError = @import("error.zig").YazapError;
const help_message_buffer_size = 4096;

/// Top level allocator for the entire library.
allocator: Allocator,
Expand All @@ -31,7 +32,7 @@ process_args: ?[]const [:0]u8 = null,
pub fn init(allocator: Allocator, name: []const u8, description: ?[]const u8) App {
return App{
.allocator = allocator,
.command = Command.init(allocator, name, description),
.command = Command.init(allocator, name, description) catch { @panic("failed to create Command"); }
};
}

Expand Down Expand Up @@ -66,7 +67,7 @@ pub fn deinit(self: *App) void {
/// var subcmd1 = app.createCommand("subcmd1", "First Subcommand");
/// ```
pub fn createCommand(self: *App, name: []const u8, description: ?[]const u8) Command {
return Command.init(self.allocator, name, description);
return Command.init(self.allocator, name, description) catch { @panic("failed to create Command"); };
}

/// Returns a pointer to the root `Command` of the application.
Expand Down Expand Up @@ -130,7 +131,8 @@ pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!ArgMatches {
};

if (result.getCommandContainingHelpFlag()) |command| {
var help_writer = HelpMessageWriter.init(command);
var buffer: [help_message_buffer_size]u8 = undefined;
var help_writer = HelpMessageWriter.init(command, &buffer);
try help_writer.write();
result.deinit();
self.deinit();
Expand Down Expand Up @@ -166,7 +168,8 @@ pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!ArgMatches {
/// ```
pub fn displayHelp(self: *App) YazapError!void {
if (self.parse_result) |parse_result| {
var help_writer = HelpMessageWriter.init(parse_result.getCommand());
var buffer: [help_message_buffer_size]u8 = undefined;
var help_writer = HelpMessageWriter.init(parse_result.getCommand(), &buffer);
try help_writer.write();
}
}
Expand Down Expand Up @@ -201,7 +204,8 @@ pub fn displaySubcommandHelp(self: *App) YazapError!void {
const parse_result = self.parse_result orelse return;

if (parse_result.getActiveSubcommand()) |subcmd| {
var help_writer = HelpMessageWriter.init(subcmd);
var buffer: [help_message_buffer_size]u8 = undefined;
var help_writer = HelpMessageWriter.init(subcmd, &buffer);
try help_writer.write();
}
}
25 changes: 13 additions & 12 deletions src/Command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mem = std.mem;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const EnumSet = std.EnumSet;
const default_init_array_capacity = 10;

/// Represents the different parsing behaviors that can be applied to a
/// command.
Expand Down Expand Up @@ -41,26 +42,26 @@ properties: EnumSet(Property) = .{},
/// var subcmd1 = app.createCommand("subcmd1", "First Subcommand");
/// var subcmd2 = app.createCommand("subcmd2", "Second Subcommand");
/// ```
pub fn init(allocator: Allocator, name: []const u8, description: ?[]const u8) Command {
pub fn init(allocator: Allocator, name: []const u8, description: ?[]const u8) !Command {
return Command{
.allocator = allocator,
.name = name,
.description = description,
.positional_args = ArrayList(Arg).init(allocator),
.options = ArrayList(Arg).init(allocator),
.subcommands = ArrayList(Command).init(allocator),
.positional_args = try ArrayList(Arg).initCapacity(allocator, default_init_array_capacity),
.options = try ArrayList(Arg).initCapacity(allocator, default_init_array_capacity),
.subcommands = try ArrayList(Command).initCapacity(allocator, default_init_array_capacity),
};
}

/// Deallocates all allocated memory.
pub fn deinit(self: *Command) void {
self.positional_args.deinit();
self.options.deinit();
self.positional_args.deinit(self.allocator);
self.options.deinit(self.allocator);

for (self.subcommands.items) |*subcommand| {
subcommand.deinit();
}
self.subcommands.deinit();
self.subcommands.deinit(self.allocator);
}

/// Appends the new argument to the list of arguments.
Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn addArg(self: *Command, arg: Arg) !void {

// If its not a positional argument, append it and return.
if (!is_positional) {
return self.options.append(new_arg);
return self.options.append(self.allocator, new_arg);
}

// Its a positonal argument.
Expand All @@ -113,14 +114,14 @@ pub fn addArg(self: *Command, arg: Arg) !void {
}
}
// No duplication; append it.
return self.positional_args.append(new_arg);
return self.positional_args.append(self.allocator, new_arg);
}

// If the position is not set and if its the first positional argument
// then return immediately by giving it first position.
if (self.positional_args.items.len == 0) {
new_arg.setIndex(1);
return self.positional_args.append(new_arg);
return self.positional_args.append(self.allocator, new_arg);
}

// If the position is not set and if its not first positional argument
Expand All @@ -136,7 +137,7 @@ pub fn addArg(self: *Command, arg: Arg) !void {
}

new_arg.setIndex(current_position + 1);
try self.positional_args.append(new_arg);
try self.positional_args.append(self.allocator, new_arg);
}

/// Appends multiple arguments to the list of arguments.
Expand Down Expand Up @@ -179,7 +180,7 @@ pub fn addArgs(self: *Command, args: []const Arg) !void {
/// try root.addSubcommand(test);
/// ```
pub fn addSubcommand(self: *Command, new_subcommand: Command) !void {
return self.subcommands.append(new_subcommand);
return self.subcommands.append(self.allocator, new_subcommand);
}

/// Appends multiple subcommands to the list of subcommands.
Expand Down
Loading
Loading