Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

112 lines
2.5 KiB
Go

package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func executeCLIWithLegacyFlagCompat(args []string, stdout, stderr io.Writer) error {
cmd := newRootCommand()
cmd.SetOut(stdout)
cmd.SetErr(stderr)
normalized, rewrites := normalizeLegacyLongFlags(args, collectLongFlags(cmd))
if len(rewrites) > 0 {
fmt.Fprint(stderr, legacyLongFlagWarning(rewrites))
}
cmd.SetArgs(normalized)
return cmd.Execute()
}
func collectLongFlags(root *cobra.Command) map[string]struct{} {
flags := make(map[string]struct{})
var visit func(cmd *cobra.Command)
visit = func(cmd *cobra.Command) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if len(flag.Name) > 1 {
flags[flag.Name] = struct{}{}
}
})
cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
if len(flag.Name) > 1 {
flags[flag.Name] = struct{}{}
}
})
for _, child := range cmd.Commands() {
visit(child)
}
}
visit(root)
return flags
}
func normalizeLegacyLongFlags(args []string, flags map[string]struct{}) ([]string, []string) {
normalized := make([]string, 0, len(args))
rewrites := make([]string, 0)
seen := make(map[string]struct{})
stop := false
for _, arg := range args {
if stop || arg == "" || arg == "-" || !strings.HasPrefix(arg, "-") || strings.HasPrefix(arg, "--") {
normalized = append(normalized, arg)
if arg == "--" {
stop = true
}
continue
}
name := strings.TrimPrefix(arg, "-")
suffix := ""
if idx := strings.IndexByte(name, '='); idx >= 0 {
suffix = name[idx:]
name = name[:idx]
}
if name == "" || len(name) == 1 || isNegativeNumber(name) {
normalized = append(normalized, arg)
continue
}
if _, ok := flags[name]; !ok {
normalized = append(normalized, arg)
continue
}
normalized = append(normalized, "--"+name+suffix)
rewrite := fmt.Sprintf("-%s -> --%s", name, name)
if _, ok := seen[rewrite]; ok {
continue
}
seen[rewrite] = struct{}{}
rewrites = append(rewrites, rewrite)
}
return normalized, rewrites
}
func legacyLongFlagWarning(rewrites []string) string {
return "warning: deprecated single-dash long flags detected; use GNU-style long flags instead: " + strings.Join(rewrites, ", ") + "\n"
}
func isNegativeNumber(arg string) bool {
for i, r := range arg {
if i == 0 {
if r < '0' || r > '9' {
return false
}
continue
}
if r < '0' || r > '9' {
return false
}
}
return arg != ""
}
func executeCLI() error {
return executeCLIWithLegacyFlagCompat(os.Args[1:], os.Stdout, os.Stderr)
}