Andere denken Sie die Ergebnisse der Parsen der commandlline wollen, die Werte aus dem args
Namespace. Aber ich vermute, dass Sie das von der add_argument
Methode definiert Action
Objekt wollen
In einer interaktiven Shell ich einen Parser definieren als:
In [207]: parser=argparse.ArgumentParser()
In [208]: arg1= parser.add_argument("num",help="The fibnocacci number to calculate:", type=int)
In [209]: arg2=parser.add_argument("-f","--file",help="Output to the text file",action="store_true")
In [210]: arg1
Out[210]: _StoreAction(option_strings=[], dest='num', nargs=None, const=None, default=None, type=<type 'int'>, choices=None, help='The fibnocacci number to calculate:', metavar=None)
In [211]: arg2
Out[211]: _StoreTrueAction(option_strings=['-f', '--file'], dest='file', nargs=0, const=True, default=False, type=None, choices=None, help='Output to the text file', metavar=None)
In [212]: parser._actions
Out[212]:
[_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None),
_StoreAction(option_strings=[], dest='num', nargs=None, const=None, default=None, type=<type 'int'>, choices=None, help='The fibnocacci number to calculate:', metavar=None),
_StoreTrueAction(option_strings=['-f', '--file'], dest='file', nargs=0, const=True, default=False, type=None, choices=None, help='Output to the text file', metavar=None)]
add_argument
schafft eine Action
Unterklasse (basierend auf action
Parametern). Sie können dies in Ihrer eigenen Variablen speichern oder in der Liste des Parsers finden.
Das Drucken zeigt einige seiner Attribute, aber Sie können mehr davon untersuchen und sogar ändern.
In [213]: arg1.help
Out[213]: 'The fibnocacci number to calculate:'
In [214]: arg1.type
Out[214]: int
In [215]: arg1.dest
Out[215]: 'num'
In [217]: vars(arg1)
Out[217]:
{'choices': None,
'const': None,
'container': <argparse._ArgumentGroup at 0x8f0cd4c>,
'default': None,
'dest': 'num',
'help': 'The fibnocacci number to calculate:',
'metavar': None,
'nargs': None,
'option_strings': [],
'required': True,
'type': int}
Sie viele müssen die Klassendefinitionen in der argparse.py
Datei zu prüfen, diese Attribute zu verstehen.
wenn ich arg1.type bekomme ich und nicht int? –
Ich bekomme das mit 'print (arg1.type)'. 'int' ist sowohl eine Klasse als auch eine Funktion, die eine Ganzzahl erzeugt. – hpaulj