diff options
Diffstat (limited to 'monitor-menu.py')
-rwxr-xr-x | monitor-menu.py | 44 |
1 files 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] <profile> - -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 '<profile>' in args and args['<profile>'] is not None: - menu.run(int(args['<profile>'])) - 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() |