diff options
| author | 2024-11-04 15:44:36 +0100 | |
|---|---|---|
| committer | 2024-11-04 15:44:36 +0100 | |
| commit | cfa86f64ea1e94f8579f46816632f3c6ce7e09ef (patch) | |
| tree | 3bac7f9321bf1f590a4a7d9a1a68fdf195d5b2a3 /clap.zig | |
| parent | fix: Subcommand example comments needs a scroll to be read (diff) | |
| download | zig-clap-cfa86f64ea1e94f8579f46816632f3c6ce7e09ef.tar.gz zig-clap-cfa86f64ea1e94f8579f46816632f3c6ce7e09ef.tar.xz zig-clap-cfa86f64ea1e94f8579f46816632f3c6ce7e09ef.zip | |
chore: Update to latest version of zig
Diffstat (limited to '')
| -rw-r--r-- | clap.zig | 47 |
1 files changed, 38 insertions, 9 deletions
| @@ -744,7 +744,7 @@ pub fn parseEx( | |||
| 744 | const allocator = opt.allocator; | 744 | const allocator = opt.allocator; |
| 745 | 745 | ||
| 746 | var positional_count: usize = 0; | 746 | var positional_count: usize = 0; |
| 747 | var positionals = Positionals(Id, params, value_parsers, .list){}; | 747 | var positionals = initPositionals(Id, params, value_parsers, .list); |
| 748 | errdefer deinitPositionals(&positionals, allocator); | 748 | errdefer deinitPositionals(&positionals, allocator); |
| 749 | 749 | ||
| 750 | var arguments = Arguments(Id, params, value_parsers, .list){}; | 750 | var arguments = Arguments(Id, params, value_parsers, .list){}; |
| @@ -835,7 +835,7 @@ pub fn parseEx( | |||
| 835 | } | 835 | } |
| 836 | 836 | ||
| 837 | // We are done parsing, but our positionals are stored in lists, and not slices. | 837 | // We are done parsing, but our positionals are stored in lists, and not slices. |
| 838 | var result_positionals = Positionals(Id, params, value_parsers, .slice){}; | 838 | var result_positionals: Positionals(Id, params, value_parsers, .slice) = undefined; |
| 839 | inline for (&result_positionals, &positionals) |*res_pos, *pos| { | 839 | inline for (&result_positionals, &positionals) |*res_pos, *pos| { |
| 840 | switch (@typeInfo(@TypeOf(pos.*))) { | 840 | switch (@typeInfo(@TypeOf(pos.*))) { |
| 841 | .@"struct" => res_pos.* = try pos.toOwnedSlice(allocator), | 841 | .@"struct" => res_pos.* = try pos.toOwnedSlice(allocator), |
| @@ -892,21 +892,21 @@ fn Positionals( | |||
| 892 | continue; | 892 | continue; |
| 893 | 893 | ||
| 894 | const T = ParamType(Id, param, value_parsers); | 894 | const T = ParamType(Id, param, value_parsers); |
| 895 | const default_value = switch (param.takes_value) { | 895 | const FieldT = switch (param.takes_value) { |
| 896 | .none => continue, | 896 | .none => continue, |
| 897 | .one => @as(?T, null), | 897 | .one => ?T, |
| 898 | .many => switch (multi_arg_kind) { | 898 | .many => switch (multi_arg_kind) { |
| 899 | .slice => @as([]const T, &[_]T{}), | 899 | .slice => []const T, |
| 900 | .list => std.ArrayListUnmanaged(T){}, | 900 | .list => std.ArrayListUnmanaged(T), |
| 901 | }, | 901 | }, |
| 902 | }; | 902 | }; |
| 903 | 903 | ||
| 904 | fields[i] = .{ | 904 | fields[i] = .{ |
| 905 | .name = std.fmt.comptimePrint("{}", .{i}), | 905 | .name = std.fmt.comptimePrint("{}", .{i}), |
| 906 | .type = @TypeOf(default_value), | 906 | .type = FieldT, |
| 907 | .default_value = @ptrCast(&default_value), | 907 | .default_value = null, |
| 908 | .is_comptime = false, | 908 | .is_comptime = false, |
| 909 | .alignment = @alignOf(@TypeOf(default_value)), | 909 | .alignment = @alignOf(FieldT), |
| 910 | }; | 910 | }; |
| 911 | i += 1; | 911 | i += 1; |
| 912 | } | 912 | } |
| @@ -919,6 +919,35 @@ fn Positionals( | |||
| 919 | } }); | 919 | } }); |
| 920 | } | 920 | } |
| 921 | 921 | ||
| 922 | fn initPositionals( | ||
| 923 | comptime Id: type, | ||
| 924 | comptime params: []const Param(Id), | ||
| 925 | comptime value_parsers: anytype, | ||
| 926 | comptime multi_arg_kind: MultiArgKind, | ||
| 927 | ) Positionals(Id, params, value_parsers, multi_arg_kind) { | ||
| 928 | var res: Positionals(Id, params, value_parsers, multi_arg_kind) = undefined; | ||
| 929 | |||
| 930 | comptime var i: usize = 0; | ||
| 931 | inline for (params) |param| { | ||
| 932 | const longest = comptime param.names.longest(); | ||
| 933 | if (longest.kind != .positional) | ||
| 934 | continue; | ||
| 935 | |||
| 936 | const T = ParamType(Id, param, value_parsers); | ||
| 937 | res[i] = switch (param.takes_value) { | ||
| 938 | .none => continue, | ||
| 939 | .one => @as(?T, null), | ||
| 940 | .many => switch (multi_arg_kind) { | ||
| 941 | .slice => @as([]const T, &[_]T{}), | ||
| 942 | .list => std.ArrayListUnmanaged(T){}, | ||
| 943 | }, | ||
| 944 | }; | ||
| 945 | i += 1; | ||
| 946 | } | ||
| 947 | |||
| 948 | return res; | ||
| 949 | } | ||
| 950 | |||
| 922 | /// Deinitializes a tuple of type `Positionals`. Since the `Positionals` type is generated, and we | 951 | /// Deinitializes a tuple of type `Positionals`. Since the `Positionals` type is generated, and we |
| 923 | /// cannot add the deinit declaration to it, we declare it here instead. | 952 | /// cannot add the deinit declaration to it, we declare it here instead. |
| 924 | fn deinitPositionals(positionals: anytype, allocator: std.mem.Allocator) void { | 953 | fn deinitPositionals(positionals: anytype, allocator: std.mem.Allocator) void { |