mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-26 01:31: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 --)
|
||||
if strings.HasPrefix(arg, "-") {
|
||||
flags = append(flags, arg)
|
||||
// Check if the next argument is a value for this flag (not starting with -)
|
||||
if i+1 < len(args) && !strings.HasPrefix(strings.TrimSpace(args[i+1]), "-") {
|
||||
// Check if the next argument might be a value for this flag
|
||||
// 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])
|
||||
// Only treat as flag value if it's not a valid URL
|
||||
u, err := url.Parse(nextArg)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
if nextArg != "" && !strings.HasPrefix(nextArg, "-") {
|
||||
// Check if it's clearly a URL (has ://)
|
||||
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)
|
||||
i++ // Skip the next argument as it's been consumed
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -81,15 +81,18 @@ func TestYtdlpArgumentParsing(t *testing.T) {
|
||||
// Check if it's a flag (starts with - or --)
|
||||
if strings.HasPrefix(arg, "-") {
|
||||
flags = append(flags, arg)
|
||||
// Check if the next argument is a value for this flag (not starting with -)
|
||||
if i+1 < len(args) && !strings.HasPrefix(strings.TrimSpace(args[i+1]), "-") {
|
||||
// Check if the next argument might be a value for this flag
|
||||
if i+1 < len(args) {
|
||||
nextArg := strings.TrimSpace(args[i+1])
|
||||
// Only treat as flag value if it's not a valid URL
|
||||
u, err := url.Parse(nextArg)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||
if nextArg != "" && !strings.HasPrefix(nextArg, "-") {
|
||||
// Check if it's clearly a URL (has ://)
|
||||
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)
|
||||
i++ // Skip the next argument as it's been consumed
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -99,8 +99,8 @@ func (t *Task) downloadFiles(ctx context.Context, tempDir string) ([]string, err
|
||||
// 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))
|
||||
|
||||
// Combine URLs and flags as arguments
|
||||
// The Run method will pass flags as raw command-line arguments
|
||||
// Combine flags and URLs as arguments (flags first, then URLs)
|
||||
// yt-dlp accepts: yt-dlp [OPTIONS] URL [URL...]
|
||||
args := append(t.Flags, t.URLs...)
|
||||
|
||||
// Run with context for cancellation support
|
||||
|
||||
Reference in New Issue
Block a user