12 proc parseArguments(): CommandLineOptions =
15 options = to_table({"musicOnly": false, "feelingLucky": false, "fullScreen": false, "download": false})
17 for kind, key, value in getopt():
21 of cmdShortOption, cmdLongOption:
23 of "m", "music": options["musicOnly"] = true
24 of "l", "lucky": options["feelingLucky"] = true
25 of "f", "full-screen": options["fullScreen"] = true
26 of "d", "download": options["download"] = true
29 return (searchQuery, options)
32 proc isValidOptions*(options: Options): bool =
33 # Check for invalid combinations of options
34 var invalidCombinations = [("musicOnly", "fullScreen")]
35 for combination in invalidCombinations:
36 if options[combination[0]] and options[combination[1]]:
37 stderr.writeLine fmt"Incompatible options provided: {combination[0]} and {combination[1]}"
44 player = selectMediaPlayer()
45 (searchQuery, options) = parseArguments()
47 if(not isValidOptions(options)):
50 if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
51 directPlay(searchQuery, player)
54 let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
56 proc getUserInput(): string =
57 if options["feelingLucky"]: "0"
59 presentVideoOptions(searchResults[..(limit-1)])
60 stdout.styledWrite(fgYellow, "Choose video number: ")
63 # This is a pure function with no side effects
64 func buildArgs(number: int): seq[string] =
65 var args = @[searchResults[number].url]
66 if options["musicOnly"]: args.add("--no-video")
67 if options["fullScreen"]: args.add("--fullscreen")
71 let userInput = getUserInput()
73 if userInput == "all":
74 for number in 0..(len(searchResults)):
75 play(player, buildArgs(number), searchResults[number].title)
80 # Play the video using the preferred/available media player
81 let videoNumber = parseInt(userInput)
82 play(player, buildArgs(videoNumber), searchResults[videoNumber].title)
83 if options["feelingLucky"]: