Sto provando ad avere un gruppo richiesto che si escluda a vicenda con un parametro richiesto. Di seguito è riportato il codice che ho messoPython argparse: gruppo obbligatorio che si esclude reciprocamente con un'opzione richiesta
#!/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()
Ora l'output di questo programma come segue
$ 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
Ma se io non-commento delle dalla linea 20 - 22 poi il cambiamento di uscita, come di seguito
$ 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
Ma voglio che sia -n/-t insieme a -k diventino obbligatori. Come ottenere lo stesso.
Grazie, non lo sapevo dato che sto imparando Python :). Ancora una cosa come verificare se l'argomento ha qualche valore. C'è qualche funzione vuota (C++). – Abhinav
Dovresti davvero fare una domanda separata se non è correlata (dopo averlo controllato non è stato già chiesto, ovviamente), ma ... Dipende da cosa esattamente vuoi fare. Python ha un concetto di "verità", quindi puoi semplicemente [usare la variabile direttamente in una clausola if o in qualsiasi altra cosa] (https://docs.python.org/2/library/stdtypes.html#truth-value-testing), per esempio. 'se variabile:' o 'se non variabile:'. A parte questo, puoi usare ad es. 'len (variabile) == 0' sui contenitori o' variabile è None' per verificare che sia Nessuno specificatamente. O semplicemente confrontare con il valore vuoto per il tipo in questione. –
Il valore predefinito per gli argomenti 'argparse' è' None'. Quindi 'print args.name is None' stamperà' True'. Il valore predefinito per 'args.quiet' è' False'. – hpaulj