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/sparsestream.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/sparsestream.py')
-rwxr-xr-x | examples/sparsestream.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/examples/sparsestream.py b/examples/sparsestream.py index 618c291..80ce0c5 100755 --- a/examples/sparsestream.py +++ b/examples/sparsestream.py @@ -1,10 +1,15 @@ #!/usr/bin/env python3 +""" +Either uploads local FILE to libvirt VOLUME, or downloads libvirt +VOLUME into local FILE while preserving FILE/VOLUME sparseness +""" # Example of sparse streams usage # # Authors: # Michal Privoznik <mprivozn@redhat.com> import libvirt, sys, os +from argparse import ArgumentParser def bytesWriteHandler(stream: libvirt.virStream, buf: bytes, opaque: int) -> int: fd = opaque @@ -94,24 +99,22 @@ def upload(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -> os.close(fd) # main -if len(sys.argv) != 5: - print("Usage: ", sys.argv[0], " URI --upload/--download VOLUME FILE") - print("Either uploads local FILE to libvirt VOLUME, or downloads libvirt ") - print("VOLUME into local FILE while preserving FILE/VOLUME sparseness") - sys.exit(1) +parser = ArgumentParser(description=__doc__) +parser.add_argument("uri") +group = parser.add_mutually_exclusive_group(required=True) +group.add_argument("--upload", action="store_const", const=upload, dest="operation") +group.add_argument("--download", action="store_const", const=download, dest="operation") +parser.add_argument("volume") +parser.add_argument("file") +args = parser.parse_args() -conn = libvirt.open(sys.argv[1]) -vol = conn.storageVolLookupByKey(sys.argv[3]) + +conn = libvirt.open(args.uri) +vol = conn.storageVolLookupByKey(args.volume) st = conn.newStream() -if sys.argv[2] == "--download": - download(vol, st, sys.argv[4]) -elif sys.argv[2] == "--upload": - upload(vol, st, sys.argv[4]) -else: - print("Unknown operation: %s " % sys.argv[1]) - sys.exit(1) +args.operation(vol, st, args.file) st.finish() conn.close() |