Skip to content

Commit e472922

Browse files
feat: add --version and --help CLI flags, bump to v0.2.0
1 parent 5db2e2c commit e472922

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed

build.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const std = @import("std");
22

3+
const version = "0.2.0";
4+
35
// Although this function looks imperative, it does not perform the build
46
// directly and instead it mutates the build graph (`b`) that will be then
57
// executed by an external runner. The functions in `std.Build` implement a DSL
@@ -77,6 +79,10 @@ pub fn build(b: *std.Build) void {
7779
//
7880
// If neither case applies to you, feel free to delete the declaration you
7981
// don't need and to put everything under a single module.
82+
// Create build options module to pass version to the executable
83+
const build_options = b.addOptions();
84+
build_options.addOption([]const u8, "version", version);
85+
8086
const exe = b.addExecutable(.{
8187
.name = "ansilust",
8288
.root_module = b.createModule(.{
@@ -100,6 +106,7 @@ pub fn build(b: *std.Build) void {
100106
// importing modules from different packages).
101107
.{ .name = "ansilust", .module = mod },
102108
.{ .name = "parsers", .module = parsers_mod },
109+
.{ .name = "build_options", .module = build_options.createModule() },
103110
},
104111
}),
105112
});

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
.name = .ansilust,
1010
// This is a [Semantic Version](https://semver.org/).
1111
// In a future version of Zig it will be used for package deduplication.
12-
.version = "0.0.0",
12+
.version = "0.2.0",
1313
// Together with name, this represents a globally unique package
1414
// identifier. This field is generated by the Zig toolchain when the
1515
// package is first created, and then *never changes*. This allows

packages/ansilust/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ansilust",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Next-generation text art processing system - convert, render, and animate ANSI art",
55
"main": "index.js",
66
"bin": {
@@ -26,16 +26,16 @@
2626
"detect-libc": "^2.1.2"
2727
},
2828
"optionalDependencies": {
29-
"ansilust-darwin-arm64": "0.0.1",
30-
"ansilust-darwin-x64": "0.0.1",
31-
"ansilust-linux-aarch64-gnu": "0.0.1",
32-
"ansilust-linux-aarch64-musl": "0.0.1",
33-
"ansilust-linux-arm-gnu": "0.0.1",
34-
"ansilust-linux-arm-musl": "0.0.1",
35-
"ansilust-linux-i386-musl": "0.0.1",
36-
"ansilust-linux-x64-gnu": "0.0.1",
37-
"ansilust-linux-x64-musl": "0.0.1",
38-
"ansilust-win32-x64": "0.0.1"
29+
"ansilust-darwin-arm64": "0.2.0",
30+
"ansilust-darwin-x64": "0.2.0",
31+
"ansilust-linux-aarch64-gnu": "0.2.0",
32+
"ansilust-linux-aarch64-musl": "0.2.0",
33+
"ansilust-linux-arm-gnu": "0.2.0",
34+
"ansilust-linux-arm-musl": "0.2.0",
35+
"ansilust-linux-i386-musl": "0.2.0",
36+
"ansilust-linux-x64-gnu": "0.2.0",
37+
"ansilust-linux-x64-musl": "0.2.0",
38+
"ansilust-win32-x64": "0.2.0"
3939
},
4040
"engines": {
4141
"node": ">=14.0.0"

src/main.zig

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
const std = @import("std");
22
const ansilust = @import("ansilust");
3+
const build_options = @import("build_options");
4+
5+
const version = build_options.version;
6+
7+
fn printVersion() void {
8+
const stdout_file = std.fs.File{ .handle = std.posix.STDOUT_FILENO };
9+
stdout_file.writeAll("ansilust " ++ version ++ "\n") catch {};
10+
}
11+
12+
fn printHelp(file: std.fs.File) void {
13+
file.writeAll(
14+
\\ansilust - Next-generation text art processing system
15+
\\
16+
\\USAGE:
17+
\\ ansilust [OPTIONS] <file.ans> [<file2.ans> ...]
18+
\\
19+
\\OPTIONS:
20+
\\ -h, --help Print this help message
21+
\\ -V, --version Print version information
22+
\\
23+
\\EXAMPLES:
24+
\\ ansilust artwork.ans Render ANSI art to terminal
25+
\\ ansilust file1.ans file2.ans Render multiple files
26+
\\
27+
) catch {};
28+
}
329

430
fn processFile(allocator: std.mem.Allocator, path: []const u8) !void {
531
const file_data = std.fs.cwd().readFileAlloc(allocator, path, 100 * 1024 * 1024) catch |e| {
@@ -34,14 +60,40 @@ pub fn main() !void {
3460

3561
_ = args.next(); // skip argv0
3662

63+
const stdout_file = std.fs.File{ .handle = std.posix.STDOUT_FILENO };
64+
const stderr_file = std.fs.File{ .handle = std.posix.STDERR_FILENO };
65+
3766
var file_count: usize = 0;
38-
while (args.next()) |path| {
39-
try processFile(allocator, path);
40-
file_count += 1;
67+
var show_help = false;
68+
var show_version = false;
69+
70+
while (args.next()) |arg| {
71+
if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
72+
show_help = true;
73+
} else if (std.mem.eql(u8, arg, "-V") or std.mem.eql(u8, arg, "--version")) {
74+
show_version = true;
75+
} else if (std.mem.startsWith(u8, arg, "-")) {
76+
std.debug.print("error: unknown option '{s}'\n", .{arg});
77+
stderr_file.writeAll("Try 'ansilust --help' for more information.\n") catch {};
78+
std.process.exit(1);
79+
} else {
80+
try processFile(allocator, arg);
81+
file_count += 1;
82+
}
4183
}
4284

43-
if (file_count == 0) {
44-
std.debug.print("usage: ansilust <file.ans> [<file2.ans> ...]\n", .{});
85+
if (show_version) {
86+
printVersion();
87+
return;
88+
}
89+
90+
if (show_help) {
91+
printHelp(stdout_file);
4592
return;
4693
}
94+
95+
if (file_count == 0) {
96+
printHelp(stderr_file);
97+
std.process.exit(1);
98+
}
4799
}

0 commit comments

Comments
 (0)