Path: blob/main/vendor/github.com/spf13/cobra/shell_completions.go
2875 views
// Copyright 2013-2023 The Cobra Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package cobra1516import (17"github.com/spf13/pflag"18)1920// MarkFlagRequired instructs the various shell completion implementations to21// prioritize the named flag when performing completion,22// and causes your command to report an error if invoked without the flag.23func (c *Command) MarkFlagRequired(name string) error {24return MarkFlagRequired(c.Flags(), name)25}2627// MarkPersistentFlagRequired instructs the various shell completion implementations to28// prioritize the named persistent flag when performing completion,29// and causes your command to report an error if invoked without the flag.30func (c *Command) MarkPersistentFlagRequired(name string) error {31return MarkFlagRequired(c.PersistentFlags(), name)32}3334// MarkFlagRequired instructs the various shell completion implementations to35// prioritize the named flag when performing completion,36// and causes your command to report an error if invoked without the flag.37func MarkFlagRequired(flags *pflag.FlagSet, name string) error {38return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})39}4041// MarkFlagFilename instructs the various shell completion implementations to42// limit completions for the named flag to the specified file extensions.43func (c *Command) MarkFlagFilename(name string, extensions ...string) error {44return MarkFlagFilename(c.Flags(), name, extensions...)45}4647// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.48// The bash completion script will call the bash function f for the flag.49//50// This will only work for bash completion.51// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows52// to register a Go function which will work across all shells.53func (c *Command) MarkFlagCustom(name string, f string) error {54return MarkFlagCustom(c.Flags(), name, f)55}5657// MarkPersistentFlagFilename instructs the various shell completion58// implementations to limit completions for the named persistent flag to the59// specified file extensions.60func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {61return MarkFlagFilename(c.PersistentFlags(), name, extensions...)62}6364// MarkFlagFilename instructs the various shell completion implementations to65// limit completions for the named flag to the specified file extensions.66func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {67return flags.SetAnnotation(name, BashCompFilenameExt, extensions)68}6970// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.71// The bash completion script will call the bash function f for the flag.72//73// This will only work for bash completion.74// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows75// to register a Go function which will work across all shells.76func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {77return flags.SetAnnotation(name, BashCompCustom, []string{f})78}7980// MarkFlagDirname instructs the various shell completion implementations to81// limit completions for the named flag to directory names.82func (c *Command) MarkFlagDirname(name string) error {83return MarkFlagDirname(c.Flags(), name)84}8586// MarkPersistentFlagDirname instructs the various shell completion87// implementations to limit completions for the named persistent flag to88// directory names.89func (c *Command) MarkPersistentFlagDirname(name string) error {90return MarkFlagDirname(c.PersistentFlags(), name)91}9293// MarkFlagDirname instructs the various shell completion implementations to94// limit completions for the named flag to directory names.95func MarkFlagDirname(flags *pflag.FlagSet, name string) error {96return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})97}9899100