From 335b2665f237fc5897c05aee3049a77b3b46b854 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 1 Nov 2022 11:40:36 +0100 Subject: [PATCH] turned completion subcommand into option --- cmd/root.go | 56 +++++++++++++++++++++++++------------------------ cmd/tablizer.go | 24 ++++++++++++++------- tablizer.1 | 24 +++++++++++++-------- tablizer.pod | 23 +++++++++++++------- 4 files changed, 75 insertions(+), 52 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 73947ca..9388cea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ package cmd import ( "bytes" + "errors" "fmt" "github.com/spf13/cobra" "github.com/tlinden/tablizer/cfg" @@ -45,35 +46,33 @@ func man() { } } +func completion(cmd *cobra.Command, mode string) error { + switch mode { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + default: + return errors.New("Invalid shell parameter! Valid ones: bash|zsh|fish|powershell") + } + + return nil +} + func Execute() { var ( - conf cfg.Config - ShowManual bool - ShowVersion bool - modeflag cfg.Modeflag - sortmode cfg.Sortmode + conf cfg.Config + ShowManual bool + ShowVersion bool + ShowCompletion string + modeflag cfg.Modeflag + sortmode cfg.Sortmode ) - var completionCmd = &cobra.Command{ - Use: "completion [bash|zsh|fish|powershell]", - Short: "Generate completion script", - DisableFlagsInUseLine: true, - ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, - Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { - switch args[0] { - case "bash": - cmd.Root().GenBashCompletion(os.Stdout) - case "zsh": - cmd.Root().GenZshCompletion(os.Stdout) - case "fish": - cmd.Root().GenFishCompletion(os.Stdout, true) - case "powershell": - cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) - } - }, - } - var rootCmd = &cobra.Command{ Use: "tablizer [regex] [file, ...]", Short: "[Re-]tabularize tabular data", @@ -89,6 +88,10 @@ func Execute() { return nil } + if len(ShowCompletion) > 0 { + return completion(cmd, ShowCompletion) + } + // Setup conf.CheckEnv() conf.PrepareModeFlags(modeflag) @@ -101,8 +104,6 @@ func Execute() { }, } - rootCmd.AddCommand(completionCmd) - // options rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging") rootCmd.PersistentFlags().BoolVarP(&conf.NoNumbering, "no-numbering", "n", false, "Disable header numbering") @@ -110,6 +111,7 @@ func Execute() { rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "V", false, "Print program version") rootCmd.PersistentFlags().BoolVarP(&conf.InvertMatch, "invert-match", "v", false, "select non-matching rows") rootCmd.PersistentFlags().BoolVarP(&ShowManual, "man", "m", false, "Display manual page") + rootCmd.PersistentFlags().StringVarP(&ShowCompletion, "completion", "", "", "Display completion code") rootCmd.PersistentFlags().StringVarP(&conf.Separator, "separator", "s", cfg.DefaultSeparator, "Custom field separator") rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") diff --git a/cmd/tablizer.go b/cmd/tablizer.go index e3b5a2c..32062ba 100644 --- a/cmd/tablizer.go +++ b/cmd/tablizer.go @@ -32,6 +32,7 @@ SYNOPSIS -t, --sort-time sort according to time string Other Flags: + --completion Generate the autocompletion script for -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page @@ -200,16 +201,22 @@ DESCRIPTION - disable colorization of matches, like -N COMPLETION + Shell completion for command line options can be enabled by using the + --completion flag. The required parameter is the name of your shell. + Currently supported are: bash, zsh, fish and powershell. + + Detailed instructions: + Bash: - source <(%[1]s completion bash) + source <(tablizer --completion bash) To load completions for each session, execute once: # Linux: - $ tablizer completion bash > /etc/bash_completion.d/%[1]s + $ tablizer --completion bash > /etc/bash_completion.d/tablizer # macOS: - $ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + $ tablizer --completion bash > $(brew --prefix)/etc/bash_completion.d/tablizer Zsh: If shell completion is not already enabled in your environment, you @@ -219,23 +226,23 @@ DESCRIPTION To load completions for each session, execute once: - $ tablizer completion zsh > "${fpath[1]}/_%[1]s" + $ tablizer --completion zsh > "${fpath[1]}/_tablizer" You will need to start a new shell for this setup to take effect. fish: - tablizer completion fish | source + tablizer --completion fish | source To load completions for each session, execute once: - tablizer completion fish > ~/.config/fish/completions/%[1]s.fish + tablizer --completion fish > ~/.config/fish/completions/tablizer.fish PowerShell: - tablizer completion powershell | Out-String | Invoke-Expression + tablizer --completion powershell | Out-String | Invoke-Expression To load completions for every new session, run: - tablizer completion powershell > tablizer.ps1 + tablizer --completion powershell > tablizer.ps1 and source this file from your PowerShell profile. @@ -292,6 +299,7 @@ Sort Mode Flags (mutually exclusive): -t, --sort-time sort according to time string Other Flags: + --completion Generate the autocompletion script for -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page diff --git a/tablizer.1 b/tablizer.1 index 4058147..5868fca 100644 --- a/tablizer.1 +++ b/tablizer.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "TABLIZER 1" -.TH TABLIZER 1 "2022-10-31" "1" "User Commands" +.TH TABLIZER 1 "2022-11-01" "1" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -170,6 +170,7 @@ tablizer \- Manipulate tabular output of other programs \& \-t, \-\-sort\-time sort according to time string \& \& Other Flags: +\& \-\-completion Generate the autocompletion script for \& \-d, \-\-debug Enable debugging \& \-h, \-\-help help for tablizer \& \-m, \-\-man Display manual page @@ -372,20 +373,25 @@ precedence over environment variables. .PD .SS "\s-1COMPLETION\s0" .IX Subsection "COMPLETION" +Shell completion for command line options can be enabled by using the +\&\fB\-\-completion\fR flag. The required parameter is the name of your +shell. Currently supported are: bash, zsh, fish and powershell. +.PP +Detailed instructions: .IP "Bash:" 4 .IX Item "Bash:" .Vb 1 -\& source <(%[1]s completion bash) +\& source <(tablizer \-\-completion bash) .Ve .Sp To load completions for each session, execute once: .Sp .Vb 2 \& # Linux: -\& $ tablizer completion bash > /etc/bash_completion.d/%[1]s +\& $ tablizer \-\-completion bash > /etc/bash_completion.d/tablizer \& \& # macOS: -\& $ tablizer completion bash > $(brew \-\-prefix)/etc/bash_completion.d/%[1]s +\& $ tablizer \-\-completion bash > $(brew \-\-prefix)/etc/bash_completion.d/tablizer .Ve .IP "Zsh:" 4 .IX Item "Zsh:" @@ -399,31 +405,31 @@ you will need to enable it. You can execute the following once: To load completions for each session, execute once: .Sp .Vb 1 -\& $ tablizer completion zsh > "${fpath[1]}/_%[1]s" +\& $ tablizer \-\-completion zsh > "${fpath[1]}/_tablizer" .Ve .Sp You will need to start a new shell for this setup to take effect. .IP "fish:" 4 .IX Item "fish:" .Vb 1 -\& tablizer completion fish | source +\& tablizer \-\-completion fish | source .Ve .Sp To load completions for each session, execute once: .Sp .Vb 1 -\& tablizer completion fish > ~/.config/fish/completions/%[1]s.fish +\& tablizer \-\-completion fish > ~/.config/fish/completions/tablizer.fish .Ve .IP "PowerShell:" 4 .IX Item "PowerShell:" .Vb 1 -\& tablizer completion powershell | Out\-String | Invoke\-Expression +\& tablizer \-\-completion powershell | Out\-String | Invoke\-Expression .Ve .Sp To load completions for every new session, run: .Sp .Vb 1 -\& tablizer completion powershell > tablizer.ps1 +\& tablizer \-\-completion powershell > tablizer.ps1 .Ve .Sp and source this file from your PowerShell profile. diff --git a/tablizer.pod b/tablizer.pod index fd7a238..bac92b4 100644 --- a/tablizer.pod +++ b/tablizer.pod @@ -31,6 +31,7 @@ tablizer - Manipulate tabular output of other programs -t, --sort-time sort according to time string Other Flags: + --completion Generate the autocompletion script for -d, --debug Enable debugging -h, --help help for tablizer -m, --man Display manual page @@ -229,19 +230,25 @@ precedence over environment variables. =head2 COMPLETION +Shell completion for command line options can be enabled by using the +B<--completion> flag. The required parameter is the name of your +shell. Currently supported are: bash, zsh, fish and powershell. + +Detailed instructions: + =over =item Bash: - source <(%[1]s completion bash) + source <(tablizer --completion bash) To load completions for each session, execute once: # Linux: - $ tablizer completion bash > /etc/bash_completion.d/%[1]s + $ tablizer --completion bash > /etc/bash_completion.d/tablizer # macOS: - $ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + $ tablizer --completion bash > $(brew --prefix)/etc/bash_completion.d/tablizer =item Zsh: @@ -252,25 +259,25 @@ you will need to enable it. You can execute the following once: To load completions for each session, execute once: - $ tablizer completion zsh > "${fpath[1]}/_%[1]s" + $ tablizer --completion zsh > "${fpath[1]}/_tablizer" You will need to start a new shell for this setup to take effect. =item fish: - tablizer completion fish | source + tablizer --completion fish | source To load completions for each session, execute once: - tablizer completion fish > ~/.config/fish/completions/%[1]s.fish + tablizer --completion fish > ~/.config/fish/completions/tablizer.fish =item PowerShell: - tablizer completion powershell | Out-String | Invoke-Expression + tablizer --completion powershell | Out-String | Invoke-Expression To load completions for every new session, run: - tablizer completion powershell > tablizer.ps1 + tablizer --completion powershell > tablizer.ps1 and source this file from your PowerShell profile.