84 lines
2.1 KiB
Markdown
84 lines
2.1 KiB
Markdown
|
+++
|
||
|
date = "2022-08-03"
|
||
|
tags = ["zig"]
|
||
|
title = "zigでcliを作る"
|
||
|
slug = "zig-cli"
|
||
|
+++
|
||
|
|
||
|
zigは、現時点で外部パッケージを読み込む機能がありません。rustならCargo.toml、rubyならGemfile、goならgo.modがあります。
|
||
|
|
||
|
zigには、いくつか有志が仕組みを作っていますが、決定的なものは存在しません。
|
||
|
|
||
|
- https://github.com/mattnite/gyro
|
||
|
|
||
|
- https://github.com/marler8997/zig-build-repos
|
||
|
|
||
|
したがって、build.zigにpathを書いていく必要があります。今回は、cli-toolを作ってみます。
|
||
|
|
||
|
```zig:build.zig
|
||
|
const std = @import("std");
|
||
|
|
||
|
pub fn build(b: *std.build.Builder) void {
|
||
|
const mode = b.standardReleaseOptions();
|
||
|
|
||
|
const lib = b.addStaticLibrary("zig-cli", "zig-cli/src/main.zig");
|
||
|
lib.setBuildMode(mode);
|
||
|
lib.install();
|
||
|
|
||
|
const main_tests = b.addTest("zig-cli/src/tests.zig");
|
||
|
main_tests.setBuildMode(mode);
|
||
|
|
||
|
const test_step = b.step("test", "Run library tests");
|
||
|
test_step.dependOn(&main_tests.step);
|
||
|
|
||
|
const origin = b.addExecutable("random", "example/random.zig");
|
||
|
origin.addPackagePath("zig-cli", "zig-cli/src/main.zig");
|
||
|
origin.setBuildMode(mode);
|
||
|
origin.install();
|
||
|
|
||
|
b.default_step.dependOn(&origin.step);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
https://github.com/sam701/zig-cli
|
||
|
|
||
|
```sh
|
||
|
$ git clone https://github.com/sam701/zig-cli
|
||
|
$ mkdir -p example
|
||
|
$ vim example/random.zig
|
||
|
```
|
||
|
|
||
|
```zig:example/random.zig
|
||
|
const std = @import("std");
|
||
|
const cli = @import("zig-cli");
|
||
|
const RndGen = std.rand.DefaultPrng;
|
||
|
|
||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||
|
const allocator = gpa.allocator();
|
||
|
|
||
|
var app = &cli.Command{
|
||
|
.name = "random",
|
||
|
.help = "get random number",
|
||
|
.action = run_server,
|
||
|
};
|
||
|
|
||
|
pub fn main() !void {
|
||
|
return cli.run(app, allocator);
|
||
|
}
|
||
|
|
||
|
fn run_server(_: []const []const u8) !void {
|
||
|
var rnd = RndGen.init(0);
|
||
|
var some_random_num = rnd.random().int(i32);
|
||
|
std.log.debug("{d}", .{some_random_num});
|
||
|
}
|
||
|
```
|
||
|
|
||
|
```sh
|
||
|
$ zig build
|
||
|
$ ./zig-out/bin/random
|
||
|
```
|
||
|
|
||
|
ref : https://www.reddit.com/r/Zig/comments/wc5rcb/hows_the_current_story_with_zig_in_terms_of/
|
||
|
|
||
|
ref : https://www.reddit.com/r/Zig/comments/wcvksf/what_is_missing_in_the_zig_ecosystem/
|