mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
turned completion subcommand into option
This commit is contained in:
56
cmd/root.go
56
cmd/root.go
@@ -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,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() {
|
func Execute() {
|
||||||
var (
|
var (
|
||||||
conf cfg.Config
|
conf cfg.Config
|
||||||
ShowManual bool
|
ShowManual bool
|
||||||
ShowVersion bool
|
ShowVersion bool
|
||||||
modeflag cfg.Modeflag
|
ShowCompletion string
|
||||||
sortmode cfg.Sortmode
|
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{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "tablizer [regex] [file, ...]",
|
Use: "tablizer [regex] [file, ...]",
|
||||||
Short: "[Re-]tabularize tabular data",
|
Short: "[Re-]tabularize tabular data",
|
||||||
@@ -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 ,)")
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
24
tablizer.1
24
tablizer.1
@@ -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.
|
||||||
|
|||||||
23
tablizer.pod
23
tablizer.pod
@@ -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.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user