Hejsil / zig-clap

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

How do i install it? #61

Closed prajwalch closed 4 months ago

prajwalch commented 2 years ago

Hi i am new to zig and i would like to use this library for my project. But I don't see any install instructions on README so idk how can i use it.

Hejsil commented 2 years ago

Edit: Yo, it's me from the year 2024. Zig now has a package manager and the readme should now instruct users on how to install zig-clap. If the instructions are still unclear, do comment here so it can be improved :+1:

Original comment:

Right now, there is no standard way to install Zig libraries. There are a few common ways people do it:

Hope this helps. I'll leave this issue open until Zig has an official package solution. I can then make the choice to add something to the README or not.

prajwalch commented 2 years ago

Right now, there is no standard way to install Zig libraries. There are a few common ways people do it:

  • git submodule or copying and pasting the library into their own project

    • After this, you can add exe.addPackagePath("clap", "path/to/clap.zig"); to your build.zig file to use the library
  • Use an unofficial package manager such as zigmod or gyro

    • Look at the docs for these package managers for how to install packages using them. They should both be able to install zig-clap as far as I know.

Hope this helps. I'll leave this issue open until Zig has an official package solution. I can then make the choice to add something to the README or not.

Thank you. I decided to use it by using git submodule.

If anyone has a same issue as me here's how you can use it.

  1. On your project root directory make a directory name libs.
  2. Run git submodule add https://github.com/Hejsil/zig-clap libs/zig-clap
  3. Then add exe.addPackagePath("clap", "libs/zig-clap/clap.zig") on your build.zig
  4. Now you can import clap on your source file as const clap = @import("clap");
punowo commented 1 year ago

Sorry, but currently how do I do this ? exe.addPackagePath didn't seem to work. Should I just read more on the package manager ?

prajwalch commented 1 year ago

Sorry, but currently how do I do this ? exe.addPackagePath didn't seem to work. Should I just read more on the package manager ?

This used to work so i am not sure whether this will work or not nowadays but you can try. Ping me if that doesn't work ;)

exe.addAnonymousModule("zig-clap", .{
    .source_file = .{ .path = "libs/zig-clap/clap.zig" },
});
punowo commented 1 year ago
exe.addAnonymousModule("zig-clap", .{
    .source_file = .{ .path = "libs/zig-clap/clap.zig" },
});

That works. Thank you.

Hejsil commented 1 year ago

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

build.zig

const clap = b.dependecy("clap", .{});
exe.addModule("clap", clap.module("clap"));

Or, if you need multiple things to depend on clap but not use the package manager:

const clap = b.createModule(.{
    .source_file = .{ .path = "lib/zig-clap/clap.zig" },
});
exe.addModule("clap", clap);
yozachar commented 1 year ago

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

@Hejsil how do I make zig tell me that?

.{ .name = "themepak", .version = "0.1.0", .dependencies = .{
    .clap = .{
        .url = "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz",
        .hash = "288d62e9fb8cbbe5f67c2644d664e3550d8b5a68646c6b32d9881304136f260b",
    },
} }
$ zig build
/home/us-er/Documents/workspace/github/themepak/build.zig.zon:4:17: error: unsupported hash function: only sha2-256 is supported
        .hash = "288d62e9fb8cbbe5f67c2644d664e3550d8b5a68646c6b32d9881304136f260b",
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
yozachar commented 1 year ago

Oh I got it, first comment or remove .hash = ..., line, then run zig build, it'll will then tell you.

$ zig build
Fetch Packages... clap... /home/us-er/Documents/workspace/github/themepak/build.zig.zon:6:20: error: url field is missing corresponding hash field
            .url = "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz",
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "12206795542ab1c19251aac74db20dda4c2abf3289597dd02d52b1d70175064fb298",
bq-wrongway commented 1 year ago

How did you find this link : "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz", i just managed to find link from release tags, and they hold number of release after archive/ .

yozachar commented 1 year ago

How did you find this link : "https://github.com/Hejsil/zig-clap/archive/34e068756e69e7ee51ba6f27eb9036e19984a128.tar.gz", i just managed to find link from release tags, and they hold number of release after archive/ .

image

Yup that's right.

image

Or from here.

rawhuul commented 9 months ago

You can also now try out the zig package manager:

build.zig.zon

.{
    .name = "name-of-my-program-or-package",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/<git-hash-or-version>.tar.gz",
            .hash = "<the-hash-zig-tells-you-the-package-has>",
        },
    }
}

build.zig

const clap = b.dependecy("clap", .{});
exe.addModule("clap", clap.module("clap"));

Or, if you need multiple things to depend on clap but not use the package manager:

const clap = b.createModule(.{
    .source_file = .{ .path = "lib/zig-clap/clap.zig" },
});
exe.addModule("clap", clap);

image

swats on  main [!] via ↯ v0.12.0-dev.2063+804cee3b9 ❯ zig version
0.12.0-dev.2063+804cee3b9
swats on  main [!] via ↯ v0.12.0-dev.2063+804cee3b9 ❯

How to resolve this issue?

Hejsil commented 9 months ago

@rawhuul Version 0.7 of zig-clap targets 0.11 of zig. There is no guarantee that this version will work with newer version of zig. Seems you're very close to zig master, so you should get a master version of zig clap as well.

rawhuul commented 9 months ago

