Updated transitive dependencies

This commit is contained in:
Seednode 2022-10-13 11:19:34 -05:00
parent 84038440af
commit 7f27050595
23 changed files with 459 additions and 89 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.19
require ( require (
github.com/h2non/filetype v1.1.3 github.com/h2non/filetype v1.1.3
github.com/spf13/cobra v1.5.0 github.com/spf13/cobra v1.6.0
) )
require ( require (

7
go.sum
View File

@ -1,13 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,3 +1,17 @@
# Copyright 2013-2022 The Cobra Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
run: run:
deadline: 5m deadline: 5m

View File

@ -6,7 +6,7 @@ $(warning "could not find golangci-lint in $(PATH), run: curl -sfL https://insta
endif endif
ifeq (, $(shell which richgo)) ifeq (, $(shell which richgo))
$(warning "could not find richgo in $(PATH), run: go get github.com/kyoh86/richgo") $(warning "could not find richgo in $(PATH), run: go install github.com/kyoh86/richgo@latest")
endif endif
.PHONY: fmt lint test install_deps clean .PHONY: fmt lint test install_deps clean

View File

@ -3,7 +3,7 @@
Cobra is a library for creating powerful modern CLI applications. Cobra is a library for creating powerful modern CLI applications.
Cobra is used in many Go projects such as [Kubernetes](https://kubernetes.io/), Cobra is used in many Go projects such as [Kubernetes](https://kubernetes.io/),
[Hugo](https://gohugo.io), and [Github CLI](https://github.com/cli/cli) to [Hugo](https://gohugo.io), and [GitHub CLI](https://github.com/cli/cli) to
name a few. [This list](./projects_using_cobra.md) contains a more extensive list of projects using Cobra. name a few. [This list](./projects_using_cobra.md) contains a more extensive list of projects using Cobra.
[![](https://img.shields.io/github/workflow/status/spf13/cobra/Test?longCache=tru&label=Test&logo=github%20actions&logoColor=fff)](https://github.com/spf13/cobra/actions?query=workflow%3ATest) [![](https://img.shields.io/github/workflow/status/spf13/cobra/Test?longCache=tru&label=Test&logo=github%20actions&logoColor=fff)](https://github.com/spf13/cobra/actions?query=workflow%3ATest)
@ -23,6 +23,7 @@ Cobra provides:
* Global, local and cascading flags * Global, local and cascading flags
* Intelligent suggestions (`app srver`... did you mean `app server`?) * Intelligent suggestions (`app srver`... did you mean `app server`?)
* Automatic help generation for commands and flags * Automatic help generation for commands and flags
* Grouping help for subcommands
* Automatic help flag recognition of `-h`, `--help`, etc. * Automatic help flag recognition of `-h`, `--help`, etc.
* Automatically generated shell autocomplete for your application (bash, zsh, fish, powershell) * Automatically generated shell autocomplete for your application (bash, zsh, fish, powershell)
* Automatically generated man pages for your application * Automatically generated man pages for your application
@ -40,9 +41,9 @@ The best applications read like sentences when used, and as a result, users
intuitively know how to interact with them. intuitively know how to interact with them.
The pattern to follow is The pattern to follow is
`APPNAME VERB NOUN --ADJECTIVE.` `APPNAME VERB NOUN --ADJECTIVE`
or or
`APPNAME COMMAND ARG --FLAG` `APPNAME COMMAND ARG --FLAG`.
A few good real world examples may better illustrate this point. A few good real world examples may better illustrate this point.

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (
@ -32,7 +46,8 @@ func NoArgs(cmd *Command, args []string) error {
return nil return nil
} }
// OnlyValidArgs returns an error if any args are not in the list of ValidArgs. // OnlyValidArgs returns an error if there are any positional args that are not in
// the `ValidArgs` field of `Command`
func OnlyValidArgs(cmd *Command, args []string) error { func OnlyValidArgs(cmd *Command, args []string) error {
if len(cmd.ValidArgs) > 0 { if len(cmd.ValidArgs) > 0 {
// Remove any description that may be included in ValidArgs. // Remove any description that may be included in ValidArgs.
@ -41,7 +56,6 @@ func OnlyValidArgs(cmd *Command, args []string) error {
for _, v := range cmd.ValidArgs { for _, v := range cmd.ValidArgs {
validArgs = append(validArgs, strings.Split(v, "\t")[0]) validArgs = append(validArgs, strings.Split(v, "\t")[0])
} }
for _, v := range args { for _, v := range args {
if !stringInSlice(v, validArgs) { if !stringInSlice(v, validArgs) {
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0])) return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
@ -86,18 +100,6 @@ func ExactArgs(n int) PositionalArgs {
} }
} }
// ExactValidArgs returns an error if
// there are not exactly N positional args OR
// there are any positional args that are not in the `ValidArgs` field of `Command`
func ExactValidArgs(n int) PositionalArgs {
return func(cmd *Command, args []string) error {
if err := ExactArgs(n)(cmd, args); err != nil {
return err
}
return OnlyValidArgs(cmd, args)
}
}
// RangeArgs returns an error if the number of args is not within the expected range. // RangeArgs returns an error if the number of args is not within the expected range.
func RangeArgs(min int, max int) PositionalArgs { func RangeArgs(min int, max int) PositionalArgs {
return func(cmd *Command, args []string) error { return func(cmd *Command, args []string) error {
@ -119,3 +121,11 @@ func MatchAll(pargs ...PositionalArgs) PositionalArgs {
return nil return nil
} }
} }
// ExactValidArgs returns an error if there are not exactly N positional args OR
// there are any positional args that are not in the `ValidArgs` field of `Command`
//
// Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead
func ExactValidArgs(n int) PositionalArgs {
return MatchAll(ExactArgs(n), OnlyValidArgs)
}

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (
@ -150,7 +164,7 @@ __%[1]s_process_completion_results() {
__%[1]s_handle_special_char "$cur" = __%[1]s_handle_special_char "$cur" =
# Print the activeHelp statements before we finish # Print the activeHelp statements before we finish
if [ ${#activeHelp} -ne 0 ]; then if [ ${#activeHelp[*]} -ne 0 ]; then
printf "\n"; printf "\n";
printf "%%s\n" "${activeHelp[@]}" printf "%%s\n" "${activeHelp[@]}"
printf "\n" printf "\n"

View File

@ -1,9 +1,10 @@
// Copyright © 2013 Steve Francia <spf@spf13.com>. // Copyright 2013-2022 The Cobra Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0 //
// http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
@ -39,15 +40,25 @@ var templateFuncs = template.FuncMap{
} }
var initializers []func() var initializers []func()
var finalizers []func()
const (
defaultPrefixMatching = false
defaultCommandSorting = true
defaultCaseInsensitive = false
)
// EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing // EnablePrefixMatching allows to set automatic prefix matching. Automatic prefix matching can be a dangerous thing
// to automatically enable in CLI tools. // to automatically enable in CLI tools.
// Set this to true to enable it. // Set this to true to enable it.
var EnablePrefixMatching = false var EnablePrefixMatching = defaultPrefixMatching
// EnableCommandSorting controls sorting of the slice of commands, which is turned on by default. // EnableCommandSorting controls sorting of the slice of commands, which is turned on by default.
// To disable sorting, set it to false. // To disable sorting, set it to false.
var EnableCommandSorting = true var EnableCommandSorting = defaultCommandSorting
// EnableCaseInsensitive allows case-insensitive commands names. (case sensitive by default)
var EnableCaseInsensitive = defaultCaseInsensitive
// MousetrapHelpText enables an information splash screen on Windows // MousetrapHelpText enables an information splash screen on Windows
// if the CLI is started from explorer.exe. // if the CLI is started from explorer.exe.
@ -84,6 +95,12 @@ func OnInitialize(y ...func()) {
initializers = append(initializers, y...) initializers = append(initializers, y...)
} }
// OnFinalize sets the passed functions to be run when each command's
// Execute method is terminated.
func OnFinalize(y ...func()) {
finalizers = append(finalizers, y...)
}
// FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra. // FIXME Gt is unused by cobra and should be removed in a version 2. It exists only for compatibility with users of cobra.
// Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans, // Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,

View File

@ -1,9 +1,10 @@
// Copyright © 2013 Steve Francia <spf@spf13.com>. // Copyright 2013-2022 The Cobra Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0 //
// http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
@ -29,9 +30,17 @@ import (
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
) )
const FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"
// FParseErrWhitelist configures Flag parse errors to be ignored // FParseErrWhitelist configures Flag parse errors to be ignored
type FParseErrWhitelist flag.ParseErrorsWhitelist type FParseErrWhitelist flag.ParseErrorsWhitelist
// Structure to manage groups for commands
type Group struct {
ID string
Title string
}
// Command is just that, a command for your application. // Command is just that, a command for your application.
// E.g. 'go run ...' - 'run' is the command. Cobra requires // E.g. 'go run ...' - 'run' is the command. Cobra requires
// you to define the usage and description as part of your command // you to define the usage and description as part of your command
@ -58,6 +67,9 @@ type Command struct {
// Short is the short description shown in the 'help' output. // Short is the short description shown in the 'help' output.
Short string Short string
// The group id under which this subcommand is grouped in the 'help' output of its parent.
GroupID string
// Long is the long message shown in the 'help <this-command>' output. // Long is the long message shown in the 'help <this-command>' output.
Long string Long string
@ -125,6 +137,9 @@ type Command struct {
// PersistentPostRunE: PersistentPostRun but returns an error. // PersistentPostRunE: PersistentPostRun but returns an error.
PersistentPostRunE func(cmd *Command, args []string) error PersistentPostRunE func(cmd *Command, args []string) error
// groups for subcommands
commandgroups []*Group
// args is actual args parsed from flags. // args is actual args parsed from flags.
args []string args []string
// flagErrorBuf contains all error messages from pflag. // flagErrorBuf contains all error messages from pflag.
@ -157,6 +172,12 @@ type Command struct {
// helpCommand is command with usage 'help'. If it's not defined by user, // helpCommand is command with usage 'help'. If it's not defined by user,
// cobra uses default help command. // cobra uses default help command.
helpCommand *Command helpCommand *Command
// helpCommandGroupID is the group id for the helpCommand
helpCommandGroupID string
// completionCommandGroupID is the group id for the completion command
completionCommandGroupID string
// versionTemplate is the version template defined by user. // versionTemplate is the version template defined by user.
versionTemplate string versionTemplate string
@ -236,8 +257,8 @@ func (c *Command) Context() context.Context {
return c.ctx return c.ctx
} }
// SetContext sets context for the command. It is set to context.Background by default and will be overwritten by // SetContext sets context for the command. This context will be overwritten by
// Command.ExecuteContext or Command.ExecuteContextC // Command.ExecuteContext or Command.ExecuteContextC.
func (c *Command) SetContext(ctx context.Context) { func (c *Command) SetContext(ctx context.Context) {
c.ctx = ctx c.ctx = ctx
} }
@ -300,6 +321,21 @@ func (c *Command) SetHelpCommand(cmd *Command) {
c.helpCommand = cmd c.helpCommand = cmd
} }
// SetHelpCommandGroup sets the group id of the help command.
func (c *Command) SetHelpCommandGroupID(groupID string) {
if c.helpCommand != nil {
c.helpCommand.GroupID = groupID
}
// helpCommandGroupID is used if no helpCommand is defined by the user
c.helpCommandGroupID = groupID
}
// SetCompletionCommandGroup sets the group id of the completion command.
func (c *Command) SetCompletionCommandGroupID(groupID string) {
// completionCommandGroupID is used if no completion command is defined by the user
c.Root().completionCommandGroupID = groupID
}
// SetHelpTemplate sets help template to be used. Application can use it to set custom template. // SetHelpTemplate sets help template to be used. Application can use it to set custom template.
func (c *Command) SetHelpTemplate(s string) { func (c *Command) SetHelpTemplate(s string) {
c.helpTemplate = s c.helpTemplate = s
@ -508,10 +544,16 @@ Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}} {{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples: Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}} {{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags: Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
@ -676,7 +718,7 @@ func (c *Command) findSuggestions(arg string) string {
func (c *Command) findNext(next string) *Command { func (c *Command) findNext(next string) *Command {
matches := make([]*Command, 0) matches := make([]*Command, 0)
for _, cmd := range c.commands { for _, cmd := range c.commands {
if cmd.Name() == next || cmd.HasAlias(next) { if commandNameMatches(cmd.Name(), next) || cmd.HasAlias(next) {
cmd.commandCalledAs.name = next cmd.commandCalledAs.name = next
return cmd return cmd
} }
@ -833,6 +875,8 @@ func (c *Command) execute(a []string) (err error) {
c.preRun() c.preRun()
defer c.postRun()
argWoFlags := c.Flags().Args() argWoFlags := c.Flags().Args()
if c.DisableFlagParsing { if c.DisableFlagParsing {
argWoFlags = a argWoFlags = a
@ -861,10 +905,10 @@ func (c *Command) execute(a []string) (err error) {
c.PreRun(c, argWoFlags) c.PreRun(c, argWoFlags)
} }
if err := c.validateRequiredFlags(); err != nil { if err := c.ValidateRequiredFlags(); err != nil {
return err return err
} }
if err := c.validateFlagGroups(); err != nil { if err := c.ValidateFlagGroups(); err != nil {
return err return err
} }
@ -903,6 +947,12 @@ func (c *Command) preRun() {
} }
} }
func (c *Command) postRun() {
for _, x := range finalizers {
x()
}
}
// ExecuteContext is the same as Execute(), but sets the ctx on the command. // ExecuteContext is the same as Execute(), but sets the ctx on the command.
// Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs
// functions. // functions.
@ -946,7 +996,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// initialize help at the last point to allow for user overriding // initialize help at the last point to allow for user overriding
c.InitDefaultHelpCmd() c.InitDefaultHelpCmd()
// initialize completion at the last point to allow for user overriding // initialize completion at the last point to allow for user overriding
c.initDefaultCompletionCmd() c.InitDefaultCompletionCmd()
args := c.args args := c.args
@ -1018,7 +1068,8 @@ func (c *Command) ValidateArgs(args []string) error {
return c.Args(c, args) return c.Args(c, args)
} }
func (c *Command) validateRequiredFlags() error { // ValidateRequiredFlags validates all required flags are present and returns an error otherwise
func (c *Command) ValidateRequiredFlags() error {
if c.DisableFlagParsing { if c.DisableFlagParsing {
return nil return nil
} }
@ -1054,6 +1105,7 @@ func (c *Command) InitDefaultHelpFlag() {
usage += c.Name() usage += c.Name()
} }
c.Flags().BoolP("help", "h", false, usage) c.Flags().BoolP("help", "h", false, usage)
_ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{"true"})
} }
} }
@ -1079,6 +1131,7 @@ func (c *Command) InitDefaultVersionFlag() {
} else { } else {
c.Flags().Bool("version", false, usage) c.Flags().Bool("version", false, usage)
} }
_ = c.Flags().SetAnnotation("version", FlagSetByCobraAnnotation, []string{"true"})
} }
} }
@ -1121,10 +1174,12 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
c.Printf("Unknown help topic %#q\n", args) c.Printf("Unknown help topic %#q\n", args)
CheckErr(c.Root().Usage()) CheckErr(c.Root().Usage())
} else { } else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.InitDefaultVersionFlag() // make possible 'version' flag to be shown
CheckErr(cmd.Help()) CheckErr(cmd.Help())
} }
}, },
GroupID: c.helpCommandGroupID,
} }
} }
c.RemoveCommand(c.helpCommand) c.RemoveCommand(c.helpCommand)
@ -1163,6 +1218,10 @@ func (c *Command) AddCommand(cmds ...*Command) {
panic("Command can't be a child of itself") panic("Command can't be a child of itself")
} }
cmds[i].parent = c cmds[i].parent = c
// if Group is not defined let the developer know right away
if x.GroupID != "" && !c.ContainsGroup(x.GroupID) {
panic(fmt.Sprintf("Group id '%s' is not defined for subcommand '%s'", x.GroupID, cmds[i].CommandPath()))
}
// update max lengths // update max lengths
usageLen := len(x.Use) usageLen := len(x.Use)
if usageLen > c.commandsMaxUseLen { if usageLen > c.commandsMaxUseLen {
@ -1185,6 +1244,36 @@ func (c *Command) AddCommand(cmds ...*Command) {
} }
} }
// Groups returns a slice of child command groups.
func (c *Command) Groups() []*Group {
return c.commandgroups
}
// AllChildCommandsHaveGroup returns if all subcommands are assigned to a group
func (c *Command) AllChildCommandsHaveGroup() bool {
for _, sub := range c.commands {
if (sub.IsAvailableCommand() || sub == c.helpCommand) && sub.GroupID == "" {
return false
}
}
return true
}
// ContainGroups return if groupID exists in the list of command groups.
func (c *Command) ContainsGroup(groupID string) bool {
for _, x := range c.commandgroups {
if x.ID == groupID {
return true
}
}
return false
}
// AddGroup adds one or more command groups to this parent command.
func (c *Command) AddGroup(groups ...*Group) {
c.commandgroups = append(c.commandgroups, groups...)
}
// RemoveCommand removes one or more commands from a parent command. // RemoveCommand removes one or more commands from a parent command.
func (c *Command) RemoveCommand(cmds ...*Command) { func (c *Command) RemoveCommand(cmds ...*Command) {
commands := []*Command{} commands := []*Command{}
@ -1328,7 +1417,7 @@ func (c *Command) Name() string {
// HasAlias determines if a given string is an alias of the command. // HasAlias determines if a given string is an alias of the command.
func (c *Command) HasAlias(s string) bool { func (c *Command) HasAlias(s string) bool {
for _, a := range c.Aliases { for _, a := range c.Aliases {
if a == s { if commandNameMatches(a, s) {
return true return true
} }
} }
@ -1505,7 +1594,8 @@ func (c *Command) LocalFlags() *flag.FlagSet {
} }
addToLocal := func(f *flag.Flag) { addToLocal := func(f *flag.Flag) {
if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil { // Add the flag if it is not a parent PFlag, or it shadows a parent PFlag
if c.lflags.Lookup(f.Name) == nil && f != c.parentsPflags.Lookup(f.Name) {
c.lflags.AddFlag(f) c.lflags.AddFlag(f)
} }
} }
@ -1694,3 +1784,14 @@ func (c *Command) updateParentsPflags() {
c.parentsPflags.AddFlagSet(parent.PersistentFlags()) c.parentsPflags.AddFlagSet(parent.PersistentFlags())
}) })
} }
// commandNameMatches checks if two command names are equal
// taking into account case sensitivity according to
// EnableCaseInsensitive global configuration.
func commandNameMatches(s string, t string) bool {
if EnableCaseInsensitive {
return strings.EqualFold(s, t)
}
return s == t
}

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows //go:build !windows
// +build !windows // +build !windows

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build windows //go:build windows
// +build windows // +build windows

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (
@ -260,6 +274,12 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
} }
finalCmd.ctx = c.ctx finalCmd.ctx = c.ctx
// These flags are normally added when `execute()` is called on `finalCmd`,
// however, when doing completion, we don't call `finalCmd.execute()`.
// Let's add the --help and --version flag ourselves.
finalCmd.InitDefaultHelpFlag()
finalCmd.InitDefaultVersionFlag()
// Check if we are doing flag value completion before parsing the flags. // Check if we are doing flag value completion before parsing the flags.
// This is important because if we are completing a flag value, we need to also // This is important because if we are completing a flag value, we need to also
// remove the flag name argument from the list of finalArgs or else the parsing // remove the flag name argument from the list of finalArgs or else the parsing
@ -292,6 +312,12 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
} }
} }
// Look for the --help or --version flags. If they are present,
// there should be no further completions.
if helpOrVersionFlagPresent(finalCmd) {
return finalCmd, []string{}, ShellCompDirectiveNoFileComp, nil
}
// We only remove the flags from the arguments if DisableFlagParsing is not set. // We only remove the flags from the arguments if DisableFlagParsing is not set.
// This is important for commands which have requested to do their own flag completion. // This is important for commands which have requested to do their own flag completion.
if !finalCmd.DisableFlagParsing { if !finalCmd.DisableFlagParsing {
@ -463,6 +489,18 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
return finalCmd, completions, directive, nil return finalCmd, completions, directive, nil
} }
func helpOrVersionFlagPresent(cmd *Command) bool {
if versionFlag := cmd.Flags().Lookup("version"); versionFlag != nil &&
len(versionFlag.Annotations[FlagSetByCobraAnnotation]) > 0 && versionFlag.Changed {
return true
}
if helpFlag := cmd.Flags().Lookup("help"); helpFlag != nil &&
len(helpFlag.Annotations[FlagSetByCobraAnnotation]) > 0 && helpFlag.Changed {
return true
}
return false
}
func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []string { func getFlagNameCompletions(flag *pflag.Flag, toComplete string) []string {
if nonCompletableFlag(flag) { if nonCompletableFlag(flag) {
return []string{} return []string{}
@ -607,12 +645,12 @@ func checkIfFlagCompletion(finalCmd *Command, args []string, lastArg string) (*p
return flag, trimmedArgs, lastArg, nil return flag, trimmedArgs, lastArg, nil
} }
// initDefaultCompletionCmd adds a default 'completion' command to c. // InitDefaultCompletionCmd adds a default 'completion' command to c.
// This function will do nothing if any of the following is true: // This function will do nothing if any of the following is true:
// 1- the feature has been explicitly disabled by the program, // 1- the feature has been explicitly disabled by the program,
// 2- c has no subcommands (to avoid creating one), // 2- c has no subcommands (to avoid creating one),
// 3- c already has a 'completion' command provided by the program. // 3- c already has a 'completion' command provided by the program.
func (c *Command) initDefaultCompletionCmd() { func (c *Command) InitDefaultCompletionCmd() {
if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() { if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() {
return return
} }
@ -635,6 +673,7 @@ See each sub-command's help for details on how to use the generated script.
Args: NoArgs, Args: NoArgs,
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
Hidden: c.CompletionOptions.HiddenDefaultCmd, Hidden: c.CompletionOptions.HiddenDefaultCmd,
GroupID: c.completionCommandGroupID,
} }
c.AddCommand(completionCmd) c.AddCommand(completionCmd)

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (

View File

@ -1,9 +1,10 @@
// Copyright © 2022 Steve Francia <spf@spf13.com>. // Copyright 2013-2022 The Cobra Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
// You may obtain a copy of the License at // You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0 //
// http://www.apache.org/licenses/LICENSE-2.0
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
@ -58,9 +59,9 @@ func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string) {
} }
} }
// validateFlagGroups validates the mutuallyExclusive/requiredAsGroup logic and returns the // ValidateFlagGroups validates the mutuallyExclusive/requiredAsGroup logic and returns the
// first error encountered. // first error encountered.
func (c *Command) validateFlagGroups() error { func (c *Command) ValidateFlagGroups() error {
if c.DisableFlagParsing { if c.DisableFlagParsing {
return nil return nil
} }

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The generated scripts require PowerShell v5.0+ (which comes Windows 10, but // The generated scripts require PowerShell v5.0+ (which comes Windows 10, but
// can be downloaded separately for windows 7 or 8.1). // can be downloaded separately for windows 7 or 8.1).
@ -8,9 +22,15 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
) )
func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) { func genPowerShellComp(buf io.StringWriter, name string, includeDesc bool) {
// Variables should not contain a '-' or ':' character
nameForVar := name
nameForVar = strings.Replace(nameForVar, "-", "_", -1)
nameForVar = strings.Replace(nameForVar, ":", "_", -1)
compCmd := ShellCompRequestCmd compCmd := ShellCompRequestCmd
if !includeDesc { if !includeDesc {
compCmd = ShellCompNoDescRequestCmd compCmd = ShellCompNoDescRequestCmd
@ -27,7 +47,7 @@ filter __%[1]s_escapeStringWithSpecialChars {
`+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+` `+" $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|\"|`|\\||<|>|&','`$&'"+`
} }
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock { [scriptblock]$__%[2]sCompleterBlock = {
param( param(
$WordToComplete, $WordToComplete,
$CommandAst, $CommandAst,
@ -52,17 +72,17 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
} }
__%[1]s_debug "Truncated command: $Command" __%[1]s_debug "Truncated command: $Command"
$ShellCompDirectiveError=%[3]d $ShellCompDirectiveError=%[4]d
$ShellCompDirectiveNoSpace=%[4]d $ShellCompDirectiveNoSpace=%[5]d
$ShellCompDirectiveNoFileComp=%[5]d $ShellCompDirectiveNoFileComp=%[6]d
$ShellCompDirectiveFilterFileExt=%[6]d $ShellCompDirectiveFilterFileExt=%[7]d
$ShellCompDirectiveFilterDirs=%[7]d $ShellCompDirectiveFilterDirs=%[8]d
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Split the command at the first space to separate the program and arguments. # Split the command at the first space to separate the program and arguments.
$Program,$Arguments = $Command.Split(" ",2) $Program,$Arguments = $Command.Split(" ",2)
$RequestComp="$Program %[2]s $Arguments" $RequestComp="$Program %[3]s $Arguments"
__%[1]s_debug "RequestComp: $RequestComp" __%[1]s_debug "RequestComp: $RequestComp"
# we cannot use $WordToComplete because it # we cannot use $WordToComplete because it
@ -92,7 +112,7 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
__%[1]s_debug "Calling $RequestComp" __%[1]s_debug "Calling $RequestComp"
# First disable ActiveHelp which is not supported for Powershell # First disable ActiveHelp which is not supported for Powershell
$env:%[8]s=0 $env:%[9]s=0
#call the command store the output in $out and redirect stderr and stdout to null #call the command store the output in $out and redirect stderr and stdout to null
# $Out is an array contains each line per element # $Out is an array contains each line per element
@ -243,7 +263,9 @@ Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock {
} }
} }
`, name, compCmd,
Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock
`, name, nameForVar, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))
} }

