feat: add bash completion generation for command line interface

- 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 <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2025-08-02 19:13:15 +02:00
commit 88a5bfb42b

View file

@ -96,8 +96,9 @@ func ParseCommandLine() *CommandLineArgs {
pflag.StringVarP(&args.ConfigPath, "config", "c", "", "Path to configuration file") 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)") 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.BoolP("help", "h", false, "Show help message")
pflag.Bool("generate-bash-completion", false, "Generate bash completion script and exit")
pflag.Parse() pflag.Parse()
@ -110,9 +111,61 @@ func ParseCommandLine() *CommandLineArgs {
os.Exit(0) os.Exit(0)
} }
// Handle bash completion generation
if generateCompletion, _ := pflag.CommandLine.GetBool("generate-bash-completion"); generateCompletion {
GenerateBashCompletion()
os.Exit(0)
}
return &args 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: // FindConfigFile searches for config.json in the following order:
// 1. Path specified by --config/-c flag // 1. Path specified by --config/-c flag
// 2. ./config.json (current directory) // 2. ./config.json (current directory)