From 88a5bfb42b352a79053a807b05367b784b81cfbd Mon Sep 17 00:00:00 2001 From: Ole-Morten Duesund Date: Sat, 2 Aug 2025 19:13:15 +0200 Subject: [PATCH] feat: add bash completion generation for command line interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add --generate-bash-completion flag to output bash completion script - Provide intelligent completions for config files (*.json) and message counts - Support tab completion for all available command line options - Generate professional script with proper comments and error handling 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- go/config/config.go | 59 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/go/config/config.go b/go/config/config.go index 8d2527d..ddb3689 100644 --- a/go/config/config.go +++ b/go/config/config.go @@ -95,10 +95,11 @@ func ParseCommandLine() *CommandLineArgs { // Define long options with -- and short options with - pflag.StringVarP(&args.ConfigPath, "config", "c", "", "Path to configuration file") pflag.IntVarP(&args.MaxMessages, "max-messages", "m", 0, "Maximum number of messages to process per mailbox per run (0 = no limit)") - - // Add help option + + // Add utility options pflag.BoolP("help", "h", false, "Show help message") - + pflag.Bool("generate-bash-completion", false, "Generate bash completion script and exit") + pflag.Parse() // Handle help flag @@ -110,9 +111,61 @@ func ParseCommandLine() *CommandLineArgs { os.Exit(0) } + // Handle bash completion generation + if generateCompletion, _ := pflag.CommandLine.GetBool("generate-bash-completion"); generateCompletion { + GenerateBashCompletion() + os.Exit(0) + } + return &args } +// GenerateBashCompletion generates a bash completion script for mail2couch +func GenerateBashCompletion() { + appName := filepath.Base(os.Args[0]) + script := fmt.Sprintf(`#!/bin/bash +# Bash completion script for %s +# Generated automatically by %s --generate-bash-completion + +_%s_completions() { + local cur prev words cword + _init_completion || return + + case $prev in + -c|--config) + # Complete config files (*.json) + _filedir "json" + return + ;; + -m|--max-messages) + # Complete with numbers, suggest common values + COMPREPLY=($(compgen -W "10 50 100 500 1000" -- "$cur")) + return + ;; + esac + + if [[ $cur == -* ]]; then + # Complete with available options + local opts="-c --config -m --max-messages -h --help --generate-bash-completion" + COMPREPLY=($(compgen -W "$opts" -- "$cur")) + return + fi + + # No default completion for other cases +} + +# Register the completion function +complete -F _%s_completions %s + +# Enable completion for common variations of the command name +if [[ "$(%s --help 2>/dev/null)" =~ "mail2couch" ]]; then + complete -F _%s_completions mail2couch +fi +`, appName, appName, appName, appName, appName, appName, appName) + + fmt.Print(script) +} + // FindConfigFile searches for config.json in the following order: // 1. Path specified by --config/-c flag // 2. ./config.json (current directory)