If you specify a fromfile-prefix-chars
(such as @
), the user could specify a file to read these urls from. The lines of this file are read and added to the sys.argv
just as though they had been typed in the command line.
http://docs.python.org/dev/library/argparse.html#fromfile-prefix-chars
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('urls',nargs='+') # require 1 or more url
args = parser.parse_args()
these all work (where urls.txt
has one url string per line)
parser.parse_args('four.txt'.split())
parser.parse_args('@urls.txt four.txt'.split())
parser.parse_args('@urls.txt'.split())
while no file or url gives an error
parser.parse_args(''.split())
The default help
does not say anything about allowing that @url.txt
file name, but you could add that in a custom description string.
PS - Your post-argparse test is simpler than anything you could specify in argparse. A required mutually-exclusive-group involving the -i
argument and the positional might work, but it doesn't do much more than your test. I wrote 'might' because I'd have to double whether a '*' positional works in such a group. But this is one of the first cases I've seen where that fromfile_prefix_chars
option would be genuinely useful.