Skip to content
Myr

shell_completions.go

This file

Path

shell_completions.go

98 lines · 4.2 KiB · Go

Last changed by

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

History of this file

Take it

1// Copyright 2013-2023 The Cobra Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415package cobra1617import (18	"github.com/spf13/pflag"19)2021// MarkFlagRequired instructs the various shell completion implementations to22// prioritize the named flag when performing completion,23// and causes your command to report an error if invoked without the flag.24func (c *Command) MarkFlagRequired(name string) error {25	return MarkFlagRequired(c.Flags(), name)26}2728// MarkPersistentFlagRequired instructs the various shell completion implementations to29// prioritize the named persistent flag when performing completion,30// and causes your command to report an error if invoked without the flag.31func (c *Command) MarkPersistentFlagRequired(name string) error {32	return MarkFlagRequired(c.PersistentFlags(), name)33}3435// MarkFlagRequired instructs the various shell completion implementations to36// prioritize the named flag when performing completion,37// and causes your command to report an error if invoked without the flag.38func MarkFlagRequired(flags *pflag.FlagSet, name string) error {39	return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})40}4142// MarkFlagFilename instructs the various shell completion implementations to43// limit completions for the named flag to the specified file extensions.44func (c *Command) MarkFlagFilename(name string, extensions ...string) error {45	return MarkFlagFilename(c.Flags(), name, extensions...)46}4748// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.49// The bash completion script will call the bash function f for the flag.50//51// This will only work for bash completion.52// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows53// to register a Go function which will work across all shells.54func (c *Command) MarkFlagCustom(name string, f string) error {55	return MarkFlagCustom(c.Flags(), name, f)56}5758// MarkPersistentFlagFilename instructs the various shell completion59// implementations to limit completions for the named persistent flag to the60// specified file extensions.61func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {62	return MarkFlagFilename(c.PersistentFlags(), name, extensions...)63}6465// MarkFlagFilename instructs the various shell completion implementations to66// limit completions for the named flag to the specified file extensions.67func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {68	return flags.SetAnnotation(name, BashCompFilenameExt, extensions)69}7071// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.72// The bash completion script will call the bash function f for the flag.73//74// This will only work for bash completion.75// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows76// to register a Go function which will work across all shells.77func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {78	return flags.SetAnnotation(name, BashCompCustom, []string{f})79}8081// MarkFlagDirname instructs the various shell completion implementations to82// limit completions for the named flag to directory names.83func (c *Command) MarkFlagDirname(name string) error {84	return MarkFlagDirname(c.Flags(), name)85}8687// MarkPersistentFlagDirname instructs the various shell completion88// implementations to limit completions for the named persistent flag to89// directory names.90func (c *Command) MarkPersistentFlagDirname(name string) error {91	return MarkFlagDirname(c.PersistentFlags(), name)92}9394// MarkFlagDirname instructs the various shell completion implementations to95// limit completions for the named flag to directory names.96func MarkFlagDirname(flags *pflag.FlagSet, name string) error {97	return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})98}