Hejsil / zig-clap

Command line argument parsing library
MIT License
939 stars 67 forks source link

build: expose 'clap' module for dependeees #92

Closed travisstaloch closed 1 year ago

travisstaloch commented 1 year ago

With this change i was able to use clap with the new package manager.

In my package's build.zig:

diff --git a/build.zig b/build.zig
index 93e123d..54a38d6 100644
--- a/build.zig
+++ b/build.zig
+    const zig_clap_pkg = b.dependency("clap", .{
+        .target = target,
+        .optimize = optimize,
+    });
+    const zig_clap = zig_clap_pkg.module("clap");
     const exe = b.addExecutable(.{ ... });
+    exe.addModule("zig-clap", zig_clap);

Note: before this patch, this change ^ resulted in an error. Something like 'can't find module clap'.

and my package's build.zig.zon:

$ zig version
0.11.0-dev.1796+c9e02d3e6

$ cat build.zig.zon 
.{
    .name = "mylib",
    .version = "0.0.1",

    .dependencies = .{
        .clap = .{
            .url = "https://github.com/travisstaloch/zig-clap/archive/refs/heads/master.tar.gz",
            .hash = "1220caf0689cafa7d546ef413f1c1cc5a278dfa1aa2e4de437d3ccee7527bf8e68cd",
        },
    }
}

I hope this is the correct way to do this. I haven't used the package manager much other than this. I got the idea from felix' similar repo: https://github.com/MasterQ32/zig-args/blob/master/build.zig#L7 . But i wanted to use the usage()/help() methods clap provides.

andrewrk commented 1 year ago

Also suggest this in your usage code:

-            .url = "https://github.com/travisstaloch/zig-clap/archive/refs/heads/master.tar.gz",
-            .hash = "1220caf0689cafa7d546ef413f1c1cc5a278dfa1aa2e4de437d3ccee7527bf8e68cd",
+            .url = "https://github.com/travisstaloch/zig-clap/archive/a4940c3dcff9a2a711ddf1ec9f2e1d64d34c63f0.tar.gz",

(don't forget to delete the hash field when changing the url field)

travisstaloch commented 1 year ago

Wow. you're quick :laughing: Thanks for the pointers. Will do! :+1:

Hejsil commented 1 year ago

I assume it would then also be nice to change the line

        example.addAnonymousModule("clap", .{ .source_file = .{ .path = "clap.zig" } });

to

        example.addModule("clap", clap_mod);
travisstaloch commented 1 year ago

sorry for the messy commits. i should have actually tried building locally. got mixed up between using b.createModule()/addModule(). let me know if i need to squash them.

travisstaloch commented 1 year ago

to

        example.addModule("clap", clap_mod);

Originally posted by @Hejsil in https://github.com/Hejsil/zig-clap/issues/92#issuecomment-1445353656

Would work if changed back to original:

const clap_mod = b.createModule(.{ .source_file = .{ .path = "clap.zig" } });
b.modules.put(b.dupe("clap"), clap_mod) catch @panic("OOM");

Let me know again if there is a better way.

Hejsil commented 1 year ago

to

        example.addModule("clap", clap_mod);

Originally posted by @Hejsil in #92 (comment)

Would work if changed back to original:

const clap_mod = b.createModule(.{ .source_file = .{ .path = "clap.zig" } });
b.modules.put(b.dupe("clap"), clap_mod) catch @panic("OOM");

Let me know again if there is a better way.

Hmm. Alright, then I have no idea how the package manager works yet :shrug:

I'll just merge this, as it is an improvement for people who are trying to use it. I'll look into further improvements at some later date.