turned completion subcommand into option

This commit is contained in:
2022-11-01 11:40:36 +01:00
parent 8552270a68
commit 335b2665f2
4 changed files with 75 additions and 52 deletions

View File

@@ -18,6 +18,7 @@ package cmd
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/tlinden/tablizer/cfg" "github.com/tlinden/tablizer/cfg"
@@ -45,23 +46,8 @@ func man() {
} }
} }
func Execute() { func completion(cmd *cobra.Command, mode string) error {
var ( switch mode {
conf cfg.Config
ShowManual bool
ShowVersion bool
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": case "bash":
cmd.Root().GenBashCompletion(os.Stdout) cmd.Root().GenBashCompletion(os.Stdout)
case "zsh": case "zsh":
@@ -70,9 +56,22 @@ func Execute() {
cmd.Root().GenFishCompletion(os.Stdout, true) cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell": case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) 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
ShowCompletion string
modeflag cfg.Modeflag
sortmode cfg.Sortmode
)
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "tablizer [regex] [file, ...]", Use: "tablizer [regex] [file, ...]",
@@ -89,6 +88,10 @@ func Execute() {
return nil return nil
} }
if len(ShowCompletion) > 0 {
return completion(cmd, ShowCompletion)
}
// Setup // Setup
conf.CheckEnv() conf.CheckEnv()
conf.PrepareModeFlags(modeflag) conf.PrepareModeFlags(modeflag)
@@ -101,8 +104,6 @@ func Execute() {
}, },
} }
rootCmd.AddCommand(completionCmd)
// options // options
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging") rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
rootCmd.PersistentFlags().BoolVarP(&conf.NoNumbering, "no-numbering", "n", false, "Disable header numbering") 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(&ShowVersion, "version", "V", false, "Print program version")
rootCmd.PersistentFlags().BoolVarP(&conf.InvertMatch, "invert-match", "v", false, "select non-matching rows") 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().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.Separator, "separator", "s", cfg.DefaultSeparator, "Custom field separator")
rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)") rootCmd.PersistentFlags().StringVarP(&conf.Columns, "columns", "c", "", "Only show the speficied columns (separated by ,)")

View File

@@ -32,6 +32,7 @@ SYNOPSIS
-t, --sort-time sort according to time string -t, --sort-time sort according to time string
Other Flags: Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-d, --debug Enable debugging -d, --debug Enable debugging
-h, --help help for tablizer -h, --help help for tablizer
-m, --man Display manual page -m, --man Display manual page
@@ -200,16 +201,22 @@ DESCRIPTION
<NO_COLORS> - disable colorization of matches, like -N <NO_COLORS> - disable colorization of matches, like -N
COMPLETION 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: Bash:
source <(%[1]s completion bash) source <(tablizer --completion bash)
To load completions for each session, execute once: To load completions for each session, execute once:
# Linux: # Linux:
$ tablizer completion bash > /etc/bash_completion.d/%[1]s $ tablizer --completion bash > /etc/bash_completion.d/tablizer
# macOS: # macOS:
$ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s $ tablizer --completion bash > $(brew --prefix)/etc/bash_completion.d/tablizer
Zsh: Zsh:
If shell completion is not already enabled in your environment, you If shell completion is not already enabled in your environment, you
@@ -219,23 +226,23 @@ DESCRIPTION
To load completions for each session, execute 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. You will need to start a new shell for this setup to take effect.
fish: fish:
tablizer completion fish | source tablizer --completion fish | source
To load completions for each session, execute once: 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: PowerShell:
tablizer completion powershell | Out-String | Invoke-Expression tablizer --completion powershell | Out-String | Invoke-Expression
To load completions for every new session, run: 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. 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 -t, --sort-time sort according to time string
Other Flags: Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-d, --debug Enable debugging -d, --debug Enable debugging
-h, --help help for tablizer -h, --help help for tablizer
-m, --man Display manual page -m, --man Display manual page

View File

