1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2025-12-14 04:23:47 +03:00

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>
This commit is contained in:
Philipp Hahn
2020-07-28 07:05:19 +02:00
committed by Philipp Hahn
parent 9cf539a2a8
commit 06aba185a8
12 changed files with 142 additions and 235 deletions

View File

@@ -1,20 +1,18 @@
#!/usr/bin/env python3
# esxlist - list active domains of an ESX host and print some info.
# also demonstrates how to use the libvirt.openAuth() method
"""
List active domains of an ESX host and print some info.
"""
# also demonstrates how to use the libvirt.openAuth() method
import libvirt
import sys
import os
import libxml2
import getpass
from argparse import ArgumentParser
from typing import Any, List
def usage() -> None:
print("Usage: %s HOSTNAME" % sys.argv[0])
print(" List active domains of HOSTNAME and print some info")
# This is the callback method passed to libvirt.openAuth() (see below).
#
# The credentials argument is a list of credentials that libvirt (actually
@@ -73,15 +71,12 @@ def print_xml(key: str, ctx, path: str) -> str:
return value
if len(sys.argv) != 2:
usage()
sys.exit(2)
hostname = sys.argv[1]
parser = ArgumentParser(description=__doc__)
parser.add_argument("hostname")
args = parser.parse_args()
# Connect to libvirt
uri = "esx://%s/?no_verify=1" % hostname
uri = "esx://%s/?no_verify=1" % args.hostname
# The auth argument is a list that contains 3 items:
# - a list of supported credential types
@@ -101,7 +96,7 @@ auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
try:
conn = libvirt.openAuth(uri, auth, 0)
except libvirt.libvirtError:
print("Failed to open connection to %s" % hostname)
print("Failed to open connection to %s" % args.hostname)
sys.exit(1)
state_names = { libvirt.VIR_DOMAIN_RUNNING : "running",