diff options
author | Philipp Hahn <hahn@univention.de> | 2020-07-28 07:05:19 +0200 |
---|---|---|
committer | Philipp Hahn <pmhahn+github@pmhahn.de> | 2020-08-05 07:43:02 +0000 |
commit | 06aba185a8c6354776776f3e665317f5c763af5c (patch) | |
tree | 3a944c876d39b6f8525817280ebfd2c00f29037d /examples/consolecallback.py | |
parent | 9cf539a2a8c0055ba2a42d26677ec392e0e4f7c1 (diff) | |
download | libvirt-python-06aba185a8c6354776776f3e665317f5c763af5c.tar.gz |
examples: Convert to ArgumentParser
Replace getopt() and hand-rolled-parser with argparse.ArgumentParser.
Fix wrong header comments copy-pasted from domstart.py
Signed-off-by: Philipp Hahn <hahn@univention.de>
Diffstat (limited to 'examples/consolecallback.py')
-rw-r--r-- | examples/consolecallback.py | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/examples/consolecallback.py b/examples/consolecallback.py index e8820f7..9947f75 100644 --- a/examples/consolecallback.py +++ b/examples/consolecallback.py @@ -2,6 +2,7 @@ # consolecallback - provide a persistent console that survives guest reboots import sys, os, logging, libvirt, tty, termios, atexit +from argparse import ArgumentParser from typing import Optional # noqa F401 def reset_term() -> None: @@ -64,18 +65,15 @@ def lifecycle_callback(connection: libvirt.virConnect, domain: libvirt.virDomain console.uuid, console.state[0], console.state[1]) # main -if len(sys.argv) != 3: - print("Usage:", sys.argv[0], "URI UUID") - print("for example:", sys.argv[0], "'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'") - sys.exit(1) - -uri = sys.argv[1] -uuid = sys.argv[2] +parser = ArgumentParser(epilog="Example: %(prog)s 'qemu:///system' '32ad945f-7e78-c33a-e96d-39f25e025d81'") +parser.add_argument("uri") +parser.add_argument("uuid") +args = parser.parse_args() print("Escape character is ^]") logging.basicConfig(filename='msg.log', level=logging.DEBUG) -logging.info("URI: %s", uri) -logging.info("UUID: %s", uuid) +logging.info("URI: %s", args.uri) +logging.info("UUID: %s", args.uuid) libvirt.virEventRegisterDefaultImpl() libvirt.registerErrorHandler(error_handler, None) @@ -84,7 +82,7 @@ atexit.register(reset_term) attrs = termios.tcgetattr(0) tty.setraw(0) -console = Console(uri, uuid) +console = Console(args.uri, args.uuid) console.stdin_watch = libvirt.virEventAddHandle(0, libvirt.VIR_EVENT_HANDLE_READABLE, stdin_callback, console) while check_console(console): |