@rawhuul Version 0.7 of zig-clap targets 0.11 of zig. There is no guarantee that this version will work with newer version of zig. Seems you're very close to zig master, so you should get a master version of zig clap as well.

OKay I will try with master version

MathewDeyo commented 7 months ago

Any insight on using the Zig Package Manager?

$ zig version
0.12.0-dev.3212+40e64245f

build.zig.zon:

.{
    .name = "package-name-here",
    .version = "0.0.0",
    .dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/refs/heads/master.tar.gz",
            .hash = "12203896de6eedec14712f4f1eaac8b646939cfed213c56accf231a0abb05f9dbb77",
         },
    },
    .paths = .{
        "",
    },
}

build.zig:

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));
exe.linkLibrary(clap.artifact("clap"));

And when I try to zig build:

$ zig build
thread 39008 panic: unable to find artifact 'clap'
C:\Users\user\.zvm\master\lib\std\debug.zig:434:22: 0x9338f4 in panicExtra__anon_18765 (build.exe.obj)
    std.builtin.panic(msg, trace, ret_addr);
                     ^
C:\Users\user\.zvm\master\lib\std\debug.zig:409:15: 0x908bac in panic__anon_17759 (build.exe.obj)
    panicExtra(null, null, format, args);
              ^
C:\Users\user\.zvm\master\lib\std\Build.zig:1790:18: 0x8dab74 in artifact (build.exe.obj)
            panic("unable to find artifact '{s}'", .{name});
                 ^
C:\Users\user\workspace\build.zig:28:34: 0x88f656 in build (build.exe.obj)
    exe.linkLibrary(clap.artifact("clap"));
                                 ^
C:\Users\user\.zvm\master\lib\std\Build.zig:1992:33: 0x86dfa5 in runBuild__anon_8716 (build.exe.obj)
        .Void => build_zig.build(b),
                                ^
C:\Users\user\.zvm\master\lib\build_runner.zig:310:29: 0x868f5a in main (build.exe.obj)
        try builder.runBuild(root);
                            ^
C:\Users\user\.zvm\master\lib\std\start.zig:350:53: 0x86ffdc in WinStartup (build.exe.obj)
    std.os.windows.ntdll.RtlExitUserProcess(callMain());
                                                    ^
???:?:?: 0x7ff80d99257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ff80f0eaa57 in ??? (ntdll.dll)
error: unable to read results of configure phase from 'C:\Users\user\workspace\zig-cache\tmp\ba9a75b7430cf0ff': FileNotFound

If I remove exe.linkLibrary(clap.artifact("clap"));, the build works, but I can't run the application:

$ zig run src/main.zig -- -h
src\main.zig:2:22: error: no module named 'clap' available within module main
const clap = @import("clap");
                     ^~~~~~
referenced by:
    main: src\main.zig:13:29
    callMain: C:\Users\user\.zvm\master\lib\std\start.zig:511:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I've tried using Zig 0.11.0 with tag 0.7.0 as well (and converted the build.zig file to match) with basically the same results:

$ zig run src/main.zig -- -h
src\main.zig:2:22: error: no package named 'clap' available within package 'root'
const clap = @import("clap");
                     ^~~~~~
referenced by:
    main: src\main.zig:13:29
    callMain: C:\Users\user\.zvm\bin\lib\std\start.zig:574:32
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
Hejsil commented 7 months ago

@MathewDeyo

You have the right code in build.zig after you removed exe.linkLibrary(clap.artifact("clap"));. The problem you're hitting now is that zig run doesn't use the build system. Only zig build actually looks at build.zig. I don't think this issue is a place for a zig tutorial so please ask in any of the zig communities for help and advice :)

kothavade commented 5 months ago

In Zig 0.12 it seems like the way to do this is now (new to Zig, so this may be wrong, but it's working for me):

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));
janie314 commented 5 months ago

To summarize:

(1) Run this:

zig fetch --save git+https://github.com/Hejsil/zig-clap

(2) Then add @kothavade 's suggestion in build.zig:

const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));

Yeag this sould probably be improved.

Happy Zigging.

microspino commented 1 month ago

Hi I'm on OS X Ventura 13.6.9 and use zig 0.13.0. I did exactly as said here and in the readme but I'm unable to build a simple "hello world" project. When i do zig build I get this:

...
error: root struct of file 'Build.Step' has no member named 'MakeOptions'
            fn make(step: *std.Build.Step, _: std.Build.Step.MakeOptions) anyerror!void {

This is what i have in my build.zig.zon:

    .dependencies = .{
        .clap = .{
            .url = "git+https://github.com/Hejsil/zig-clap#de916df4982807d48ae497df0d75d6324b69a71a",
            .hash = "...long hash here...",
        },
    },

I'm fairly new to Zig, so I don't understand where that MakeOptions come from. ~Any clue?~

EDIT

Nevermind, i did this:

  1. change dependencies to use the 0.9.1 release:
.dependencies = .{
        .clap = .{
            .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
            ...
        },
    },
  1. commented my hash row below .url = "..."
  2. zig build which failed
  3. copied and pasted the expected .hash = ... that zig build gave me when failed

And it worked.

Hejsil commented 1 month ago

@microspino Seems the readme still needed a bit of work. While it did mention that the master branch of zig-clap targets the master branch of Zig, this wasn't under the installation section and therefor easily missed. I've rewritten the installation section to explain how to install zig-clap both for people who use a tagged release of Zig and Zig master.

Hopefully that should avoid the dance you did for others :)