argparse: let the same required argument be positional OR optional
问题 I would like to have a required command line argument passed as a positional argument or an optional argument. For example, I would like the same action be performed from any of the following invocations: prog 10 prog -10 prog -n 10 prog --num 10 Is this possible with argparse? 回答1: With a mutually exclusive group I can create a reasonable approximation: In an interactive session: In [10]: parser=argparse.ArgumentParser() In [11]: grp=parser.add_mutually_exclusive_group(required=True) In [12]