Aby dostać dokładnie to, czego szukasz, sztuką jest wykorzystanie parse_known_args()
zamiast parse_args()
:
#!/bin/env python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a', action="store_true")
parser.add_argument('-b', action="store_true")
opts = parser.parse_known_args()
# Print info about flags
if opts[0].a: print('You set the option "-a"')
if opts[0].b: print('You set the option "-b"')
# Collect remainder (opts[1] is a list (possibly empty) of all remaining args)
if opts[1]: print('You passed the strings %s' % opts[1])
EDIT:
Powyższy kod wyświetla następujące informacje pomocy:
./clargs.py -h
usage: clargs_old.py [-h] [-a] [-b]
optional arguments:
-h, --help show this help message and exit
-a
-b
Jeśli chcesz poinformować użytkownika o opcjonalnym dowolnym argumencie, jedynym rozwiązaniem, jakie mogę wymyślić jest podklasa ArgumentParser i zapisz to w sobie.
Na przykład:
#!/bin/env python
import os
import argparse
class MyParser(argparse.ArgumentParser):
def format_help(self):
help = super(MyParser, self).format_help()
helplines = help.splitlines()
helplines[0] += ' [FOO]'
helplines.append(' FOO some description of FOO')
helplines.append('') # Just a trick to force a linesep at the end
return os.linesep.join(helplines)
parser = MyParser()
parser.add_argument('-a', action="store_true")
parser.add_argument('-b', action="store_true")
opts = parser.parse_known_args()
# Print info about flags
if opts[0].a: print('You set the option "-a"')
if opts[0].b: print('You set the option "-b"')
# Collect remainder
if opts[1]: print('You passed the strings %s' % opts[1])
który wyświetla następujące informacje pomocy:
./clargs.py -h
usage: clargs.py [-h] [-a] [-b] [FOO]
optional arguments:
-h, --help show this help message and exit
-a
-b
FOO some description of FOO
Uwaga dodanie [FOO]
w wierszu „użytkowania” i FOO
w pomocy w sekcji „argumentów opcjonalnych ".
Czy przechodziłeś przez samouczek argparse? Wszystko, co opisujesz, jest tam. – SheetJS
Jedyną nie omawianą częścią iirc jest zachowanie 'nargs =" * "' (tak, że jeden argument pozycyjny może pobrać wszystkie pozostałe argumenty) – SheetJS
@Nirk, Trudne jest to, że jeśli po prostu użyjesz 'parse_args', gdy tylko zobaczy argument" pozycyjny ", przestaje szukać innych argumentów i zbiera argumenty z argumentu pozycyjnego na koniec. W większości przypadków jest to w porządku, ale złamałby pierwszy i trzeci przykład. – jedwards