View File

@ -1,7 +1,10 @@
## Projects using Cobra ## Projects using Cobra
- [Allero](https://github.com/allero-io/allero)
- [Arduino CLI](https://github.com/arduino/arduino-cli) - [Arduino CLI](https://github.com/arduino/arduino-cli)
- [Bleve](https://blevesearch.com/) - [Bleve](https://blevesearch.com/)
- [Cilium](https://cilium.io/)
- [CloudQuery](https://github.com/cloudquery/cloudquery)
- [CockroachDB](https://www.cockroachlabs.com/) - [CockroachDB](https://www.cockroachlabs.com/)
- [Cosmos SDK](https://github.com/cosmos/cosmos-sdk) - [Cosmos SDK](https://github.com/cosmos/cosmos-sdk)
- [Datree](https://github.com/datreeio/datree) - [Datree](https://github.com/datreeio/datree)
@ -11,7 +14,7 @@
- [Gardener](https://github.com/gardener/gardenctl) - [Gardener](https://github.com/gardener/gardenctl)
- [Giant Swarm's gsctl](https://github.com/giantswarm/gsctl) - [Giant Swarm's gsctl](https://github.com/giantswarm/gsctl)
- [Git Bump](https://github.com/erdaltsksn/git-bump) - [Git Bump](https://github.com/erdaltsksn/git-bump)
- [Github CLI](https://github.com/cli/cli) - [GitHub CLI](https://github.com/cli/cli)
- [GitHub Labeler](https://github.com/erdaltsksn/gh-label) - [GitHub Labeler](https://github.com/erdaltsksn/gh-label)
- [Golangci-lint](https://golangci-lint.run) - [Golangci-lint](https://golangci-lint.run)
- [GopherJS](https://github.com/gopherjs/gopherjs) - [GopherJS](https://github.com/gopherjs/gopherjs)
@ -23,6 +26,7 @@
- [Kool](https://github.com/kool-dev/kool) - [Kool](https://github.com/kool-dev/kool)
- [Kubernetes](https://kubernetes.io/) - [Kubernetes](https://kubernetes.io/)
- [Kubescape](https://github.com/armosec/kubescape) - [Kubescape](https://github.com/armosec/kubescape)
- [KubeVirt](https://github.com/kubevirt/kubevirt)
- [Linkerd](https://linkerd.io/) - [Linkerd](https://linkerd.io/)
- [Mattermost-server](https://github.com/mattermost/mattermost-server) - [Mattermost-server](https://github.com/mattermost/mattermost-server)
- [Mercure](https://mercure.rocks/) - [Mercure](https://mercure.rocks/)
@ -33,6 +37,7 @@
- [Multi-gitter](https://github.com/lindell/multi-gitter) - [Multi-gitter](https://github.com/lindell/multi-gitter)
- [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack) - [Nanobox](https://github.com/nanobox-io/nanobox)/[Nanopack](https://github.com/nanopack)
- [nFPM](https://nfpm.goreleaser.com) - [nFPM](https://nfpm.goreleaser.com)
- [Okteto](https://github.com/okteto/okteto)
- [OpenShift](https://www.openshift.com/) - [OpenShift](https://www.openshift.com/)
- [Ory Hydra](https://github.com/ory/hydra) - [Ory Hydra](https://github.com/ory/hydra)
- [Ory Kratos](https://github.com/ory/kratos) - [Ory Kratos](https://github.com/ory/kratos)
@ -52,3 +57,4 @@
- [UpCloud CLI (`upctl`)](https://github.com/UpCloudLtd/upcloud-cli) - [UpCloud CLI (`upctl`)](https://github.com/UpCloudLtd/upcloud-cli)
- VMware's [Tanzu Community Edition](https://github.com/vmware-tanzu/community-edition) & [Tanzu Framework](https://github.com/vmware-tanzu/tanzu-framework) - VMware's [Tanzu Community Edition](https://github.com/vmware-tanzu/community-edition) & [Tanzu Framework](https://github.com/vmware-tanzu/tanzu-framework)
- [Werf](https://werf.io/) - [Werf](https://werf.io/)
- [ZITADEL](https://github.com/zitadel/zitadel)

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (

View File

@ -99,6 +99,11 @@ To tell Cobra *not* to provide the default `completion` command:
rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.CompletionOptions.DisableDefaultCmd = true
``` ```
To tell Cobra to mark the default `completion` command as *hidden*:
```
rootCmd.CompletionOptions.HiddenDefaultCmd = true
```
To tell Cobra *not* to provide the user with the `--no-descriptions` flag to the completion sub-commands: To tell Cobra *not* to provide the user with the `--no-descriptions` flag to the completion sub-commands:
``` ```
rootCmd.CompletionOptions.DisableNoDescFlag = true rootCmd.CompletionOptions.DisableNoDescFlag = true
@ -530,6 +535,21 @@ search for a keyword in charts
$ helm s[tab] $ helm s[tab]
search show status search show status
``` ```
### Aliases
You can also configure `powershell` aliases for your program and they will also support completions.
```
$ sal aliasname origcommand
$ Register-ArgumentCompleter -CommandName 'aliasname' -ScriptBlock $__origcommandCompleterBlock
# and now when you run `aliasname` completion will make
# suggestions as it did for `origcommand`.
$ aliasname <tab>
completion firstcommand secondcommand
```
The name of the completer block variable is of the form `$__<programName>CompleterBlock` where every `-` and `:` in the program name have been replaced with `_`, to respect powershell naming syntax.
### Limitations ### Limitations

View File

@ -302,15 +302,15 @@ rootCmd.MarkPersistentFlagRequired("region")
### Flag Groups ### Flag Groups
If you have different flags that must be provided together (e.g. if they provide the `--username` flag they MUST provide the `--password` flag as well) then If you have different flags that must be provided together (e.g. if they provide the `--username` flag they MUST provide the `--password` flag as well) then
Cobra can enforce that requirement: Cobra can enforce that requirement:
```go ```go
rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)") rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")
rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)") rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")
rootCmd.MarkFlagsRequiredTogether("username", "password") rootCmd.MarkFlagsRequiredTogether("username", "password")
``` ```
You can also prevent different flags from being provided together if they represent mutually You can also prevent different flags from being provided together if they represent mutually
exclusive options such as specifying an output format as either `--json` or `--yaml` but never both: exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:
```go ```go
rootCmd.Flags().BoolVar(&u, "json", false, "Output in JSON") rootCmd.Flags().BoolVar(&u, "json", false, "Output in JSON")
@ -327,29 +327,47 @@ In both of these cases:
## Positional and Custom Arguments ## Positional and Custom Arguments
Validation of positional arguments can be specified using the `Args` field of `Command`. Validation of positional arguments can be specified using the `Args` field of `Command`.
If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`.
The following validators are built in: The following validators are built in:
- `NoArgs` - the command will report an error if there are any positional args. - Number of arguments:
- `ArbitraryArgs` - the command will accept any args. - `NoArgs` - report an error if there are any positional args.
- `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`. - `ArbitraryArgs` - accept any number of args.
- `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args. - `MinimumNArgs(int)` - report an error if less than N positional args are provided.
- `MaximumNArgs(int)` - the command will report an error if there are more than N positional args. - `MaximumNArgs(int)` - report an error if more than N positional args are provided.
- `ExactArgs(int)` - the command will report an error if there are not exactly N positional args. - `ExactArgs(int)` - report an error if there are not exactly N positional args.
- `ExactValidArgs(int)` - the command will report an error if there are not exactly N positional args OR if there are any positional args that are not in the `ValidArgs` field of `Command` - `RangeArgs(min, max)` - report an error if the number of args is not between `min` and `max`.
- `RangeArgs(min, max)` - the command will report an error if the number of args is not between the minimum and maximum number of expected args. - Content of the arguments:
- `MatchAll(pargs ...PositionalArgs)` - enables combining existing checks with arbitrary other checks (e.g. you want to check the ExactArgs length along with other qualities). - `OnlyValidArgs` - report an error if there are any positional args not specified in the `ValidArgs` field of `Command`, which can optionally be set to a list of valid values for positional args.
An example of setting the custom validator: If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`.
Moreover, `MatchAll(pargs ...PositionalArgs)` enables combining existing checks with arbitrary other checks.
For instance, if you want to report an error if there are not exactly N positional args OR if there are any positional
args that are not in the `ValidArgs` field of `Command`, you can call `MatchAll` on `ExactArgs` and `OnlyValidArgs`, as
shown below:
```go
var cmd = &cobra.Command{
Short: "hello",
Args: MatchAll(ExactArgs(2), OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, World!")
},
}
```
It is possible to set any custom validator that satisfies `func(cmd *cobra.Command, args []string) error`.
For example:
```go ```go
var cmd = &cobra.Command{ var cmd = &cobra.Command{
Short: "hello", Short: "hello",
Args: func(cmd *cobra.Command, args []string) error { Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 { // Optionally run one of the validators provided by cobra
return errors.New("requires a color argument") if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
} }
// Run the custom validation logic
if myapp.IsValidColor(args[0]) { if myapp.IsValidColor(args[0]) {
return nil return nil
} }
@ -444,37 +462,45 @@ create' is called. Every command will automatically have the '--help' flag adde
The following output is automatically generated by Cobra. Nothing beyond the The following output is automatically generated by Cobra. Nothing beyond the
command and flag definitions are needed. command and flag definitions are needed.
$ cobra help $ cobra-cli help
Cobra is a CLI library for Go that empowers applications. Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files This application is a tool to generate the needed files
to quickly create a Cobra application. to quickly create a Cobra application.
Usage: Usage:
cobra [command] cobra-cli [command]
Available Commands: Available Commands:
add Add a command to a Cobra Application add Add a command to a Cobra Application
completion Generate the autocompletion script for the specified shell
help Help about any command help Help about any command
init Initialize a Cobra Application init Initialize a Cobra Application
Flags: Flags:
-a, --author string author name for copyright attribution (default "YOUR NAME") -a, --author string author name for copyright attribution (default "YOUR NAME")
--config string config file (default is $HOME/.cobra.yaml) --config string config file (default is $HOME/.cobra.yaml)
-h, --help help for cobra -h, --help help for cobra-cli
-l, --license string name of license for the project -l, --license string name of license for the project
--viper use Viper for configuration (default true) --viper use Viper for configuration
Use "cobra [command] --help" for more information about a command. Use "cobra-cli [command] --help" for more information about a command.
Help is just a command like any other. There is no special logic or behavior Help is just a command like any other. There is no special logic or behavior
around it. In fact, you can provide your own if you want. around it. In fact, you can provide your own if you want.
### Grouping commands in help
Cobra supports grouping of available commands. Groups must be explicitly defined by `AddGroup` and set by
the `GroupId` element of a subcommand. The groups will appear in the same order as they are defined.
If you use the generated `help` or `completion` commands, you can set the group ids by `SetHelpCommandGroupId`
and `SetCompletionCommandGroupId`, respectively.
### Defining your own help ### Defining your own help
You can provide your own Help command or your own template for the default command to use You can provide your own Help command or your own template for the default command to use
with following functions: with the following functions:
```go ```go
cmd.SetHelpCommand(cmd *Command) cmd.SetHelpCommand(cmd *Command)
@ -493,22 +519,23 @@ showing the user the 'usage'.
You may recognize this from the help above. That's because the default help You may recognize this from the help above. That's because the default help
embeds the usage as part of its output. embeds the usage as part of its output.
$ cobra --invalid $ cobra-cli --invalid
Error: unknown flag: --invalid Error: unknown flag: --invalid
Usage: Usage:
cobra [command] cobra-cli [command]
Available Commands: Available Commands:
add Add a command to a Cobra Application add Add a command to a Cobra Application
completion Generate the autocompletion script for the specified shell
help Help about any command help Help about any command
init Initialize a Cobra Application init Initialize a Cobra Application
Flags: Flags:
-a, --author string author name for copyright attribution (default "YOUR NAME") -a, --author string author name for copyright attribution (default "YOUR NAME")
--config string config file (default is $HOME/.cobra.yaml) --config string config file (default is $HOME/.cobra.yaml)
-h, --help help for cobra -h, --help help for cobra-cli
-l, --license string name of license for the project -l, --license string name of license for the project
--viper use Viper for configuration (default true) --viper use Viper for configuration
Use "cobra [command] --help" for more information about a command. Use "cobra [command] --help" for more information about a command.
@ -627,7 +654,7 @@ Did you mean this?
Run 'hugo --help' for usage. Run 'hugo --help' for usage.
``` ```
Suggestions are automatic based on every subcommand registered and use an implementation of [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion. Suggestions are automatically generated based on existing subcommands and use an implementation of [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance). Every registered command that matches a minimum distance of 2 (ignoring case) will be displayed as a suggestion.
If you need to disable suggestions or tweak the string distance in your command, use: If you need to disable suggestions or tweak the string distance in your command, use:
@ -641,7 +668,8 @@ or
command.SuggestionsMinimumDistance = 1 command.SuggestionsMinimumDistance = 1
``` ```
You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but makes sense in your set of commands and for some which you don't want aliases. Example: You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but make sense in your set of commands but for which
you don't want aliases. Example:
``` ```
$ kubectl remove $ kubectl remove

View File

@ -1,3 +1,17 @@
// Copyright 2013-2022 The Cobra Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cobra package cobra
import ( import (

2
vendor/modules.txt vendored
View File

@ -7,7 +7,7 @@ github.com/h2non/filetype/types
# github.com/inconshreveable/mousetrap v1.0.1 # github.com/inconshreveable/mousetrap v1.0.1
## explicit; go 1.18 ## explicit; go 1.18
github.com/inconshreveable/mousetrap github.com/inconshreveable/mousetrap
# github.com/spf13/cobra v1.5.0 # github.com/spf13/cobra v1.6.0
## explicit; go 1.15 ## explicit; go 1.15
github.com/spf13/cobra github.com/spf13/cobra
# github.com/spf13/pflag v1.0.5 # github.com/spf13/pflag v1.0.5