arg parsing
This commit is contained in:
parent
e7139298ba
commit
b5e1b15f8c
2 changed files with 62 additions and 1 deletions
57
src/main.zig
57
src/main.zig
|
@ -1,5 +1,60 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() !void {
|
const util = @import("util.zig");
|
||||||
|
|
||||||
|
const InitError = error {
|
||||||
|
NotEnoughArgs,
|
||||||
|
InvalidDimension,
|
||||||
|
InvalidQuality,
|
||||||
|
InvalidNumJobs,
|
||||||
|
};
|
||||||
|
|
||||||
|
inline fn next_arg(args: *std.process.ArgIterator) ![:0]const u8 {
|
||||||
|
return args.next() orelse InitError.NotEnoughArgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fn get_dim(arg: [:0]const u8) !usize {
|
||||||
|
const d = try std.fmt.parseInt(usize, arg, 10);
|
||||||
|
if (d % 16 != 0 or d == 0) {
|
||||||
|
return InitError.InvalidDimension;
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fn get_qual(arg: [:0]const u8) !f16 {
|
||||||
|
const q = try std.fmt.parseFloat(f16, arg);
|
||||||
|
if (q < 0 or q > 1) {
|
||||||
|
return InitError.InvalidQuality;
|
||||||
|
}
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fn get_n_jobs(arg: [:0]const u8) !usize {
|
||||||
|
const n = try std.fmt.parseInt(usize, arg, 10);
|
||||||
|
if (n == 0) {
|
||||||
|
return InitError.InvalidNumJobs;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_opts(alloc: std.mem.Allocator) !util.Options {
|
||||||
|
var args = try std.process.argsWithAllocator(alloc);
|
||||||
|
defer args.deinit();
|
||||||
|
_ = args.next();
|
||||||
|
|
||||||
|
return util.Options {
|
||||||
|
.width = try get_dim(try next_arg(&args)),
|
||||||
|
.height = try get_dim(try next_arg(&args)),
|
||||||
|
.quality = try get_qual(try next_arg(&args)),
|
||||||
|
.n_quant_jobs = try get_n_jobs(try next_arg(&args)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
var arg_buff: [100]u8 = undefined;
|
||||||
|
var arg_fba = std.heap.FixedBufferAllocator.init(&arg_buff);
|
||||||
|
const alloc = arg_fba.allocator();
|
||||||
|
|
||||||
|
const opts = try get_opts(alloc);
|
||||||
|
std.debug.print("{any}\n", .{opts});
|
||||||
}
|
}
|
||||||
|
|
6
src/util.zig
Normal file
6
src/util.zig
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
pub const Options = struct {
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
|
quality: f16,
|
||||||
|
n_quant_jobs: usize,
|
||||||
|
};
|
Loading…
Reference in a new issue