add option to show help

This commit is contained in:
2025-03-08 19:31:05 +01:00
parent 21a4c231a3
commit b4f82ebfda

View File

@@ -88,13 +88,14 @@ fn printHelp() void {
\\ eval "$(zig-totp --bash)" \\ eval "$(zig-totp --bash)"
\\ to your ~/.bashrc \\ to your ~/.bashrc
\\ --config [path] The path to a config file (default is $XDG_CONFIG_HOME/zig-totp or $HOME/.zig-totp). \\ --config [path] The path to a config file (default is $XDG_CONFIG_HOME/zig-totp or $HOME/.zig-totp).
\\ --help Show this help
\\ --version Print the version number \\ --version Print the version number
\\ \\
; ;
std.debug.print("{s}", .{msg}); std.debug.print("{s}", .{msg});
} }
const Args = struct { list: bool, show: ?[]const u8, config_location: []const u8, validateAuthenticator: ?[]const u8, validateCode: ?[]const u8, print_version: bool }; const Args = struct { list: bool, show: ?[]const u8, config_location: []const u8, validateAuthenticator: ?[]const u8, validateCode: ?[]const u8, print_version: bool, show_help: bool };
const OtpAuthUrl = struct { name: []const u8, secretEncoded: []const u8, url: []const u8, period: u32, digits: u4, algorithm: []const u8 }; const OtpAuthUrl = struct { name: []const u8, secretEncoded: []const u8, url: []const u8, period: u32, digits: u4, algorithm: []const u8 };
@@ -190,7 +191,7 @@ fn zeroPad(allocator: Allocator, digits: u4, x: anytype) ![]u8 {
const ArgumentError = error{ InvalidOtpAuthUrl, UnknownParameter, MissingConfigLocation, AuthenticatorNotFound, MissingAuthenticatorParam, FailedToOpenConfigFile, TooManyParsinErrors }; const ArgumentError = error{ InvalidOtpAuthUrl, UnknownParameter, MissingConfigLocation, AuthenticatorNotFound, MissingAuthenticatorParam, FailedToOpenConfigFile, TooManyParsinErrors };
fn parseArgs(allocator: Allocator, args: []const []const u8) !Args { fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
var result = Args{ .list = false, .show = null, .config_location = try configLocation(allocator), .validateAuthenticator = null, .validateCode = null, .print_version = false }; var result = Args{ .list = false, .show = null, .config_location = try configLocation(allocator), .validateAuthenticator = null, .validateCode = null, .print_version = false, .show_help = false };
var i: u17 = 1; var i: u17 = 1;
while (i < args.len) : (i += 1) { while (i < args.len) : (i += 1) {
const arg = args[i]; const arg = args[i];
@@ -224,6 +225,8 @@ fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
return error.MissingConfigLocation; return error.MissingConfigLocation;
} }
result.config_location = args[i]; result.config_location = args[i];
} else if (eql(u8, "--help", arg) or eql(u8, "help", arg)) {
result.show_help = true;
} else if (eql(u8, "--version", arg)) { } else if (eql(u8, "--version", arg)) {
result.print_version = true; result.print_version = true;
} else if (eql(u8, "list", arg)) { } else if (eql(u8, "list", arg)) {