mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-31 13:11:29 +08:00
Improve flag parsing logic and clarify argument order
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
This commit is contained in:
@@ -37,15 +37,19 @@ func handleYtdlpCmd(ctx *ext.Context, update *ext.Update) error {
|
|||||||
// Check if it's a flag (starts with - or --)
|
// Check if it's a flag (starts with - or --)
|
||||||
if strings.HasPrefix(arg, "-") {
|
if strings.HasPrefix(arg, "-") {
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
// Check if the next argument is a value for this flag (not starting with -)
|
// Check if the next argument might be a value for this flag
|
||||||
if i+1 < len(args) && !strings.HasPrefix(strings.TrimSpace(args[i+1]), "-") {
|
// Don't consume it if it starts with - or looks like a URL with scheme
|
||||||
|
if i+1 < len(args) {
|
||||||
nextArg := strings.TrimSpace(args[i+1])
|
nextArg := strings.TrimSpace(args[i+1])
|
||||||
// Only treat as flag value if it's not a valid URL
|
if nextArg != "" && !strings.HasPrefix(nextArg, "-") {
|
||||||
u, err := url.Parse(nextArg)
|
// Check if it's clearly a URL (has ://)
|
||||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
if strings.Contains(nextArg, "://") {
|
||||||
|
// It's a URL, don't consume it as a flag value
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Otherwise, treat it as a flag value
|
||||||
flags = append(flags, nextArg)
|
flags = append(flags, nextArg)
|
||||||
i++ // Skip the next argument as it's been consumed
|
i++ // Skip the next argument as it's been consumed
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -81,15 +81,18 @@ func TestYtdlpArgumentParsing(t *testing.T) {
|
|||||||
// Check if it's a flag (starts with - or --)
|
// Check if it's a flag (starts with - or --)
|
||||||
if strings.HasPrefix(arg, "-") {
|
if strings.HasPrefix(arg, "-") {
|
||||||
flags = append(flags, arg)
|
flags = append(flags, arg)
|
||||||
// Check if the next argument is a value for this flag (not starting with -)
|
// Check if the next argument might be a value for this flag
|
||||||
if i+1 < len(args) && !strings.HasPrefix(strings.TrimSpace(args[i+1]), "-") {
|
if i+1 < len(args) {
|
||||||
nextArg := strings.TrimSpace(args[i+1])
|
nextArg := strings.TrimSpace(args[i+1])
|
||||||
// Only treat as flag value if it's not a valid URL
|
if nextArg != "" && !strings.HasPrefix(nextArg, "-") {
|
||||||
u, err := url.Parse(nextArg)
|
// Check if it's clearly a URL (has ://)
|
||||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
if strings.Contains(nextArg, "://") {
|
||||||
|
// It's a URL, don't consume it as a flag value
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Otherwise, treat it as a flag value
|
||||||
flags = append(flags, nextArg)
|
flags = append(flags, nextArg)
|
||||||
i++ // Skip the next argument as it's been consumed
|
i++ // Skip the next argument as it's been consumed
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -99,8 +99,8 @@ func (t *Task) downloadFiles(ctx context.Context, tempDir string) ([]string, err
|
|||||||
// Execute download with URLs and custom flags
|
// Execute download with URLs and custom flags
|
||||||
logger.Infof("Executing yt-dlp for %d URL(s) with %d custom flag(s)", len(t.URLs), len(t.Flags))
|
logger.Infof("Executing yt-dlp for %d URL(s) with %d custom flag(s)", len(t.URLs), len(t.Flags))
|
||||||
|
|
||||||
// Combine URLs and flags as arguments
|
// Combine flags and URLs as arguments (flags first, then URLs)
|
||||||
// The Run method will pass flags as raw command-line arguments
|
// yt-dlp accepts: yt-dlp [OPTIONS] URL [URL...]
|
||||||
args := append(t.Flags, t.URLs...)
|
args := append(t.Flags, t.URLs...)
|
||||||
|
|
||||||
// Run with context for cancellation support
|
// Run with context for cancellation support
|
||||||
|
|||||||
Reference in New Issue
Block a user