args.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import argparse
  2. from urllib3.util import parse_url
  3. desc = '''Starting program with parameters'''
  4. class ValidateUrl(argparse.Action):
  5. def __call__(self, parser, namespace, values, option_string=None):
  6. if not self.__validate(values):
  7. parser.error(f"Please enter a valid URL. Got: {values}")
  8. setattr(namespace, self.dest, values)
  9. def __validate(self, url) -> bool:
  10. try:
  11. result = parse_url(url)
  12. return all([result.scheme, result.netloc])
  13. except:
  14. return False
  15. class ArgParser:
  16. def __init__(self) -> None:
  17. self.parser = argparse.ArgumentParser(description=desc)
  18. self.parser.add_argument(
  19. "--link", default=None,
  20. help="Insert youtube video URL",
  21. action=ValidateUrl,
  22. type=str
  23. )
  24. self.parser.add_argument(
  25. "--download", default=False,
  26. help="Only downloading and tagging mp3",
  27. action="store_true"
  28. )
  29. self.args = self.parser.parse_args()
  30. print(self.args)