flags: automatically prepend '-'

This makes the usage a bit cleaner as the results of the parsing may be
accessed with e.g. ret.flags.version instead of ret.flags.@"-version".
This commit is contained in:
Isaac Freund 2022-12-28 22:11:14 +01:00
parent 2be9ac05d6
commit e18d0d5e1c
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
6 changed files with 47 additions and 47 deletions

View File

@ -72,13 +72,13 @@ pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
while (i < args.len) : (i += 1) { while (i < args.len) : (i += 1) {
var parsed_flag = false; var parsed_flag = false;
inline for (flags) |flag| { inline for (flags) |flag| {
if (mem.eql(u8, flag.name, mem.span(args[i]))) { if (mem.eql(u8, "-" ++ flag.name, mem.span(args[i]))) {
switch (flag.kind) { switch (flag.kind) {
.boolean => @field(result_flags, flag.name) = true, .boolean => @field(result_flags, flag.name) = true,
.arg => { .arg => {
i += 1; i += 1;
if (i == args.len) { if (i == args.len) {
std.log.err("option '" ++ flag.name ++ std.log.err("option '-" ++ flag.name ++
"' requires an argument but none was provided!", .{}); "' requires an argument but none was provided!", .{});
return error.MissingFlagArgument; return error.MissingFlagArgument;
} }

View File

@ -32,10 +32,10 @@ pub fn keyboardLayout(
_: *?[]const u8, _: *?[]const u8,
) Error!void { ) Error!void {
const result = flags.parser([:0]const u8, &.{ const result = flags.parser([:0]const u8, &.{
.{ .name = "-rules", .kind = .arg }, .{ .name = "rules", .kind = .arg },
.{ .name = "-model", .kind = .arg }, .{ .name = "model", .kind = .arg },
.{ .name = "-variant", .kind = .arg }, .{ .name = "variant", .kind = .arg },
.{ .name = "-options", .kind = .arg }, .{ .name = "options", .kind = .arg },
}).parse(args[1..]) catch { }).parse(args[1..]) catch {
return error.InvalidValue; return error.InvalidValue;
}; };
@ -44,10 +44,10 @@ pub fn keyboardLayout(
const new_layout = xkb.RuleNames{ const new_layout = xkb.RuleNames{
.layout = try util.gpa.dupeZ(u8, result.args[0]), .layout = try util.gpa.dupeZ(u8, result.args[0]),
.rules = if (result.flags.@"-rules") |s| try util.gpa.dupeZ(u8, s) else null, .rules = if (result.flags.rules) |s| try util.gpa.dupeZ(u8, s) else null,
.model = if (result.flags.@"-model") |s| try util.gpa.dupeZ(u8, s) else null, .model = if (result.flags.model) |s| try util.gpa.dupeZ(u8, s) else null,
.variant = if (result.flags.@"-variant") |s| try util.gpa.dupeZ(u8, s) else null, .variant = if (result.flags.variant) |s| try util.gpa.dupeZ(u8, s) else null,
.options = if (result.flags.@"-options") |s| try util.gpa.dupeZ(u8, s) else null, .options = if (result.flags.options) |s| try util.gpa.dupeZ(u8, s) else null,
}; };
errdefer util.free_xkb_rule_names(new_layout); errdefer util.free_xkb_rule_names(new_layout);

View File

@ -43,18 +43,18 @@ pub fn map(
out: *?[]const u8, out: *?[]const u8,
) Error!void { ) Error!void {
const result = flags.parser([:0]const u8, &.{ const result = flags.parser([:0]const u8, &.{
.{ .name = "-release", .kind = .boolean }, .{ .name = "release", .kind = .boolean },
.{ .name = "-repeat", .kind = .boolean }, .{ .name = "repeat", .kind = .boolean },
.{ .name = "-layout", .kind = .arg }, .{ .name = "layout", .kind = .arg },
}).parse(args[1..]) catch { }).parse(args[1..]) catch {
return error.InvalidValue; return error.InvalidValue;
}; };
if (result.args.len < 4) return Error.NotEnoughArguments; if (result.args.len < 4) return Error.NotEnoughArguments;
if (result.flags.@"-release" and result.flags.@"-repeat") return Error.ConflictingOptions; if (result.flags.release and result.flags.repeat) return Error.ConflictingOptions;
const layout_index = blk: { const layout_index = blk: {
if (result.flags.@"-layout") |layout_raw| { if (result.flags.layout) |layout_raw| {
break :blk try fmt.parseInt(u32, layout_raw, 10); break :blk try fmt.parseInt(u32, layout_raw, 10);
} else { } else {
break :blk null; break :blk null;
@ -77,18 +77,18 @@ pub fn map(
modifiers, modifiers,
command, command,
.{ .{
.release = result.flags.@"-release", .release = result.flags.release,
.repeat = result.flags.@"-repeat", .repeat = result.flags.repeat,
.layout_index = layout_index, .layout_index = layout_index,
}, },
); );
errdefer new.deinit(); errdefer new.deinit();
if (mappingExists(mode_mappings, modifiers, keysym, result.flags.@"-release")) |current| { if (mappingExists(mode_mappings, modifiers, keysym, result.flags.release)) |current| {
mode_mappings.items[current].deinit(); mode_mappings.items[current].deinit();
mode_mappings.items[current] = new; mode_mappings.items[current] = new;
// Warn user if they overwrote an existing keybinding using riverctl. // Warn user if they overwrote an existing keybinding using riverctl.
const opts = if (result.flags.@"-release") "-release " else ""; const opts = if (result.flags.release) "-release " else "";
out.* = try fmt.allocPrint( out.* = try fmt.allocPrint(
util.gpa, util.gpa,
"overwrote an existing keybinding: {s} {s}{s} {s}", "overwrote an existing keybinding: {s} {s}{s} {s}",
@ -345,7 +345,7 @@ fn parseSwitchState(
/// unmap normal Mod4+Shift Return /// unmap normal Mod4+Shift Return
pub fn unmap(seat: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!void { pub fn unmap(seat: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!void {
const result = flags.parser([:0]const u8, &.{ const result = flags.parser([:0]const u8, &.{
.{ .name = "-release", .kind = .boolean }, .{ .name = "release", .kind = .boolean },
}).parse(args[1..]) catch { }).parse(args[1..]) catch {
return error.InvalidValue; return error.InvalidValue;
}; };
@ -361,7 +361,7 @@ pub fn unmap(seat: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!v
mode_mappings, mode_mappings,
modifiers, modifiers,
keysym, keysym,
result.flags.@"-release", result.flags.release,
) orelse return; ) orelse return;
// Repeating mappings borrow the Mapping directly. To prevent a possible // Repeating mappings borrow the Mapping directly. To prevent a possible

View File

@ -45,15 +45,15 @@ pub fn main() anyerror!void {
// This line is here because of https://github.com/ziglang/zig/issues/7807 // This line is here because of https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv; const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &.{ const result = flags.parser([*:0]const u8, &.{
.{ .name = "-h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "-version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
.{ .name = "-c", .kind = .arg }, .{ .name = "c", .kind = .arg },
.{ .name = "-log-level", .kind = .arg }, .{ .name = "log-level", .kind = .arg },
}).parse(argv[1..]) catch { }).parse(argv[1..]) catch {
try io.getStdErr().writeAll(usage); try io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };
if (result.flags.@"-h") { if (result.flags.h) {
try io.getStdOut().writeAll(usage); try io.getStdOut().writeAll(usage);
os.exit(0); os.exit(0);
} }
@ -63,11 +63,11 @@ pub fn main() anyerror!void {
os.exit(1); os.exit(1);
} }
if (result.flags.@"-version") { if (result.flags.version) {
try io.getStdOut().writeAll(build_options.version ++ "\n"); try io.getStdOut().writeAll(build_options.version ++ "\n");
os.exit(0); os.exit(0);
} }
if (result.flags.@"-log-level") |level| { if (result.flags.@"log-level") |level| {
if (mem.eql(u8, level, std.log.Level.err.asText())) { if (mem.eql(u8, level, std.log.Level.err.asText())) {
runtime_log_level = .err; runtime_log_level = .err;
} else if (mem.eql(u8, level, std.log.Level.warn.asText())) { } else if (mem.eql(u8, level, std.log.Level.warn.asText())) {
@ -83,7 +83,7 @@ pub fn main() anyerror!void {
} }
} }
const startup_command = blk: { const startup_command = blk: {
if (result.flags.@"-c") |command| { if (result.flags.c) |command| {
break :blk try util.gpa.dupeZ(u8, command); break :blk try util.gpa.dupeZ(u8, command);
} else { } else {
break :blk try defaultInitPath(); break :blk try defaultInitPath();

View File

@ -66,17 +66,17 @@ fn _main() !void {
// This line is here because of https://github.com/ziglang/zig/issues/7807 // This line is here because of https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv; const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &.{ const result = flags.parser([*:0]const u8, &.{
.{ .name = "-h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "-version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
}).parse(argv[1..]) catch { }).parse(argv[1..]) catch {
try io.getStdErr().writeAll(usage); try io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };
if (result.flags.@"-h") { if (result.flags.h) {
try io.getStdOut().writeAll(usage); try io.getStdOut().writeAll(usage);
os.exit(0); os.exit(0);
} }
if (result.flags.@"-version") { if (result.flags.version) {
try io.getStdOut().writeAll(@import("build_options").version ++ "\n"); try io.getStdOut().writeAll(@import("build_options").version ++ "\n");
os.exit(0); os.exit(0);
} }

View File

@ -307,44 +307,44 @@ pub fn main() !void {
// https://github.com/ziglang/zig/issues/7807 // https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv; const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &[_]flags.Flag{ const result = flags.parser([*:0]const u8, &[_]flags.Flag{
.{ .name = "-h", .kind = .boolean }, .{ .name = "h", .kind = .boolean },
.{ .name = "-version", .kind = .boolean }, .{ .name = "version", .kind = .boolean },
.{ .name = "-view-padding", .kind = .arg }, .{ .name = "view-padding", .kind = .arg },
.{ .name = "-outer-padding", .kind = .arg }, .{ .name = "outer-padding", .kind = .arg },
.{ .name = "-main-location", .kind = .arg }, .{ .name = "main-location", .kind = .arg },
.{ .name = "-main-count", .kind = .arg }, .{ .name = "main-count", .kind = .arg },
.{ .name = "-main-ratio", .kind = .arg }, .{ .name = "main-ratio", .kind = .arg },
}).parse(argv[1..]) catch { }).parse(argv[1..]) catch {
try std.io.getStdErr().writeAll(usage); try std.io.getStdErr().writeAll(usage);
os.exit(1); os.exit(1);
}; };
if (result.flags.@"-h") { if (result.flags.h) {
try std.io.getStdOut().writeAll(usage); try std.io.getStdOut().writeAll(usage);
os.exit(0); os.exit(0);
} }
if (result.args.len != 0) fatalPrintUsage("unknown option '{s}'", .{result.args[0]}); if (result.args.len != 0) fatalPrintUsage("unknown option '{s}'", .{result.args[0]});
if (result.flags.@"-version") { if (result.flags.version) {
try std.io.getStdOut().writeAll(@import("build_options").version ++ "\n"); try std.io.getStdOut().writeAll(@import("build_options").version ++ "\n");
os.exit(0); os.exit(0);
} }
if (result.flags.@"-view-padding") |raw| { if (result.flags.@"view-padding") |raw| {
view_padding = fmt.parseUnsigned(u31, raw, 10) catch view_padding = fmt.parseUnsigned(u31, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -view-padding", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -view-padding", .{raw});
} }
if (result.flags.@"-outer-padding") |raw| { if (result.flags.@"outer-padding") |raw| {
outer_padding = fmt.parseUnsigned(u31, raw, 10) catch outer_padding = fmt.parseUnsigned(u31, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -outer-padding", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -outer-padding", .{raw});
} }
if (result.flags.@"-main-location") |raw| { if (result.flags.@"main-location") |raw| {
default_main_location = std.meta.stringToEnum(Location, raw) orelse default_main_location = std.meta.stringToEnum(Location, raw) orelse
fatalPrintUsage("invalid value '{s}' provided to -main-location", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-location", .{raw});
} }
if (result.flags.@"-main-count") |raw| { if (result.flags.@"main-count") |raw| {
default_main_count = fmt.parseUnsigned(u31, raw, 10) catch default_main_count = fmt.parseUnsigned(u31, raw, 10) catch
fatalPrintUsage("invalid value '{s}' provided to -main-count", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-count", .{raw});
} }
if (result.flags.@"-main-ratio") |raw| { if (result.flags.@"main-ratio") |raw| {
default_main_ratio = fmt.parseFloat(f64, raw) catch { default_main_ratio = fmt.parseFloat(f64, raw) catch {
fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw}); fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw});
}; };