From 13f3018846c8d9fd9fdaa4e3f5c9dcbf3b574fb6 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 11 Feb 2023 17:22:55 +0100 Subject: Drop use of docopt in favor of argparse Signed-off-by: Olivier Gayot --- monitor-menu.py | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/monitor-menu.py b/monitor-menu.py index 413b12d..42228ce 100755 --- a/monitor-menu.py +++ b/monitor-menu.py @@ -1,28 +1,26 @@ #!/usr/bin/env python3 -''' -monitor-menu.py -Usage: - monitor-menu.py [options] - monitor-menu.py [options] - -Options: - --loglevel=LOGLEVEL Set the log level [default: info] -''' - +import argparse import json import logging from os.path import expanduser import subprocess import dialog -import docopt class NoMatchingProfile(ValueError): pass +class LogLevel: + def __init__(self, value: str) -> None: + try: + self.numerical_level = logging.getLevelNamesMapping()[value.upper()] + except KeyError: + raise ValueError("invalid value") + + class MonitorMenu(): def __init__(self, config_file='~/.config/monitor-profiles.json'): with open(expanduser(config_file)) as fh: @@ -72,24 +70,20 @@ class MonitorMenu(): subprocess.run(feh_cmd, check=False) -def main(argv=None): - args = docopt.docopt(__doc__, version='1', argv=argv) +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--log-level", "--loglevel", type=LogLevel, default="info") + parser.add_argument("--index", type=int, required=False) + args = parser.parse_args() + logging.basicConfig(level=args.log_level.numerical_level) menu = MonitorMenu() - numeric_level = getattr(logging, args["--loglevel"].upper(), None) - if not isinstance(numeric_level, int): - raise ValueError(f"invalid log level: {args['--loglevel']}") - logging.basicConfig(level=numeric_level) - - try: - if '' in args and args[''] is not None: - menu.run(int(args[''])) - else: - menu.run() + if args.index is not None: + menu.run(profile_idx=args.index) + else: + menu.run() - except ValueError as e: - raise docopt.DocoptExit(str(e)) if __name__ == '__main__': main() -- cgit v1.2.3