add bash completion script
This commit is contained in:
24
src/main.zig
24
src/main.zig
@@ -73,6 +73,9 @@ fn printHelp() void {
|
|||||||
\\ show NAME Show the code for the authenticator with name NAME
|
\\ show NAME Show the code for the authenticator with name NAME
|
||||||
\\
|
\\
|
||||||
\\Options:
|
\\Options:
|
||||||
|
\\ --bash Print script to set up Bash shell integration. Usage: add
|
||||||
|
\\ eval "$(zig-totp --bash)"
|
||||||
|
\\ 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).
|
||||||
\\
|
\\
|
||||||
;
|
;
|
||||||
@@ -182,7 +185,26 @@ fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
|
|||||||
while (i < args.len) : (i += 1) {
|
while (i < args.len) : (i += 1) {
|
||||||
const arg = args[i];
|
const arg = args[i];
|
||||||
|
|
||||||
if (eql(u8, "--config", arg)) {
|
if (eql(u8, "--bash", arg)) {
|
||||||
|
const msg =
|
||||||
|
\\_zig_totp_completions()
|
||||||
|
\\{
|
||||||
|
\\ if [ "${COMP_WORDS[$COMP_CWORD-1]}" = 'show' ]
|
||||||
|
\\ then
|
||||||
|
\\ COMPREPLY=($(compgen -W "$(zig-totp list 2> /dev/null )" -- "${COMP_WORDS[$COMP_CWORD]}" ))
|
||||||
|
\\ elif [ "${COMP_WORDS[$COMP_CWORD-1]}" = '--config' ]
|
||||||
|
\\ then
|
||||||
|
\\ COMPREPLY=($(compgen -A file -- "${COMP_WORDS[$COMP_CWORD]}" ))
|
||||||
|
\\ else
|
||||||
|
\\ COMPREPLY=($(compgen -W "list show --bash --config" -- "${COMP_WORDS[$COMP_CWORD]}" ))
|
||||||
|
\\ fi
|
||||||
|
\\}
|
||||||
|
\\complete -F _zig_totp_completions zig-totp
|
||||||
|
\\
|
||||||
|
;
|
||||||
|
try std.io.getStdOut().writer().print("{s}\n", .{msg});
|
||||||
|
std.process.exit(0);
|
||||||
|
} else if (eql(u8, "--config", arg)) {
|
||||||
i += 1;
|
i += 1;
|
||||||
if (i >= args.len) {
|
if (i >= args.len) {
|
||||||
std.debug.print("expected a path after parameter '--config'\n see 'zig-totp' for help\n", .{});
|
std.debug.print("expected a path after parameter '--config'\n see 'zig-totp' for help\n", .{});
|
||||||
|
|||||||
Reference in New Issue
Block a user