Skip to content

Commit

Permalink
Merge pull request #26 from fardragon/fix-multivalue-behaviour
Browse files Browse the repository at this point in the history
fix: Fix `Arg.multiValuesOption` behaviour when single value is provided
  • Loading branch information
prajwalch authored Aug 9, 2024
2 parents c9ec8d4 + 8f53fcb commit e7a6d60
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "yazap",
.version = "0.5.1",
.version = "0.5.2",
.dependencies = .{
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
},
Expand Down
1 change: 1 addition & 0 deletions src/Arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub fn multiValuesOption(
arg.setMaxValues(max_values);
arg.setDefaultValuesDelimiter();
arg.setProperty(.takes_value);
arg.setProperty(.takes_multiple_values);
return arg;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ fn parseOptionValue(self: *Parser, arg: *const Arg, attached_value: ?[]const u8)
self.err.setContext(.{ .valid_arg = arg.name });
return Error.ArgValueNotProvided;
}
if (values.items.len == 1) {
if (values.items.len == 1 and !arg.hasProperty(.takes_multiple_values)) {
const value = values.pop();
try self.verifyValue(arg, value);
try self.putMatchedArg(arg, .{ .single = value });
Expand Down
11 changes: 9 additions & 2 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ pub const Arg = @import("Arg.zig");
pub const arg_matches = @import("arg_matches.zig");
pub const ArgMatches = arg_matches.ArgMatches;
pub const Command = @import("Command.zig");
pub const YazapError = @import("error.zig").YazapError;
pub const yazap_error = @import("error.zig");
pub const YazapError = yazap_error.YazapError;

test "emit docs" {
std.testing.refAllDeclsRecursive(@This());
comptime {
std.testing.refAllDecls(App);
std.testing.refAllDecls(Arg);
std.testing.refAllDecls(arg_matches);
std.testing.refAllDecls(Command);
std.testing.refAllDecls(yazap_error);
}
}
52 changes: 52 additions & 0 deletions src/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,58 @@ test "Option that takes single value" {
app.deinit();
}

test "multiValuesOption provided with single value short" {
var app = App.init(allocator, "clang", null);
defer app.deinit();

const srcs = Arg.multiValuesOption("sources", 's', null, 128);

// ex: clang sources...
try app.rootCommand().addArg(srcs);

const matches_short = try app.parseFrom(&.{ "-s", "f1" });
try testing.expectEqual(@as(usize, 1), matches_short.getMultiValues("sources").?.len);
}

test "multiValuesOption provided with single value long" {
var app = App.init(allocator, "clang", null);
defer app.deinit();

const srcs = Arg.multiValuesOption("sources", 's', null, 128);

// ex: clang sources...
try app.rootCommand().addArg(srcs);

const matches_short = try app.parseFrom(&.{ "--sources", "f1" });
try testing.expectEqual(@as(usize, 1), matches_short.getMultiValues("sources").?.len);
}

test "multiValuesOption provided with multiple values short" {
var app = App.init(allocator, "clang", null);
defer app.deinit();

const srcs = Arg.multiValuesOption("sources", 's', null, 128);

// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches = try app.parseFrom(&.{ "-s", "f1", "f2", "f3", "f4", "f5" });

try testing.expectEqual(@as(usize, 5), matches.getMultiValues("sources").?.len);
}

test "multiValuesOption provided with multiple values long" {
var app = App.init(allocator, "clang", null);
defer app.deinit();

const srcs = Arg.multiValuesOption("sources", 's', null, 128);

// ex: clang sources...
try app.rootCommand().addArg(srcs);
const matches = try app.parseFrom(&.{ "--sources", "f1", "f2", "f3", "f4", "f5" });

try testing.expectEqual(@as(usize, 5), matches.getMultiValues("sources").?.len);
}

test "Option that takes many/multiple values" {
var app = App.init(allocator, "clang", null);
errdefer app.deinit();
Expand Down

0 comments on commit e7a6d60

Please sign in to comment.