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:
parent
2be9ac05d6
commit
e18d0d5e1c
@ -72,13 +72,13 @@ pub fn parser(comptime Arg: type, comptime flags: []const Flag) type {
|
||||
while (i < args.len) : (i += 1) {
|
||||
var parsed_flag = false;
|
||||
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) {
|
||||
.boolean => @field(result_flags, flag.name) = true,
|
||||
.arg => {
|
||||
i += 1;
|
||||
if (i == args.len) {
|
||||
std.log.err("option '" ++ flag.name ++
|
||||
std.log.err("option '-" ++ flag.name ++
|
||||
"' requires an argument but none was provided!", .{});
|
||||
return error.MissingFlagArgument;
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
@ -66,17 +66,17 @@ fn _main() !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 = "h", .kind = .boolean },
|
||||
.{ .name = "version", .kind = .boolean },
|
||||
}).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);
|
||||
}
|
||||
if (result.flags.@"-version") {
|
||||
if (result.flags.version) {
|
||||
try io.getStdOut().writeAll(@import("build_options").version ++ "\n");
|
||||
os.exit(0);
|
||||
}
|
||||
|
@ -307,44 +307,44 @@ pub fn main() !void {
|
||||
// https://github.com/ziglang/zig/issues/7807
|
||||
const argv: [][*:0]const u8 = os.argv;
|
||||
const result = flags.parser([*:0]const u8, &[_]flags.Flag{
|
||||
.{ .name = "-h", .kind = .boolean },
|
||||
.{ .name = "-version", .kind = .boolean },
|
||||
.{ .name = "-view-padding", .kind = .arg },
|
||||
.{ .name = "-outer-padding", .kind = .arg },
|
||||
.{ .name = "-main-location", .kind = .arg },
|
||||
.{ .name = "-main-count", .kind = .arg },
|
||||
.{ .name = "-main-ratio", .kind = .arg },
|
||||
.{ .name = "h", .kind = .boolean },
|
||||
.{ .name = "version", .kind = .boolean },
|
||||
.{ .name = "view-padding", .kind = .arg },
|
||||
.{ .name = "outer-padding", .kind = .arg },
|
||||
.{ .name = "main-location", .kind = .arg },
|
||||
.{ .name = "main-count", .kind = .arg },
|
||||
.{ .name = "main-ratio", .kind = .arg },
|
||||
}).parse(argv[1..]) catch {
|
||||
try std.io.getStdErr().writeAll(usage);
|
||||
os.exit(1);
|
||||
};
|
||||
if (result.flags.@"-h") {
|
||||
if (result.flags.h) {
|
||||
try std.io.getStdOut().writeAll(usage);
|
||||
os.exit(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");
|
||||
os.exit(0);
|
||||
}
|
||||
if (result.flags.@"-view-padding") |raw| {
|
||||
if (result.flags.@"view-padding") |raw| {
|
||||
view_padding = fmt.parseUnsigned(u31, raw, 10) catch
|
||||
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
|
||||
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
|
||||
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
|
||||
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 {
|
||||
fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user