In the code below, I cannot add the -v and -p arguments after the action (update, blacklist, auto), presumably due to the subparser. How can I make it so that I can add these optional arguments in any order?
parser = argparse.ArgumentParser(usage='pyfilter.py <file> <action> <options>')
parser.add_argument('file', help='blacklist file containing IPs', type=str)
subparsers = parser.add_subparsers(help='help', dest='action')
parser_update = subparsers.add_parser('update', help='update help')
parser_blacklist = subparsers.add_parser('blacklist', help='blacklist help')
parser_auto = subparsers.add_parser('auto', help='auto help')
parser_auto.add_argument('-i', '--interval', help='interval help')
parser.add_argument('-p', '--port', help='specify the port to block', type=int)
parser.add_argument('-v', '--verbose', help='write output to screen', nargs='?')
args = parser.parse_args()
According to the parser, this is valid:
python3.5 testfilter.py /etc/blacklist.lst -p 22 -v yes update
Whereas this yields errors for every argument following 'update':
python3.5 testfilter.py /etc/blacklist.lst update -p 22 -v yes