@@ -133,7 +133,7 @@
.\" ======================================================================== .\" ========================================================================
.\" .\"
.IX Title "TABLIZER 1" .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 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents. .\" way too many mistakes in technical documents.
.if n .ad l .if n .ad l
@@ -170,6 +170,7 @@ tablizer \- Manipulate tabular output of other programs
\& \-t, \-\-sort\-time sort according to time string \& \-t, \-\-sort\-time sort according to time string
\& \&
\& Other Flags: \& Other Flags:
\& \-\-completion <shell> Generate the autocompletion script for <shell>
\& \-d, \-\-debug Enable debugging \& \-d, \-\-debug Enable debugging
\& \-h, \-\-help help for tablizer \& \-h, \-\-help help for tablizer
\& \-m, \-\-man Display manual page \& \-m, \-\-man Display manual page
@@ -372,20 +373,25 @@ precedence over environment variables.
.PD .PD
.SS "\s-1COMPLETION\s0" .SS "\s-1COMPLETION\s0"
.IX Subsection "COMPLETION" .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 .IP "Bash:" 4
.IX Item "Bash:" .IX Item "Bash:"
.Vb 1 .Vb 1
\& source <(%[1]s completion bash) \& source <(tablizer \-\-completion bash)
.Ve .Ve
.Sp .Sp
To load completions for each session, execute once: To load completions for each session, execute once:
.Sp .Sp
.Vb 2 .Vb 2
\& # Linux: \& # Linux:
\& $ tablizer completion bash > /etc/bash_completion.d/%[1]s \& $ tablizer \-\-completion bash > /etc/bash_completion.d/tablizer
\& \&
\& # macOS: \& # macOS:
\& $ tablizer completion bash > $(brew \-\-prefix)/etc/bash_completion.d/%[1]s \& $ tablizer \-\-completion bash > $(brew \-\-prefix)/etc/bash_completion.d/tablizer
.Ve .Ve
.IP "Zsh:" 4 .IP "Zsh:" 4
.IX Item "Zsh:" .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: To load completions for each session, execute once:
.Sp .Sp
.Vb 1 .Vb 1
\& $ tablizer completion zsh > "${fpath[1]}/_%[1]s" \& $ tablizer \-\-completion zsh > "${fpath[1]}/_tablizer"
.Ve .Ve
.Sp .Sp
You will need to start a new shell for this setup to take effect. You will need to start a new shell for this setup to take effect.
.IP "fish:" 4 .IP "fish:" 4
.IX Item "fish:" .IX Item "fish:"
.Vb 1 .Vb 1
\& tablizer completion fish | source \& tablizer \-\-completion fish | source
.Ve .Ve
.Sp .Sp
To load completions for each session, execute once: To load completions for each session, execute once:
.Sp .Sp
.Vb 1 .Vb 1
\& tablizer completion fish > ~/.config/fish/completions/%[1]s.fish \& tablizer \-\-completion fish > ~/.config/fish/completions/tablizer.fish
.Ve .Ve
.IP "PowerShell:" 4 .IP "PowerShell:" 4
.IX Item "PowerShell:" .IX Item "PowerShell:"
.Vb 1 .Vb 1
\& tablizer completion powershell | Out\-String | Invoke\-Expression \& tablizer \-\-completion powershell | Out\-String | Invoke\-Expression
.Ve .Ve
.Sp .Sp
To load completions for every new session, run: To load completions for every new session, run:
.Sp .Sp
.Vb 1 .Vb 1
\& tablizer completion powershell > tablizer.ps1 \& tablizer \-\-completion powershell > tablizer.ps1
.Ve .Ve
.Sp .Sp
and source this file from your PowerShell profile. and source this file from your PowerShell profile.

View File

@@ -31,6 +31,7 @@ tablizer - Manipulate tabular output of other programs
-t, --sort-time sort according to time string -t, --sort-time sort according to time string
Other Flags: Other Flags:
--completion <shell> Generate the autocompletion script for <shell>
-d, --debug Enable debugging -d, --debug Enable debugging
-h, --help help for tablizer -h, --help help for tablizer
-m, --man Display manual page -m, --man Display manual page
@@ -229,19 +230,25 @@ precedence over environment variables.
=head2 COMPLETION =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 =over
=item Bash: =item Bash:
source <(%[1]s completion bash) source <(tablizer --completion bash)
To load completions for each session, execute once: To load completions for each session, execute once:
# Linux: # Linux:
$ tablizer completion bash > /etc/bash_completion.d/%[1]s $ tablizer --completion bash > /etc/bash_completion.d/tablizer
# macOS: # macOS:
$ tablizer completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s $ tablizer --completion bash > $(brew --prefix)/etc/bash_completion.d/tablizer
=item Zsh: =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: 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. You will need to start a new shell for this setup to take effect.
=item fish: =item fish:
tablizer completion fish | source tablizer --completion fish | source
To load completions for each session, execute once: 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: =item PowerShell:
tablizer completion powershell | Out-String | Invoke-Expression tablizer --completion powershell | Out-String | Invoke-Expression
To load completions for every new session, run: 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. and source this file from your PowerShell profile.