scripts/qemu-ga-client: replace deprecated optparse with argparse

optparse isn't supported anymore, it's from the python2 days. Replace it
with the mostly similar argparse.

Signed-off-by: John Snow <jsnow@redhat.com>
Message-id: 20210604155532.1499282-5-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
This commit is contained in:
John Snow 2021-06-04 11:55:25 -04:00
parent e6de9ce90a
commit 0cf1a52d28

View File

@ -37,8 +37,8 @@
# See also: https://wiki.qemu.org/Features/QAPI/GuestAgent # See also: https://wiki.qemu.org/Features/QAPI/GuestAgent
# #
import argparse
import base64 import base64
import optparse
import os import os
import random import random
import sys import sys
@ -255,7 +255,7 @@ def _cmd_reboot(client, args):
commands = [m.replace('_cmd_', '') for m in dir() if '_cmd_' in m] commands = [m.replace('_cmd_', '') for m in dir() if '_cmd_' in m]
def main(address, cmd, args): def send_command(address, cmd, args):
if not os.path.exists(address): if not os.path.exists(address):
print('%s not found' % address) print('%s not found' % address)
sys.exit(1) sys.exit(1)
@ -283,25 +283,23 @@ def main(address, cmd, args):
globals()['_cmd_' + cmd](client, args) globals()['_cmd_' + cmd](client, args)
if __name__ == '__main__': def main():
address = os.environ.get('QGA_CLIENT_ADDRESS') address = os.environ.get('QGA_CLIENT_ADDRESS')
usage = ("%prog [--address=<unix_path>|<ipv4_address>]" parser = argparse.ArgumentParser()
" <command> [args...]\n") parser.add_argument('--address', action='store',
usage += '<command>: ' + ', '.join(commands) default=address,
parser = optparse.OptionParser(usage=usage) help='Specify a ip:port pair or a unix socket path')
parser.add_option('--address', action='store', type='string', parser.add_argument('command', choices=commands)
default=address, parser.add_argument('args', nargs='*')
help='Specify a ip:port pair or a unix socket path')
options, args = parser.parse_args()
address = options.address args = parser.parse_args()
if address is None: if args.address is None:
parser.error('address is not specified') parser.error('address is not specified')
sys.exit(1) sys.exit(1)
if len(args) == 0: send_command(args.address, args.command, args.args)
parser.error('Less argument')
sys.exit(1)
main(address, args[0], args[1:])
if __name__ == '__main__':
main()