argparse
This commit is contained in:
parent
6d2a43a357
commit
f9e940e509
1 changed files with 29 additions and 41 deletions
70
virt-back
70
virt-back
|
|
@ -16,8 +16,8 @@ the latest version may be downloaded here:
|
|||
https://git.unturf.com/python/virt-back
|
||||
"""
|
||||
|
||||
"""The varible doms represents a list of libvirt dom objects.
|
||||
Use the Domfetcher class to aquire lists of dom objects."""
|
||||
"""The variable doms represents a list of libvirt dom objects.
|
||||
Use the Domfetcher class to acquire a list of dom objects."""
|
||||
|
||||
import libvirt
|
||||
import tarfile
|
||||
|
|
@ -26,11 +26,9 @@ import re
|
|||
from time import sleep
|
||||
from datetime import date
|
||||
from sys import exit
|
||||
from optparse import OptionParser, OptionGroup
|
||||
from os import path
|
||||
from os import remove
|
||||
import argparse
|
||||
from os import path, remove
|
||||
from shutil import move, copy2
|
||||
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
|
|
@ -417,12 +415,10 @@ def rotate(target, retention=3):
|
|||
def getoptions():
|
||||
"""Fetch cli args, parse and map to python, test sanity"""
|
||||
|
||||
# create an option parcer object
|
||||
p = OptionParser()
|
||||
# create an argument parser object
|
||||
parser = argparse.ArgumentParser(description=DESCRIPTION)
|
||||
|
||||
p.set_description(DESCRIPTION)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-q",
|
||||
"--quiet",
|
||||
dest="quiet",
|
||||
|
|
@ -431,7 +427,7 @@ def getoptions():
|
|||
help="prevent output to stdout",
|
||||
)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--date",
|
||||
dest="tardate",
|
||||
|
|
@ -440,7 +436,7 @@ def getoptions():
|
|||
help="append date to tar filename [default: no date]",
|
||||
)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-g",
|
||||
"--no-gzip",
|
||||
dest="nogzip",
|
||||
|
|
@ -449,17 +445,17 @@ def getoptions():
|
|||
help="do not gzip the resulting tar file",
|
||||
)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-a",
|
||||
"--retention",
|
||||
dest="retention",
|
||||
metavar="amount",
|
||||
default=3,
|
||||
type="int",
|
||||
type=int,
|
||||
help="backups to retain [default: 3]",
|
||||
)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--path",
|
||||
dest="backpath",
|
||||
|
|
@ -468,19 +464,17 @@ def getoptions():
|
|||
help="backup path [default: '/KVMBACK']",
|
||||
)
|
||||
|
||||
p.add_option(
|
||||
parser.add_argument(
|
||||
"-u", "--uri", dest="uri", metavar="'URI'", help="optional hypervisor uri"
|
||||
)
|
||||
|
||||
# Actions for info testing: These options display info/ test a list of guests only.
|
||||
|
||||
g0 = OptionGroup(
|
||||
p,
|
||||
info_group = parser.add_argument_group(
|
||||
"Actions for info testing",
|
||||
"These options display info or test a list of guests.",
|
||||
)
|
||||
|
||||
g0.add_option(
|
||||
info_group.add_argument(
|
||||
"-i",
|
||||
"--info",
|
||||
dest="info",
|
||||
|
|
@ -489,7 +483,7 @@ def getoptions():
|
|||
help="info/test a list of guests (space delimited dom names)",
|
||||
)
|
||||
|
||||
g0.add_option(
|
||||
info_group.add_argument(
|
||||
"--info-all",
|
||||
dest="infoall",
|
||||
action="store_true",
|
||||
|
|
@ -498,14 +492,12 @@ def getoptions():
|
|||
)
|
||||
|
||||
# WARNING: Dangerous options below, option grouping for scary actions
|
||||
|
||||
g1 = OptionGroup(
|
||||
p,
|
||||
action_group = parser.add_argument_group(
|
||||
"Actions for a list of dom names",
|
||||
"WARNING: These options WILL bring down guests!",
|
||||
)
|
||||
|
||||
g1.add_option(
|
||||
action_group.add_argument(
|
||||
"-b",
|
||||
"--backup",
|
||||
dest="backup",
|
||||
|
|
@ -514,7 +506,7 @@ def getoptions():
|
|||
help="backup a list of guests (space delimited dom names)",
|
||||
)
|
||||
|
||||
g1.add_option(
|
||||
action_group.add_argument(
|
||||
"-r",
|
||||
"--reboot",
|
||||
dest="reboot",
|
||||
|
|
@ -523,7 +515,7 @@ def getoptions():
|
|||
help="reboot a list of guests (space delimited dom names)",
|
||||
)
|
||||
|
||||
g1.add_option(
|
||||
action_group.add_argument(
|
||||
"-s",
|
||||
"--shutdown",
|
||||
dest="shutdown",
|
||||
|
|
@ -532,7 +524,7 @@ def getoptions():
|
|||
help="shutdown a list of guests (space delimited dom names)",
|
||||
)
|
||||
|
||||
g1.add_option(
|
||||
action_group.add_argument(
|
||||
"-c",
|
||||
"--create",
|
||||
dest="create",
|
||||
|
|
@ -541,11 +533,11 @@ def getoptions():
|
|||
help="start a list of guests (space delimited dom names)",
|
||||
)
|
||||
|
||||
g2 = OptionGroup(
|
||||
p, "Actions for all doms", "WARNING: These options WILL bring down ALL guests!"
|
||||
all_group = parser.add_argument_group(
|
||||
"Actions for all doms", "WARNING: These options WILL bring down ALL guests!"
|
||||
)
|
||||
|
||||
g2.add_option(
|
||||
all_group.add_argument(
|
||||
"--backup-all",
|
||||
dest="backupall",
|
||||
action="store_true",
|
||||
|
|
@ -553,7 +545,7 @@ def getoptions():
|
|||
help="attempt to shutdown, backup, and start ALL guests",
|
||||
)
|
||||
|
||||
g2.add_option(
|
||||
all_group.add_argument(
|
||||
"--reboot-all",
|
||||
dest="rebootall",
|
||||
action="store_true",
|
||||
|
|
@ -561,7 +553,7 @@ def getoptions():
|
|||
help="attempt to shutdown and then start ALL guests",
|
||||
)
|
||||
|
||||
g2.add_option(
|
||||
all_group.add_argument(
|
||||
"--shutdown-all",
|
||||
dest="shutdownall",
|
||||
action="store_true",
|
||||
|
|
@ -569,7 +561,7 @@ def getoptions():
|
|||
help="attempt to shutdown ALL guests",
|
||||
)
|
||||
|
||||
g2.add_option(
|
||||
all_group.add_argument(
|
||||
"--create-all",
|
||||
dest="createall",
|
||||
action="store_true",
|
||||
|
|
@ -577,13 +569,8 @@ def getoptions():
|
|||
help="attempt to start ALL guests",
|
||||
)
|
||||
|
||||
# attach groups g1 and g2 to the parser p object
|
||||
p.add_option_group(g0)
|
||||
p.add_option_group(g1)
|
||||
p.add_option_group(g2)
|
||||
|
||||
# parse options and args
|
||||
options, guest_names = p.parse_args()
|
||||
options, unknown_args = parser.parse_known_args()
|
||||
|
||||
# the actionsum should be 1 to continue, bool math ftw
|
||||
actions = [
|
||||
|
|
@ -601,6 +588,7 @@ def getoptions():
|
|||
actionsum = sum(actions)
|
||||
|
||||
if actionsum == 1:
|
||||
guest_names = unknown_args
|
||||
return options, guest_names
|
||||
else:
|
||||
exit(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue