-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.py
More file actions
46 lines (33 loc) · 1.76 KB
/
cli.py
File metadata and controls
46 lines (33 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import argparse
import os
from youtool.commands import COMMANDS
def main():
"""Main function for the YouTube CLI Tool.
This function sets up the argument parser for the CLI tool, including options for the YouTube API key and
command-specific subparsers. It then parses the command-line arguments, retrieving the YouTube API key
from either the command-line argument '--api-key' or the environment variable 'YOUTUBE_API_KEY'. If the API
key is not provided through any means, it raises an argparse.ArgumentError.
Finally, the function executes the appropriate command based on the parsed arguments. If an exception occurs
during the execution of the command, it is caught and raised as an argparse error for proper handling.
Raises:
argparse.ArgumentError: If the YouTube API key is not provided.
argparse.ArgumentError: If there is an error during the execution of the command.
"""
parser = argparse.ArgumentParser(description="CLI Tool for managing YouTube videos add playlists")
parser.add_argument("--api-key", type=str, help="YouTube API Key", dest="api_key")
parser.add_argument("--debug", help="Debug mode", dest="debug", default=False, action="store_true")
subparsers = parser.add_subparsers(required=True, dest="command", title="Command", help="Command to be executed")
for command in COMMANDS:
command.parse_arguments(subparsers)
args = parser.parse_args()
args.api_key = args.api_key or os.environ.get("YOUTUBE_API_KEY")
if not args.api_key:
parser.error("YouTube API Key is required")
try:
print(args.func(**args.__dict__))
except Exception as error:
if args.debug:
raise error
parser.error(error)
if __name__ == "__main__":
main()