Resolve flag aliases in failed command routes
This commit is contained in:
@@ -19,10 +19,11 @@ import (
|
||||
|
||||
// Session owns one mutable backend tree for a single command invocation.
|
||||
type Session struct {
|
||||
metadata *commandmeta.Command
|
||||
root *spfcobra.Command
|
||||
declarations map[*spfcobra.Command]*commandmeta.Command
|
||||
flagAliases []commandmeta.FlagAlias
|
||||
metadata *commandmeta.Command
|
||||
root *spfcobra.Command
|
||||
declarations map[*spfcobra.Command]*commandmeta.Command
|
||||
flagAliases []commandmeta.FlagAlias
|
||||
flagAliasesForPath func([]string) []commandmeta.FlagAlias
|
||||
}
|
||||
|
||||
// Prepare constructs a fresh backend tree for one invocation.
|
||||
@@ -33,6 +34,7 @@ func Prepare(root *commandmeta.Command, options commandmeta.ParseOptions) *Sessi
|
||||
}
|
||||
session.metadata = root
|
||||
session.flagAliases = append([]commandmeta.FlagAlias(nil), options.FlagAliases...)
|
||||
session.flagAliasesForPath = options.FlagAliasesForPath
|
||||
options = normalizeOptions(options)
|
||||
session.root, session.declarations = buildTree(root, options)
|
||||
return session
|
||||
@@ -80,7 +82,11 @@ func (session *Session) Parse(argv []string) commandmeta.Result {
|
||||
localFlags := make([]commandmeta.Flag, 0, len(selectedNode.Flags)+len(selectedNode.CoreFlags))
|
||||
localFlags = append(localFlags, selectedNode.Flags...)
|
||||
localFlags = append(localFlags, selectedNode.CoreFlags...)
|
||||
interpreted, spellings, occurrences, interpretationFailure := interpretFlagTokens(remaining, localFlags, session.flagAliases)
|
||||
aliases := session.flagAliases
|
||||
if session.flagAliasesForPath != nil {
|
||||
aliases = session.flagAliasesForPath(result.Path)
|
||||
}
|
||||
interpreted, spellings, occurrences, interpretationFailure := interpretFlagTokens(remaining, localFlags, aliases)
|
||||
for _, flag := range localFlags {
|
||||
if value := selected.Flags().Lookup(flag.Name); value != nil {
|
||||
value.Value.(*rawValue).queueSpellings(spellings[flag.Name])
|
||||
|
||||
@@ -130,9 +130,12 @@ type Result struct {
|
||||
|
||||
// ParseOptions makes parser context and streams explicit.
|
||||
type ParseOptions struct {
|
||||
Context context.Context
|
||||
In io.Reader
|
||||
Out io.Writer
|
||||
Err io.Writer
|
||||
FlagAliases []FlagAlias
|
||||
// FlagAliasesForPath selects aliases for the actual parser context when
|
||||
// routing has not already selected one. It overrides FlagAliases when set.
|
||||
FlagAliasesForPath func([]string) []FlagAlias
|
||||
Context context.Context
|
||||
In io.Reader
|
||||
Out io.Writer
|
||||
Err io.Writer
|
||||
FlagAliases []FlagAlias
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user