common: standardize on -help option

Also:

- Check for -help option *before* unknown options
- Use common flags parsing code for riverctl
- Add usage text to riverctl
This commit is contained in:
Isaac Freund
2021-07-24 19:31:04 +02:00
parent a5a505ecba
commit 23088b800c
5 changed files with 60 additions and 31 deletions

View File

@ -51,7 +51,7 @@ const flags = @import("flags");
const usage =
\\usage: rivertile [options]
\\
\\ -h, --help Print this help message and exit.
\\ -help Print this help message and exit.
\\ -version Print the version number and exit.
\\ -view-padding Set the padding around views in pixels. (Default 6)
\\ -outer-padding Set the padding around the edge of the layout area in
@ -314,8 +314,7 @@ pub fn main() !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const result = flags.parse(argv[1..], &[_]flags.Flag{
.{ .name = "-h", .kind = .boolean },
.{ .name = "--help", .kind = .boolean },
.{ .name = "-help", .kind = .boolean },
.{ .name = "-version", .kind = .boolean },
.{ .name = "-view-padding", .kind = .arg },
.{ .name = "-outer-padding", .kind = .arg },
@ -326,12 +325,12 @@ pub fn main() !void {
try std.io.getStdErr().writeAll(usage);
os.exit(1);
};
if (result.args.len != 0) fatalPrintUsage("unknown option '{s}'", .{result.args[0]});
if (result.boolFlag("-h") or result.boolFlag("--help")) {
if (result.boolFlag("-help")) {
try std.io.getStdOut().writeAll(usage);
os.exit(0);
}
if (result.args.len != 0) fatalPrintUsage("unknown option '{s}'", .{result.args[0]});
if (result.boolFlag("-version")) {
try std.io.getStdOut().writeAll(@import("build_options").version);
os.exit(0);