2016-06-15 13:54:07 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-10-25 14:25:08 +00:00
|
|
|
"flag"
|
2016-06-15 13:54:07 +00:00
|
|
|
"fmt"
|
2022-11-04 13:57:20 +00:00
|
|
|
"reflect"
|
2016-06-15 13:54:07 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command is a subcommand for a cli.App.
|
|
|
|
type Command struct {
|
|
|
|
// The name of the command
|
|
|
|
Name string
|
|
|
|
// A list of aliases for the command
|
|
|
|
Aliases []string
|
|
|
|
// A short description of the usage of this command
|
|
|
|
Usage string
|
|
|
|
// Custom text to show on USAGE section of help
|
|
|
|
UsageText string
|
|
|
|
// A longer explanation of how the command works
|
|
|
|
Description string
|
2024-05-15 23:15:00 +00:00
|
|
|
// Whether this command supports arguments
|
|
|
|
Args bool
|
2016-06-15 13:54:07 +00:00
|
|
|
// A short description of the arguments of this command
|
|
|
|
ArgsUsage string
|
|
|
|
// The category the command is part of
|
|
|
|
Category string
|
|
|
|
// The function to call when checking for bash command completions
|
|
|
|
BashComplete BashCompleteFunc
|
|
|
|
// An action to execute before any sub-subcommands are run, but after the context is ready
|
|
|
|
// If a non-nil error is returned, no sub-subcommands are run
|
|
|
|
Before BeforeFunc
|
|
|
|
// An action to execute after any subcommands are run, but after the subcommand has finished
|
|
|
|
// It is run even if Action() panics
|
|
|
|
After AfterFunc
|
|
|
|
// The function to call when this command is invoked
|
2022-10-25 14:25:08 +00:00
|
|
|
Action ActionFunc
|
2016-06-15 13:54:07 +00:00
|
|
|
// Execute this function if a usage error occurs.
|
|
|
|
OnUsageError OnUsageErrorFunc
|
|
|
|
// List of child commands
|
2022-10-25 14:25:08 +00:00
|
|
|
Subcommands []*Command
|
2016-06-15 13:54:07 +00:00
|
|
|
// List of flags to parse
|
2022-10-25 14:25:08 +00:00
|
|
|
Flags []Flag
|
|
|
|
flagCategories FlagCategories
|
2016-06-15 13:54:07 +00:00
|
|
|
// Treat all flags as normal arguments if true
|
|
|
|
SkipFlagParsing bool
|
2022-10-25 14:25:08 +00:00
|
|
|
// Boolean to hide built-in help command and help flag
|
2016-06-15 13:54:07 +00:00
|
|
|
HideHelp bool
|
2022-10-25 14:25:08 +00:00
|
|
|
// Boolean to hide built-in help command but keep help flag
|
|
|
|
// Ignored if HideHelp is true.
|
|
|
|
HideHelpCommand bool
|
2016-06-15 13:54:07 +00:00
|
|
|
// Boolean to hide this command from help or completion
|
|
|
|
Hidden bool
|
2022-10-25 14:25:08 +00:00
|
|
|
// Boolean to enable short-option handling so user can combine several
|
|
|
|
// single-character bool arguments into one
|
|
|
|
// i.e. foobar -o -v -> foobar -ov
|
|
|
|
UseShortOptionHandling bool
|
2016-06-15 13:54:07 +00:00
|
|
|
|
|
|
|
// Full name of command for help, defaults to full command name, including parent commands.
|
|
|
|
HelpName string
|
|
|
|
commandNamePath []string
|
2018-01-25 13:08:43 +00:00
|
|
|
|
|
|
|
// CustomHelpTemplate the text template for the command help topic.
|
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
CustomHelpTemplate string
|
2022-11-04 13:57:20 +00:00
|
|
|
|
|
|
|
// categories contains the categorized commands and is populated on app startup
|
|
|
|
categories CommandCategories
|
|
|
|
|
|
|
|
// if this is a root "special" command
|
|
|
|
isRoot bool
|
2023-02-22 21:58:17 +00:00
|
|
|
|
|
|
|
separator separatorSpec
|
2018-01-25 13:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
type Commands []*Command
|
|
|
|
|
|
|
|
type CommandsByName []*Command
|
2018-01-25 13:08:43 +00:00
|
|
|
|
|
|
|
func (c CommandsByName) Len() int {
|
|
|
|
return len(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandsByName) Less(i, j int) bool {
|
2022-10-25 14:25:08 +00:00
|
|
|
return lexicographicLess(c[i].Name, c[j].Name)
|
2018-01-25 13:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandsByName) Swap(i, j int) {
|
|
|
|
c[i], c[j] = c[j], c[i]
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullName returns the full name of the command.
|
|
|
|
// For subcommands this ensures that parent commands are part of the command path
|
2022-10-25 14:25:08 +00:00
|
|
|
func (c *Command) FullName() string {
|
2016-06-15 13:54:07 +00:00
|
|
|
if c.commandNamePath == nil {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
return strings.Join(c.commandNamePath, " ")
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
func (cmd *Command) Command(name string) *Command {
|
|
|
|
for _, c := range cmd.Subcommands {
|
|
|
|
if c.HasName(name) {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) setup(ctx *Context) {
|
|
|
|
if c.Command(helpCommand.Name) == nil && !c.HideHelp {
|
|
|
|
if !c.HideHelpCommand {
|
|
|
|
c.Subcommands = append(c.Subcommands, helpCommand)
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
if !c.HideHelp && HelpFlag != nil {
|
2016-06-15 13:54:07 +00:00
|
|
|
// append help to flags
|
2022-10-25 14:25:08 +00:00
|
|
|
c.appendFlag(HelpFlag)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
if ctx.App.UseShortOptionHandling {
|
|
|
|
c.UseShortOptionHandling = true
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
c.categories = newCommandCategories()
|
|
|
|
for _, command := range c.Subcommands {
|
|
|
|
c.categories.AddCommand(command.Category, command)
|
|
|
|
}
|
|
|
|
sort.Sort(c.categories.(*commandCategories))
|
2016-06-15 13:54:07 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
for _, scmd := range c.Subcommands {
|
|
|
|
if scmd.HelpName == "" {
|
|
|
|
scmd.HelpName = fmt.Sprintf("%s %s", c.HelpName, scmd.Name)
|
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
scmd.separator = c.separator
|
2022-11-04 13:57:20 +00:00
|
|
|
}
|
2023-01-16 13:00:07 +00:00
|
|
|
|
|
|
|
if c.BashComplete == nil {
|
|
|
|
c.BashComplete = DefaultCompleteWithFlags(c)
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {
|
|
|
|
|
|
|
|
if !c.isRoot {
|
|
|
|
c.setup(cCtx)
|
2024-05-15 23:15:00 +00:00
|
|
|
if err := checkDuplicatedCmds(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a := args(arguments)
|
|
|
|
set, err := c.parseFlags(&a, cCtx.shellComplete)
|
|
|
|
cCtx.flagSet = set
|
|
|
|
|
2023-01-16 13:00:07 +00:00
|
|
|
if checkCompletions(cCtx) {
|
2016-06-15 13:54:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-25 13:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
if c.OnUsageError != nil {
|
2022-11-04 13:57:20 +00:00
|
|
|
err = c.OnUsageError(cCtx, err, !c.isRoot)
|
2022-10-25 14:25:08 +00:00
|
|
|
cCtx.App.handleExitCoder(cCtx, err)
|
2018-01-25 13:08:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
_, _ = fmt.Fprintf(cCtx.App.Writer, "%s %s\n\n", "Incorrect Usage:", err.Error())
|
|
|
|
if cCtx.App.Suggest {
|
|
|
|
if suggestion, err := c.suggestFlagFromError(err, ""); err == nil {
|
|
|
|
fmt.Fprintf(cCtx.App.Writer, "%s", suggestion)
|
2022-10-25 14:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !c.HideHelp {
|
2022-11-04 13:57:20 +00:00
|
|
|
if c.isRoot {
|
|
|
|
_ = ShowAppHelp(cCtx)
|
|
|
|
} else {
|
|
|
|
_ = ShowCommandHelp(cCtx.parentContext, c.Name)
|
|
|
|
}
|
2022-10-25 14:25:08 +00:00
|
|
|
}
|
2018-01-25 13:08:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
if checkHelp(cCtx) {
|
|
|
|
return helpCommand.Action(cCtx)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
if c.isRoot && !cCtx.App.HideVersion && checkVersion(cCtx) {
|
|
|
|
ShowVersion(cCtx)
|
|
|
|
return nil
|
2022-10-25 14:25:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
if c.After != nil && !cCtx.shellComplete {
|
2016-06-15 13:54:07 +00:00
|
|
|
defer func() {
|
2022-10-25 14:25:08 +00:00
|
|
|
afterErr := c.After(cCtx)
|
2016-06-15 13:54:07 +00:00
|
|
|
if afterErr != nil {
|
2022-10-25 14:25:08 +00:00
|
|
|
cCtx.App.handleExitCoder(cCtx, err)
|
2016-06-15 13:54:07 +00:00
|
|
|
if err != nil {
|
2022-10-25 14:25:08 +00:00
|
|
|
err = newMultiError(err, afterErr)
|
2016-06-15 13:54:07 +00:00
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
cerr := cCtx.checkRequiredFlags(c.Flags)
|
|
|
|
if cerr != nil {
|
2023-01-16 13:00:07 +00:00
|
|
|
_ = helpCommand.Action(cCtx)
|
2022-11-04 13:57:20 +00:00
|
|
|
return cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Before != nil && !cCtx.shellComplete {
|
|
|
|
beforeErr := c.Before(cCtx)
|
|
|
|
if beforeErr != nil {
|
|
|
|
cCtx.App.handleExitCoder(cCtx, beforeErr)
|
|
|
|
err = beforeErr
|
2016-06-15 13:54:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
if err = runFlagActions(cCtx, c.Flags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
var cmd *Command
|
|
|
|
args := cCtx.Args()
|
|
|
|
if args.Present() {
|
|
|
|
name := args.First()
|
|
|
|
cmd = c.Command(name)
|
|
|
|
if cmd == nil {
|
|
|
|
hasDefault := cCtx.App.DefaultCommand != ""
|
|
|
|
isFlagName := checkStringSliceIncludes(name, cCtx.FlagNames())
|
|
|
|
|
|
|
|
var (
|
|
|
|
isDefaultSubcommand = false
|
|
|
|
defaultHasSubcommands = false
|
|
|
|
)
|
|
|
|
|
|
|
|
if hasDefault {
|
|
|
|
dc := cCtx.App.Command(cCtx.App.DefaultCommand)
|
|
|
|
defaultHasSubcommands = len(dc.Subcommands) > 0
|
|
|
|
for _, dcSub := range dc.Subcommands {
|
|
|
|
if checkStringSliceIncludes(name, dcSub.Names()) {
|
|
|
|
isDefaultSubcommand = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isFlagName || (hasDefault && (defaultHasSubcommands && isDefaultSubcommand)) {
|
|
|
|
argsWithDefault := cCtx.App.argsWithDefaultCommand(args)
|
|
|
|
if !reflect.DeepEqual(args, argsWithDefault) {
|
|
|
|
cmd = cCtx.App.rootCommand.Command(argsWithDefault.First())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 13:00:07 +00:00
|
|
|
} else if c.isRoot && cCtx.App.DefaultCommand != "" {
|
2022-11-04 13:57:20 +00:00
|
|
|
if dc := cCtx.App.Command(cCtx.App.DefaultCommand); dc != c {
|
|
|
|
cmd = dc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd != nil {
|
|
|
|
newcCtx := NewContext(cCtx.App, nil, cCtx)
|
|
|
|
newcCtx.Command = cmd
|
|
|
|
return cmd.Run(newcCtx, cCtx.Args().Slice()...)
|
|
|
|
}
|
|
|
|
|
2018-01-25 13:08:43 +00:00
|
|
|
if c.Action == nil {
|
2022-10-25 14:25:08 +00:00
|
|
|
c.Action = helpCommand.Action
|
2018-01-25 13:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
err = c.Action(cCtx)
|
2016-06-15 13:54:07 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
cCtx.App.handleExitCoder(cCtx, err)
|
2016-06-15 13:54:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
func (c *Command) newFlagSet() (*flag.FlagSet, error) {
|
2023-02-22 21:58:17 +00:00
|
|
|
return flagSet(c.Name, c.Flags, c.separator)
|
2022-10-25 14:25:08 +00:00
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
func (c *Command) useShortOptionHandling() bool {
|
|
|
|
return c.UseShortOptionHandling
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
func (c *Command) suggestFlagFromError(err error, command string) (string, error) {
|
|
|
|
flag, parseErr := flagFromError(err)
|
|
|
|
if parseErr != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := c.Flags
|
|
|
|
hideHelp := c.HideHelp
|
|
|
|
if command != "" {
|
|
|
|
cmd := c.Command(command)
|
|
|
|
if cmd == nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
flags = cmd.Flags
|
|
|
|
hideHelp = hideHelp || cmd.HideHelp
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestion := SuggestFlag(flags, flag, hideHelp)
|
|
|
|
if len(suggestion) == 0 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(SuggestDidYouMeanTemplate, suggestion) + "\n\n", nil
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
func (c *Command) parseFlags(args Args, shellComplete bool) (*flag.FlagSet, error) {
|
|
|
|
set, err := c.newFlagSet()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
if c.SkipFlagParsing {
|
|
|
|
return set, set.Parse(append([]string{"--"}, args.Tail()...))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = parseIter(set, c, args.Tail(), shellComplete)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = normalizeFlags(c.Flags, set)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return set, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Names returns the names including short names and aliases.
|
|
|
|
func (c *Command) Names() []string {
|
|
|
|
return append([]string{c.Name}, c.Aliases...)
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
// HasName returns true if Command.Name matches given name
|
|
|
|
func (c *Command) HasName(name string) bool {
|
2016-06-15 13:54:07 +00:00
|
|
|
for _, n := range c.Names() {
|
|
|
|
if n == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
// VisibleCategories returns a slice of categories and commands that are
|
|
|
|
// Hidden=false
|
|
|
|
func (c *Command) VisibleCategories() []CommandCategory {
|
|
|
|
ret := []CommandCategory{}
|
|
|
|
for _, category := range c.categories.Categories() {
|
|
|
|
if visible := func() CommandCategory {
|
|
|
|
if len(category.VisibleCommands()) > 0 {
|
|
|
|
return category
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}(); visible != nil {
|
|
|
|
ret = append(ret, visible)
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
return ret
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
|
|
|
func (c *Command) VisibleCommands() []*Command {
|
|
|
|
var ret []*Command
|
2016-06-15 13:54:07 +00:00
|
|
|
for _, command := range c.Subcommands {
|
2022-11-04 13:57:20 +00:00
|
|
|
if !command.Hidden {
|
|
|
|
ret = append(ret, command)
|
|
|
|
}
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
return ret
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 14:25:08 +00:00
|
|
|
// VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain
|
|
|
|
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
|
|
|
|
if c.flagCategories == nil {
|
2022-11-04 13:57:20 +00:00
|
|
|
c.flagCategories = newFlagCategoriesFromFlags(c.Flags)
|
2022-10-25 14:25:08 +00:00
|
|
|
}
|
|
|
|
return c.flagCategories.VisibleCategories()
|
|
|
|
}
|
|
|
|
|
2016-06-15 13:54:07 +00:00
|
|
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
2022-10-25 14:25:08 +00:00
|
|
|
func (c *Command) VisibleFlags() []Flag {
|
2016-06-15 13:54:07 +00:00
|
|
|
return visibleFlags(c.Flags)
|
|
|
|
}
|
2022-10-25 14:25:08 +00:00
|
|
|
|
|
|
|
func (c *Command) appendFlag(fl Flag) {
|
|
|
|
if !hasFlag(c.Flags, fl) {
|
|
|
|
c.Flags = append(c.Flags, fl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasCommand(commands []*Command, command *Command) bool {
|
|
|
|
for _, existing := range commands {
|
|
|
|
if command == existing {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
|
|
|
|
func checkDuplicatedCmds(parent *Command) error {
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, c := range parent.Subcommands {
|
|
|
|
for _, name := range c.Names() {
|
|
|
|
if _, exists := seen[name]; exists {
|
|
|
|
return fmt.Errorf("parent command [%s] has duplicated subcommand name or alias: %s", parent.Name, name)
|
|
|
|
}
|
|
|
|
seen[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|