Ich versuche, eine erforderliche, sich gegenseitig ausschließende Gruppe mit einem erforderlichen Parameter zu haben. Unten ist der Code, den ichPython argparse: Gegenseitig exklusive erforderliche Gruppe mit einer erforderlichen Option
gesetzt habe#!/usr/bin/python
import argparse
import sys
# Check for the option provided as part of arguments
def parseArgv():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", choices=[1,2,3,4],
help = "Increase verbosity")
group.add_argument("-q", "--quiet", action="store_true", help = "Run quietly")
name = parser.add_mutually_exclusive_group(required=True)
name.add_argument("-n", "--name", help = "Name of the virtual machine")
name.add_argument("-t", "--template", help = "Name of the template to use \
for creating vm. If path is not provided then it will be looked \
under template directory.")
parser.add_argument("-s", "--save", help = "Save the machine template. If \
path is not provided then it will be saved under template directory.");
#parser.add_argument("-k", "--kick_start", required = True, help = "Name of the \
# kick start file. If path is not provided then it will be look into http \
# directory.")
if len(sys.argv) == 1:
parser.print_help()
args = parser.parse_args()
if __name__ == '__main__':
parseArgv()
Jetzt dieses Programm die Ausgabe wie folgt
$ python test.py
usage: test.py [-h] [-v {1,2,3,4} | -q] (-n NAME | -t TEMPLATE) [-s SAVE]
optional arguments:
-h, --help show this help message and exit
-v {1,2,3,4}, --verbose {1,2,3,4}
Increase verbosity
-q, --quiet Run quietly
-n NAME, --name NAME Name of the virtual machine
-t TEMPLATE, --template TEMPLATE
Name of the template to use for creating vm. If path
is not provided then it will be looked under template
directory.
-s SAVE, --save SAVE Save the machine template. If path is not provided
then it will be saved under template directory.
usage: test.py [-h] [-v {1,2,3,4} | -q] (-n NAME | -t TEMPLATE) [-s SAVE]
test.py: error: one of the arguments -n/--name -t/--template is required
Aber wenn ich un-Kommentar des von der Linie 20-22 dann wie unten
der Ausgangsänderung$ python test.py
usage: test.py [-h] [-v {1,2,3,4} | -q] (-n NAME | -t TEMPLATE) [-s SAVE] -k
KICK_START
optional arguments:
-h, --help show this help message and exit
-v {1,2,3,4}, --verbose {1,2,3,4}
Increase verbosity
-q, --quiet Run quietly
-n NAME, --name NAME Name of the virtual machine
-t TEMPLATE, --template TEMPLATE
Name of the template to use for creating vm. If path
is not provided then it will be looked under template
directory.
-s SAVE, --save SAVE Save the machine template. If path is not provided
then it will be saved under template directory.
-k KICK_START, --kick_start KICK_START
Name of the kick start file. If path is not provided
then it will be look into http directory.
usage: test.py [-h] [-v {1,2,3,4} | -q] (-n NAME | -t TEMPLATE) [-s SAVE] -k
KICK_START
test.py: error: argument -k/--kick_start is required
Aber ich möchte, dass entweder -n/-t zusammen mit -k obligatorisch werden. Wie man dasselbe erreicht.
Danke, ich wusste es nicht, da ich Python lerne :). Eine weitere Sache, wie überprüft werden kann, ob Argument einen Wert hat. Gibt es eine leere (C++) ähnliche Funktion? – Abhinav
Du solltest wirklich eine separate Frage stellen, wenn sie nicht zusammenhängt (nach Überprüfung wurde das natürlich noch nicht gefragt), aber ... Es kommt darauf an was genau du machen willst. Python hat ein Konzept der "Wahrhaftigkeit", also können Sie [die Variable direkt in einer if-Klausel oder was auch immer verwenden] (https://docs.python.org/2/library/stdtypes.html#truth-value-testing)), z.B. 'if variable:' oder 'if not variable:'. Abgesehen davon können Sie zB. 'len (variable) == 0' in Containern oder' Variable ist None', um zu überprüfen, dass es None ist. Oder vergleichen Sie einfach mit dem leeren Wert für den betreffenden Typ. –
Der Standardwert für Ihre argparse Argumente ist 'None'. "Print args.name ist None" wird also 'True' ausgeben. Der Standardwert für 'args.quiet' ist' False'. – hpaulj