[mutter] tools/get-state: Change to use argparse



commit e883046c5fee859a6a6dcf8bc15ca07e9fb4c9c2
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Thu Jun 2 17:44:02 2022 +0200

    tools/get-state: Change to use argparse
    
    This will make adding more arguments easier.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2448>

 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 624738ad18..79d0173469 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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]