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
6 changed files with 47 additions and 47 deletions

View File

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

View File

@ -43,18 +43,18 @@ pub fn map(
out: *?[]const u8,
) Error!void {
const result = flags.parser([:0]const u8, &.{
.{ .name = "-release", .kind = .boolean },
.{ .name = "-repeat", .kind = .boolean },
.{ .name = "-layout", .kind = .arg },
.{ .name = "release", .kind = .boolean },
.{ .name = "repeat", .kind = .boolean },
.{ .name = "layout", .kind = .arg },
}).parse(args[1..]) catch {
return error.InvalidValue;
};
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: {
if (result.flags.@"-layout") |layout_raw| {
if (result.flags.layout) |layout_raw| {
break :blk try fmt.parseInt(u32, layout_raw, 10);
} else {
break :blk null;
@ -77,18 +77,18 @@ pub fn map(
modifiers,
command,
.{
.release = result.flags.@"-release",
.repeat = result.flags.@"-repeat",
.release = result.flags.release,
.repeat = result.flags.repeat,
.layout_index = layout_index,
},
);
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] = new;
// 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(
util.gpa,
"overwrote an existing keybinding: {s} {s}{s} {s}",
@ -345,7 +345,7 @@ fn parseSwitchState(
/// unmap normal Mod4+Shift Return
pub fn unmap(seat: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!void {
const result = flags.parser([:0]const u8, &.{
.{ .name = "-release", .kind = .boolean },
.{ .name = "release", .kind = .boolean },
}).parse(args[1..]) catch {
return error.InvalidValue;
};
@ -361,7 +361,7 @@ pub fn unmap(seat: *Seat, args: []const [:0]const u8, out: *?[]const u8) Error!v
mode_mappings,
modifiers,
keysym,
result.flags.@"-release",
result.flags.release,
) orelse return;
// 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
const argv: [][*:0]const u8 = os.argv;
const result = flags.parser([*:0]const u8, &.{
.{ .name = "-h", .kind = .boolean },
.{ .name = "-version", .kind = .boolean },
.{ .name = "-c", .kind = .arg },
.{ .name = "-log-level", .kind = .arg },
.{ .name = "h", .kind = .boolean },
.{ .name = "version", .kind = .boolean },
.{ .name = "c", .kind = .arg },
.{ .name = "log-level", .kind = .arg },
}).parse(argv[1..]) catch {
try io.getStdErr().writeAll(usage);
os.exit(1);
};
if (result.flags.@"-h") {
if (result.flags.h) {
try io.getStdOut().writeAll(usage);
os.exit(0);
}
@ -63,11 +63,11 @@ pub fn main() anyerror!void {
os.exit(1);
}
if (result.flags.@"-version") {
if (result.flags.version) {
try io.getStdOut().writeAll(build_options.version ++ "\n");
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())) {
runtime_log_level = .err;
} else if (mem.eql(u8, level, std.log.Level.warn.asText())) {
@ -83,7 +83,7 @@ pub fn main() anyerror!void {
}
}
const startup_command = blk: {
if (result.flags.@"-c") |command| {
if (result.flags.c) |command| {
break :blk try util.gpa.dupeZ(u8, command);
} else {
break :blk try defaultInitPath();