summaryrefslogtreecommitdiff
path: root/monitor-menu.py
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2023-02-11 17:22:55 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2023-02-11 17:47:20 +0100
commit13f3018846c8d9fd9fdaa4e3f5c9dcbf3b574fb6 (patch)
tree289a9fd2358a1eda39f475438698d4daf84634cc /monitor-menu.py
parentee64301c07fe5a8a363d91670f1caddc4a744abd (diff)
Drop use of docopt in favor of argparse
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'monitor-menu.py')
-rwxr-xr-xmonitor-menu.py44
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()