Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ func (a *App) addCommand(cmd *Command, addHelpFlag bool) {

// RunCommand runs a single command.
func (a *App) RunCommand(args []string) error {
if a.config.CommandPreProc != nil {
var err error

args, err = a.config.CommandPreProc(args)
if err != nil {
return fmt.Errorf("command preprocessor failed - %w", err)
}
}

// Parse the arguments string and obtain the command path to the root,
// and the command flags.
cmds, fg, args, err := a.commands.parse(args, a.flagMap, false)
Expand Down
17 changes: 16 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,23 @@ type Config struct {
HelpSubCommands bool
HelpHeadlineColor *color.Color

// Override default iterrupt handler
// Override default iterrupt handler.
InterruptHandler func(a *App, count int)

// CommandPreProc is an optional function that allows callers
// to modify the command string and its arguments prior to
// looking up the command code and executing it. This field
// is useful for implementing variable replacement using
// functionality like os.Expand (ps: "PreProc" here means
// "preprocessor").
//
// This function receives the list of strings typed by the user
// and returns the new list of strings to use in the RunCommand
// method. An optional error can be returned as well which will
// stop the command from being executed.
//
// This field is ignored if it is nil.
CommandPreProc func(args []string) ([]string, error)
}

// SetDefaults sets the default values if not set.
Expand Down