This commit is contained in:
joren
2026-04-04 20:38:08 +02:00
parent ea32c0baa6
commit c131bf6ab1
4 changed files with 4301 additions and 2192 deletions

View File

@@ -117,7 +117,7 @@ func Load() (Config, error) {
flag.Var(&playlists, "playlist", "Playlist name to transfer (repeatable)")
flag.Var(&playlistURLs, "playlist-url", "Spotify playlist URL/URI/ID to transfer (repeatable)")
if err := flag.CommandLine.Parse(parseArgs); err != nil {
if err := flag.CommandLine.Parse(preprocessArgs(parseArgs)); err != nil {
return Config{}, err
}
cfg.Command = command
@@ -158,9 +158,6 @@ func (c Config) Validate() error {
if c.TargetPlaylistID < 0 {
return fmt.Errorf("target-playlist-id must be >= 0")
}
if c.TargetPlaylistID > 0 && !c.Monitor {
return fmt.Errorf("target-playlist-id is currently supported with --monitor mode")
}
if strings.TrimSpace(c.SessionPath) == "" {
return fmt.Errorf("session file path cannot be empty")
}
@@ -190,6 +187,48 @@ func splitComma(s string) []string {
return res
}
type boolFlag interface {
IsBoolFlag() bool
}
// preprocessArgs moves non-flag positional arguments to the end so that flags
// can appear in any order relative to non-flag arguments.
func preprocessArgs(args []string) []string {
boolFlagNames := make(map[string]bool)
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if bf, ok := f.Value.(boolFlag); ok && bf.IsBoolFlag() {
boolFlagNames[f.Name] = true
}
})
var flagArgs []string
var positional []string
i := 0
for i < len(args) {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
positional = append(positional, arg)
i++
continue
}
flagArgs = append(flagArgs, arg)
i++
name := strings.TrimLeft(arg, "-")
if idx := strings.Index(name, "="); idx >= 0 {
continue // value embedded in --flag=value form
}
if boolFlagNames[name] {
continue // bool flags don't consume a following value
}
if i < len(args) && !strings.HasPrefix(args[i], "-") {
flagArgs = append(flagArgs, args[i])
i++
}
}
return append(flagArgs, positional...)
}
func envOr(key, fallback string) string {
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
return v