From e883046c5fee859a6a6dcf8bc15ca07e9fb4c9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Thu, 2 Jun 2022 17:44:02 +0200 Subject: [PATCH] tools/get-state: Change to use argparse This will make adding more arguments easier. Part-of: --- tools/get-state.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/tools/get-state.py b/tools/get-state.py index 624738ad1..79d017346 100755 --- a/tools/get-state.py +++ b/tools/get-state.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import argparse import enum import subprocess import sys @@ -12,29 +13,17 @@ class Source(enum.Enum): FILE = 2 -def print_usage(): - print(f'Usage: {sys.argv[0]} [--help] [FILE]') +parser = argparse.ArgumentParser(description='Get display state') +parser.add_argument('file', metavar='FILE', type=str, nargs='?', + help='Read the output from gdbus call instead of calling D-Bus') -def print_help(): - print(f'Without any argument this command queries the GNOME display server ' - 'using D-Bus and pretty prints the result') - print() - print(f' --help Show this help screen') - print(f' FILE Read the output from gdbus call instead of calling D-Bus') +args = parser.parse_args() - -if len(sys.argv) > 2: - print_usage() - sys.exit(1) -elif len(sys.argv) == 1: - source = Source.DBUS -elif sys.argv[1] == '--help': - print_usage() - print_help() - sys.exit(0) +if args.file: + source = Source.FILE + path = args.file else: - source = Source.FILE - path = sys.argv[1] + source = Source.DBUS type_signature = '(ua((ssss)a(siiddada{sv})a{sv})a(iiduba(ssss)a{sv})a{sv})' variant_type = GLib.VariantType.new(type_signature)