Rearchitect Regz (#63)

wch-ch32v003
Matt Knight 2 years ago committed by Matt Knight
parent 2b985fc898
commit 9627d4196c

@ -10,20 +10,22 @@ jobs:
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest] os: [ubuntu-latest, windows-latest]
step: [examples, test]
release: [release-safe=false, release-safe=true, release-fast=true, release-small=true]
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: with:
submodules: recursive submodules: recursive
- uses: goto-bus-stop/setup-zig@v1.3.0 - uses: goto-bus-stop/setup-zig@v2.0.1
with: with:
version: master version: master
- run: zig build - run: zig build ${{ matrix.step }} -D${{ matrix.release }}
lint: lint:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v1.3.0 - uses: goto-bus-stop/setup-zig@v2.0.1
with: with:
version: master version: master
- run: zig fmt --check . - run: zig fmt --check .

@ -6,31 +6,16 @@ pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_all_step = b.step("test", "Run all tests in all modes."); const test_step = b.step("test", "Run all tests in all modes.");
for ([_]bool{ true, false }) |stage1| { const tests = b.addTest("clap.zig");
for (std.meta.tags(std.builtin.Mode)) |test_mode| { tests.setBuildMode(mode);
const mode_str = @tagName(test_mode); tests.setTarget(target);
const stage1_str = if (stage1) "stage1" else "stage2"; test_step.dependOn(&tests.step);
const tests = b.addTest("clap.zig");
tests.setBuildMode(test_mode);
tests.setTarget(target);
tests.use_stage1 = stage1;
const test_step = b.step(
b.fmt("test-{s}-{s}", .{ stage1_str, mode_str }),
b.fmt("Run all tests with {s} compiler in {s}.", .{ stage1_str, mode_str }),
);
test_step.dependOn(&tests.step);
test_all_step.dependOn(test_step);
}
}
const example_step = b.step("examples", "Build examples"); const example_step = b.step("examples", "Build examples");
inline for (.{ inline for (.{
"simple", "simple",
"simple-ex", "simple-ex",
//"simple-error",
"streaming-clap", "streaming-clap",
"help", "help",
"usage", "usage",
@ -49,7 +34,7 @@ pub fn build(b: *Builder) void {
readme_step.dependOn(readme); readme_step.dependOn(readme);
const all_step = b.step("all", "Build everything and runs all tests"); const all_step = b.step("all", "Build everything and runs all tests");
all_step.dependOn(test_all_step); all_step.dependOn(test_step);
all_step.dependOn(example_step); all_step.dependOn(example_step);
all_step.dependOn(readme_step); all_step.dependOn(readme_step);

@ -36,13 +36,27 @@ pub const Names = struct {
return .{ .kind = .short, .name = casted }; return .{ .kind = .short, .name = casted };
} }
return .{ .kind = .positinal, .name = "" }; return .{ .kind = .positional, .name = "" };
} }
pub const Longest = struct { pub const Longest = struct {
kind: enum { long, short, positinal }, kind: Kind,
name: []const u8, name: []const u8,
}; };
pub const Kind = enum {
long,
short,
positional,
pub fn prefix(kind: Kind) []const u8 {
return switch (kind) {
.long => "--",
.short => "-",
.positional => "",
};
}
};
}; };
/// Whether a param takes no value (a flag), one value, or can be specified multiple times. /// Whether a param takes no value (a flag), one value, or can be specified multiple times.
@ -95,7 +109,7 @@ pub fn parseParamsEx(allocator: mem.Allocator, str: []const u8, end: *usize) ![]
errdefer list.deinit(); errdefer list.deinit();
try parseParamsIntoArrayListEx(&list, str, end); try parseParamsIntoArrayListEx(&list, str, end);
return list.toOwnedSlice(); return try list.toOwnedSlice();
} }
/// Takes a string and parses it into many Param(Help) at comptime. Returned is an array of /// Takes a string and parses it into many Param(Help) at comptime. Returned is an array of
@ -514,7 +528,7 @@ test "parseParams" {
}, },
.{ .{
.id = .{ .id = .{
.desc = .desc =
\\ This is \\ This is
\\ help spanning multiple \\ help spanning multiple
\\ lines \\ lines
@ -550,29 +564,22 @@ pub const Diagnostic = struct {
/// Default diagnostics reporter when all you want is English with no colors. /// Default diagnostics reporter when all you want is English with no colors.
/// Use this as a reference for implementing your own if needed. /// Use this as a reference for implementing your own if needed.
pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void { pub fn report(diag: Diagnostic, stream: anytype, err: anyerror) !void {
const Arg = struct { var longest = diag.name.longest();
prefix: []const u8, if (longest.kind == .positional)
name: []const u8, longest.name = diag.arg;
};
const a = if (diag.name.short) |*c|
Arg{ .prefix = "-", .name = @as(*const [1]u8, c)[0..] }
else if (diag.name.long) |l|
Arg{ .prefix = "--", .name = l }
else
Arg{ .prefix = "", .name = diag.arg };
switch (err) { switch (err) {
streaming.Error.DoesntTakeValue => try stream.print( streaming.Error.DoesntTakeValue => try stream.print(
"The argument '{s}{s}' does not take a value\n", "The argument '{s}{s}' does not take a value\n",
.{ a.prefix, a.name }, .{ longest.kind.prefix(), longest.name },
), ),
streaming.Error.MissingValue => try stream.print( streaming.Error.MissingValue => try stream.print(
"The argument '{s}{s}' requires a value but none was supplied\n", "The argument '{s}{s}' requires a value but none was supplied\n",
.{ a.prefix, a.name }, .{ longest.kind.prefix(), longest.name },
), ),
streaming.Error.InvalidArgument => try stream.print( streaming.Error.InvalidArgument => try stream.print(
"Invalid argument '{s}{s}'\n", "Invalid argument '{s}{s}'\n",
.{ a.prefix, a.name }, .{ longest.kind.prefix(), longest.name },
), ),
else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}), else => try stream.print("Error while parsing arguments: {s}\n", .{@errorName(err)}),
} }
@ -762,10 +769,10 @@ pub fn parseEx(
// fields to slices and return that. // fields to slices and return that.
var result_args = Arguments(Id, params, value_parsers, .slice){}; var result_args = Arguments(Id, params, value_parsers, .slice){};
inline for (meta.fields(@TypeOf(arguments))) |field| { inline for (meta.fields(@TypeOf(arguments))) |field| {
if (@typeInfo(field.field_type) == .Struct and if (@typeInfo(field.type) == .Struct and
@hasDecl(field.field_type, "toOwnedSlice")) @hasDecl(field.type, "toOwnedSlice"))
{ {
const slice = @field(arguments, field.name).toOwnedSlice(allocator); const slice = try @field(arguments, field.name).toOwnedSlice(allocator);
@field(result_args, field.name) = slice; @field(result_args, field.name) = slice;
} else { } else {
@field(result_args, field.name) = @field(arguments, field.name); @field(result_args, field.name) = @field(arguments, field.name);
@ -774,7 +781,7 @@ pub fn parseEx(
return ResultEx(Id, params, value_parsers){ return ResultEx(Id, params, value_parsers){
.args = result_args, .args = result_args,
.positionals = positionals.toOwnedSlice(), .positionals = try positionals.toOwnedSlice(),
.allocator = allocator, .allocator = allocator,
}; };
} }
@ -803,7 +810,7 @@ fn parseArg(
try @field(arguments, longest.name).append(allocator, value); try @field(arguments, longest.name).append(allocator, value);
}, },
}, },
.positinal => try positionals.append(try parser(arg.value.?)), .positional => try positionals.append(try parser(arg.value.?)),
} }
} }
@ -837,7 +844,7 @@ fn FindPositionalType(
fn findPositional(comptime Id: type, params: []const Param(Id)) ?Param(Id) { fn findPositional(comptime Id: type, params: []const Param(Id)) ?Param(Id) {
for (params) |param| { for (params) |param| {
const longest = param.names.longest(); const longest = param.names.longest();
if (longest.kind == .positinal) if (longest.kind == .positional)
return param; return param;
} }
@ -868,7 +875,7 @@ fn deinitArgs(
) void { ) void {
inline for (params) |param| { inline for (params) |param| {
const longest = comptime param.names.longest(); const longest = comptime param.names.longest();
if (longest.kind == .positinal) if (longest.kind == .positional)
continue; continue;
if (param.takes_value != .many) if (param.takes_value != .many)
continue; continue;
@ -900,7 +907,7 @@ fn Arguments(
var i: usize = 0; var i: usize = 0;
for (params) |param| { for (params) |param| {
const longest = param.names.longest(); const longest = param.names.longest();
if (longest.kind == .positinal) if (longest.kind == .positional)
continue; continue;
const T = ParamType(Id, param, value_parsers); const T = ParamType(Id, param, value_parsers);
@ -915,7 +922,7 @@ fn Arguments(
fields[i] = .{ fields[i] = .{
.name = longest.name, .name = longest.name,
.field_type = @TypeOf(default_value), .type = @TypeOf(default_value),
.default_value = @ptrCast(*const anyopaque, &default_value), .default_value = @ptrCast(*const anyopaque, &default_value),
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(@TypeOf(default_value)), .alignment = @alignOf(@TypeOf(default_value)),
@ -975,7 +982,9 @@ test "everything" {
test "empty" { test "empty" {
var iter = args.SliceIterator{ .args = &.{} }; var iter = args.SliceIterator{ .args = &.{} };
var res = try parseEx(u8, &.{}, parsers.default, &iter, .{ .allocator = testing.allocator }); var res = try parseEx(u8, &[_]Param(u8){}, parsers.default, &iter, .{
.allocator = testing.allocator,
});
defer res.deinit(); defer res.deinit();
} }
@ -1117,7 +1126,7 @@ pub fn help(
var first_paramter: bool = true; var first_paramter: bool = true;
for (params) |param| { for (params) |param| {
if (param.names.longest().kind == .positinal) if (param.names.longest().kind == .positional)
continue; continue;
if (!first_paramter) if (!first_paramter)
try writer.writeByteNTimes('\n', opt.spacing_between_parameters); try writer.writeByteNTimes('\n', opt.spacing_between_parameters);
@ -1690,7 +1699,7 @@ test "clap.help" {
/// [-abc] [--longa] [-d <T>] [--longb <T>] <T> /// [-abc] [--longa] [-d <T>] [--longb <T>] <T>
/// ///
/// First all none value taking parameters, which have a short name are printed, then non /// First all none value taking parameters, which have a short name are printed, then non
/// positional parameters and finally the positinal. /// positional parameters and finally the positional.
pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !void { pub fn usage(stream: anytype, comptime Id: type, params: []const Param(Id)) !void {
var cos = io.countingWriter(stream); var cos = io.countingWriter(stream);
const cs = cos.writer(); const cs = cos.writer();

@ -1,6 +1,6 @@
pkgs: pkgs:
clap: clap:
version: 0.5.0 version: 0.6.0
license: MIT license: MIT
description: Simple command line argument parsing library description: Simple command line argument parsing library
source_url: "https://github.com/Hejsil/zig-clap" source_url: "https://github.com/Hejsil/zig-clap"

@ -10,9 +10,9 @@ jobs:
aarch64-linux-gnu, aarch64-linux-gnu,
aarch64-linux-musl, aarch64-linux-musl,
aarch64-macos, aarch64-macos,
i386-linux-gnu, x86-linux-gnu,
i386-linux-musl, x86-linux-musl,
i386-windows, x86-windows,
x86_64-linux-gnu, x86_64-linux-gnu,
x86_64-linux-musl, x86_64-linux-musl,
x86_64-macos, x86_64-macos,

@ -32,16 +32,16 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1663083997, "lastModified": 1671652719,
"narHash": "sha256-tasWWaQUIhWkFhp3D+8ANMpVY0aU9AczfYnAWhSgyRE=", "narHash": "sha256-bKwe16yUenQNzsiXZ+iLLiC5PG6NAkoJLFx61O2rb1o=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "19bb831cc27d7bfac268de9986964ff8f6d7f0e7", "rev": "f4eddbd6a88c51e9a4693e2887cf4543917aa279",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nixos", "owner": "nixos",
"ref": "release-22.05", "ref": "release-22.11",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
@ -75,11 +75,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1663935107, "lastModified": 1671668597,
"narHash": "sha256-uCA5Y3cn2Jk15VBRCUIq1lIb7mFyi3j2lp9Up+FbjFY=", "narHash": "sha256-oD+Zx3IeXx2d91AiD5O74P4582iRPTPabCt6HDhagSo=",
"owner": "mitchellh", "owner": "mitchellh",
"repo": "zig-overlay", "repo": "zig-overlay",
"rev": "39778bb594f9eed1c2673b08d0c29d52b6f7dfb2", "rev": "64fcf1f2efcc63e97830e203f8e64ca20137ffa2",
"type": "github" "type": "github"
}, },
"original": { "original": {

@ -2,7 +2,7 @@
description = "libflightplan"; description = "libflightplan";
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/release-22.05"; nixpkgs.url = "github:nixos/nixpkgs/release-22.11";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
zig.url = "github:mitchellh/zig-overlay"; zig.url = "github:mitchellh/zig-overlay";
}; };

@ -164,7 +164,7 @@ pub fn create(
// Enable our `./configure` options. For bool-type fields we translate // Enable our `./configure` options. For bool-type fields we translate
// it to the `LIBXML_{field}_ENABLED` C define where field is uppercased. // it to the `LIBXML_{field}_ENABLED` C define where field is uppercased.
inline for (std.meta.fields(@TypeOf(opts))) |field| { inline for (std.meta.fields(@TypeOf(opts))) |field| {
if (field.field_type == bool and @field(opts, field.name)) { if (field.type == bool and @field(opts, field.name)) {
var nameBuf: [32]u8 = undefined; var nameBuf: [32]u8 = undefined;
const name = std.ascii.upperString(&nameBuf, field.name); const name = std.ascii.upperString(&nameBuf, field.name);
const define = try std.fmt.allocPrint(b.allocator, "-DLIBXML_{s}_ENABLED=1", .{name}); const define = try std.fmt.allocPrint(b.allocator, "-DLIBXML_{s}_ENABLED=1", .{name});

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save