vendor/github.com/spf13/cobra/shell_completions.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 04 Feb 2023 12:58:35 +0100
changeset 265 05c40b36d3b2
parent 256 6d9efbef00a9
permissions -rw-r--r--
Bump to version 3.0.0-dev, using madon v3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
265
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     1
// Copyright 2013-2022 The Cobra Authors
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     2
//
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     3
// Licensed under the Apache License, Version 2.0 (the "License");
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     4
// you may not use this file except in compliance with the License.
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     5
// You may obtain a copy of the License at
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     6
//
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     7
//      http://www.apache.org/licenses/LICENSE-2.0
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     8
//
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
     9
// Unless required by applicable law or agreed to in writing, software
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
    10
// distributed under the License is distributed on an "AS IS" BASIS,
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
    11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
    12
// See the License for the specific language governing permissions and
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
    13
// limitations under the License.
05c40b36d3b2 Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents: 256
diff changeset
    14
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
package cobra
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
import (
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	"github.com/spf13/pflag"
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    21
// MarkFlagRequired instructs the various shell completion implementations to
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    22
// prioritize the named flag when performing completion,
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
// and causes your command to report an error if invoked without the flag.
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
func (c *Command) MarkFlagRequired(name string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	return MarkFlagRequired(c.Flags(), name)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    28
// MarkPersistentFlagRequired instructs the various shell completion implementations to
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    29
// prioritize the named persistent flag when performing completion,
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
// and causes your command to report an error if invoked without the flag.
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
func (c *Command) MarkPersistentFlagRequired(name string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	return MarkFlagRequired(c.PersistentFlags(), name)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    35
// MarkFlagRequired instructs the various shell completion implementations to
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    36
// prioritize the named flag when performing completion,
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
// and causes your command to report an error if invoked without the flag.
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    42
// MarkFlagFilename instructs the various shell completion implementations to
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    43
// limit completions for the named flag to the specified file extensions.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	return MarkFlagFilename(c.Flags(), name, extensions...)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    49
// The bash completion script will call the bash function f for the flag.
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    50
//
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    51
// This will only work for bash completion.
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    52
// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    53
// to register a Go function which will work across all shells.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
func (c *Command) MarkFlagCustom(name string, f string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
	return MarkFlagCustom(c.Flags(), name, f)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
// MarkPersistentFlagFilename instructs the various shell completion
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    59
// implementations to limit completions for the named persistent flag to the
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    60
// specified file extensions.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
// MarkFlagFilename instructs the various shell completion implementations to
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    66
// limit completions for the named flag to the specified file extensions.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
	return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    71
// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    72
// The bash completion script will call the bash function f for the flag.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
//
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    74
// This will only work for bash completion.
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    75
// It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    76
// to register a Go function which will work across all shells.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
	return flags.SetAnnotation(name, BashCompCustom, []string{f})
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
// MarkFlagDirname instructs the various shell completion implementations to
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    82
// limit completions for the named flag to directory names.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
func (c *Command) MarkFlagDirname(name string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
	return MarkFlagDirname(c.Flags(), name)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
// MarkPersistentFlagDirname instructs the various shell completion
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    88
// implementations to limit completions for the named persistent flag to
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    89
// directory names.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
func (c *Command) MarkPersistentFlagDirname(name string) error {
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	return MarkFlagDirname(c.PersistentFlags(), name)
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
}
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
// MarkFlagDirname instructs the various shell completion implementations to
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    95
// limit completions for the named flag to directory names.
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
256
6d9efbef00a9 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents: 251
diff changeset
    97
	return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})
251
1c52a0eeb952 Update dependencies
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
}