Add captured alias declaration types
This commit is contained in:
111
command.go
111
command.go
@@ -3,6 +3,7 @@ package commandkit
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
|
||||
@@ -15,6 +16,45 @@ import (
|
||||
// declaration before changing an App registry.
|
||||
type Command struct{ record *commandRecord }
|
||||
|
||||
// Declaration is a captured command or application routing rule accepted by
|
||||
// New and App.Register. Only CommandKit declaration types implement it.
|
||||
type Declaration interface {
|
||||
commandkitDeclaration()
|
||||
}
|
||||
|
||||
func (Command) commandkitDeclaration() {}
|
||||
|
||||
// RoutingRule is an opaque, reusable command or flag alias declaration.
|
||||
type RoutingRule struct{ record *routingRuleRecord }
|
||||
|
||||
func (RoutingRule) commandkitDeclaration() {}
|
||||
|
||||
type routingRuleKind uint8
|
||||
|
||||
const (
|
||||
commandAliasRule routingRuleKind = iota + 1
|
||||
globalFlagAliasRule
|
||||
scopedFlagAliasRule
|
||||
)
|
||||
|
||||
type capturedFlagSpelling struct {
|
||||
name string
|
||||
short bool
|
||||
}
|
||||
|
||||
type routingRuleRecord struct {
|
||||
kind routingRuleKind
|
||||
sourcePath commandpath.Path
|
||||
targetPath commandpath.Path
|
||||
scopePath commandpath.Path
|
||||
sourceFlag capturedFlagSpelling
|
||||
targetFlag capturedFlagSpelling
|
||||
sourceText string
|
||||
targetText string
|
||||
scopeText string
|
||||
issues []error
|
||||
}
|
||||
|
||||
type commandRecord struct {
|
||||
name string
|
||||
summary string
|
||||
@@ -259,6 +299,77 @@ func WithError(command Command, err error) Command {
|
||||
// not an absence marker.
|
||||
func NoCommand() Command { return Command{record: &commandRecord{absent: true}} }
|
||||
|
||||
// Alias captures a command-path alias from path to target. Paths are
|
||||
// whitespace-separated sequences of command names; construction issues are
|
||||
// retained for New or App.Register to report.
|
||||
func Alias(path, target string) RoutingRule {
|
||||
record := &routingRuleRecord{
|
||||
kind: commandAliasRule,
|
||||
sourceText: path,
|
||||
targetText: target,
|
||||
}
|
||||
record.sourcePath = captureRoutingPath("source", path, &record.issues)
|
||||
record.targetPath = captureRoutingPath("target", target, &record.issues)
|
||||
return RoutingRule{record: record}
|
||||
}
|
||||
|
||||
// FlagAlias captures an application-wide flag spelling alias. Spellings must
|
||||
// be one long flag name or one individual short spelling.
|
||||
func FlagAlias(spelling, target string) RoutingRule {
|
||||
record := &routingRuleRecord{
|
||||
kind: globalFlagAliasRule,
|
||||
sourceText: spelling,
|
||||
targetText: target,
|
||||
}
|
||||
record.sourceFlag = captureFlagSpelling("source", spelling, &record.issues)
|
||||
record.targetFlag = captureFlagSpelling("target", target, &record.issues)
|
||||
return RoutingRule{record: record}
|
||||
}
|
||||
|
||||
// FlagAliasFor captures a flag spelling alias scoped to a canonical command
|
||||
// path.
|
||||
func FlagAliasFor(commandPath, spelling, target string) RoutingRule {
|
||||
record := &routingRuleRecord{
|
||||
kind: scopedFlagAliasRule,
|
||||
sourceText: spelling,
|
||||
targetText: target,
|
||||
scopeText: commandPath,
|
||||
}
|
||||
record.scopePath = captureRoutingPath("command scope", commandPath, &record.issues)
|
||||
record.sourceFlag = captureFlagSpelling("source", spelling, &record.issues)
|
||||
record.targetFlag = captureFlagSpelling("target", target, &record.issues)
|
||||
return RoutingRule{record: record}
|
||||
}
|
||||
|
||||
func captureRoutingPath(label, value string, issues *[]error) commandpath.Path {
|
||||
path, err := commandpath.ParseDeclaration(value)
|
||||
if err != nil {
|
||||
*issues = append(*issues, fmt.Errorf("invalid %s path: %w", label, err))
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func captureFlagSpelling(label, value string, issues *[]error) capturedFlagSpelling {
|
||||
spelling, err := parseFlagSpelling(value)
|
||||
if err != nil {
|
||||
*issues = append(*issues, fmt.Errorf("invalid %s flag spelling %q: %w", label, value, err))
|
||||
}
|
||||
return spelling
|
||||
}
|
||||
|
||||
func parseFlagSpelling(value string) (capturedFlagSpelling, error) {
|
||||
if strings.HasPrefix(value, "--") {
|
||||
name := value[2:]
|
||||
if validFlagName(name) {
|
||||
return capturedFlagSpelling{name: name}, nil
|
||||
}
|
||||
}
|
||||
if len(value) == 2 && value[0] == '-' && value[1] != '-' {
|
||||
return capturedFlagSpelling{name: value[1:], short: true}, nil
|
||||
}
|
||||
return capturedFlagSpelling{}, fmt.Errorf("must be a long flag or one-byte short flag spelling")
|
||||
}
|
||||
|
||||
func captureComposition(record *commandRecord) {
|
||||
if err := validateCommandName(record.name); err != nil {
|
||||
record.issues = append(record.issues, err)
|
||||
|
||||
Reference in New Issue
Block a user