podman
98 строк · 4.2 Кб
1// Copyright 2013-2023 The Cobra Authors
2//
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 at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// 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 and
13// limitations under the License.
14
15package cobra
16
17import (
18"github.com/spf13/pflag"
19)
20
21// MarkFlagRequired instructs the various shell completion implementations to
22// 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 {
25return MarkFlagRequired(c.Flags(), name)
26}
27
28// MarkPersistentFlagRequired instructs the various shell completion implementations to
29// 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 {
32return MarkFlagRequired(c.PersistentFlags(), name)
33}
34
35// MarkFlagRequired instructs the various shell completion implementations to
36// 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 {
39return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
40}
41
42// MarkFlagFilename instructs the various shell completion implementations to
43// limit completions for the named flag to the specified file extensions.
44func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
45return MarkFlagFilename(c.Flags(), name, extensions...)
46}
47
48// 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 allows
53// to register a Go function which will work across all shells.
54func (c *Command) MarkFlagCustom(name string, f string) error {
55return MarkFlagCustom(c.Flags(), name, f)
56}
57
58// MarkPersistentFlagFilename instructs the various shell completion
59// implementations to limit completions for the named persistent flag to the
60// specified file extensions.
61func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
62return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
63}
64
65// MarkFlagFilename instructs the various shell completion implementations to
66// limit completions for the named flag to the specified file extensions.
67func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
68return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
69}
70
71// 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 allows
76// to register a Go function which will work across all shells.
77func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
78return flags.SetAnnotation(name, BashCompCustom, []string{f})
79}
80
81// MarkFlagDirname instructs the various shell completion implementations to
82// limit completions for the named flag to directory names.
83func (c *Command) MarkFlagDirname(name string) error {
84return MarkFlagDirname(c.Flags(), name)
85}
86
87// MarkPersistentFlagDirname instructs the various shell completion
88// implementations to limit completions for the named persistent flag to
89// directory names.
90func (c *Command) MarkPersistentFlagDirname(name string) error {
91return MarkFlagDirname(c.PersistentFlags(), name)
92}
93
94// MarkFlagDirname instructs the various shell completion implementations to
95// limit completions for the named flag to directory names.
96func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
97return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})
98}
99