2016-05-27 17 views
2

Also was ich erreichen möchte, ist sowohl sicherzustellen, dass die Argumente Teil bestimmter vordefinierter Set (hier tool1, tool2, tool3), wie mit @click.option und type=click.Choice() sind, und gleichzeitig zu sein in der Lage, mehrere Argumente wie mit @click.argument mit nargs=-1 zu übergeben.click.Choice für mehrere Argumente

import click 

@click.command() 
@click.option('--tools', type=click.Choice(['tool1', 'tool2', 'tool3'])) 
@click.argument('programs', nargs=-1) 
def chooseTools(programs, tools): 
    """Select one or more tools to solve the task""" 

    # Selection of only one tool, but it is from the predefined set 
    click.echo("Selected tools with 'tools' are {}".format(tools)) 

    # Selection of multiple tools, but no in-built error handling if not in set 
    click.echo("Selected tools with 'programs' are {}".format(programs)) 

zum Beispiel wie folgt Dies würde aussehen:

python selectTools.py --tools tool1 tool3 tool2 nonsense 
Selected tools with 'tools' are tool1 
Selected tools with 'programs' are (u'tool3', u'tool2', u'nonsense') 

Gibt es eine eingebaute Möglichkeit, hier klicken um das zu erreichen? Oder soll ich einfach @click.argument verwenden und den Eingang in der Funktion selbst prüfen?

Da ich ziemlich neu in der Programmierung von Befehlszeilenschnittstellen bin, insbesondere mit click, und nur anfange, tiefer in Python zu graben, würde ich Empfehlungen dafür schätzen, wie man dieses Problem auf eine ordentliche Weise behandelt.

Antwort

2

Wie sich herausstellt, habe ich die Verwendung von multiple=True bei der Verwendung @click.option missverstanden.

Zum Beispiel ist es möglich, möglich -t mehrfach

python selectTools.py -t tool1 -t tool3 -t tool2 

unter Verwendung

@click.option('--tools', '-t', type=click.Choice(['tool1', 'tool2', 'tool3']), multiple=True) 

und damit macht die Auswahl mehrerer Werkzeuge aus der Auswahl zu berufen.