1
$ cat  test_version.py
import argparse
parser = argparse.ArgumentParser(prog='test_version.py')
parser.add_argument("-f", "--file", help="file name", type=str, required=True)
parser.add_argument("-v", "--version", help="show program version", action="store_true")
args = parser.parse_args()

if args.version:
    print('test version: 3.5.1')    

$ python test_version.py --version
usage: test_version.py [-h] -f FILE [-v]
test_version.py: error: the following arguments are required: -f/--file

$ python test_version.py --version -f a
test version: 3.5.1

Question> Is there a method I can use to enable the following operation?

$ python test_version.py --version
test version: 3.5.1

As you can see, in this case, I don't have to provide the required parameter.

tadman
  • 208,517
  • 23
  • 234
  • 262
q0987
  • 34,938
  • 69
  • 242
  • 387

0 Answers0