More better:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("positional",type=int)
parser.add_argument('--version',action="version",version="2.0")
args = parser.parse_args()
# double argument
print(args.positional * 2)
From: Python-list <[email protected]> on
behalf of Weatherby,Gerard <[email protected]>
Date: Sunday, November 27, 2022 at 10:29 PM
To: Skip Montanaro <[email protected]>, Python <[email protected]>
Subject: Re: argparse — adding a --version flag in the face of positional args
Use two parsers:
import argparse
import sys
vparser = argparse.ArgumentParser(add_help=False)
vparser.add_argument('--version',action="store_true",help="show version")
# look for version, ignore remaining arguments
vargs, _ = vparser.parse_known_args()
if vargs.version:
print("Version 2.0")
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument("positional",type=int)
# add version again, so it displays if --help called
parser.add_argument('--version',action="store_true",help="show version")
args = parser.parse_args()
# double argument
print(args.positional * 2)
From: Python-list <[email protected]> on
behalf of Skip Montanaro <[email protected]>
Date: Sunday, November 27, 2022 at 6:42 PM
To: Python <[email protected]>
Subject: argparse — adding a --version flag in the face of positional args
*** Attention: This is an external email. Use caution responding, opening
attachments or clicking on links. ***
I have a script to which I'd like to add a --version flag. It should print
the version number then exit, much in the same way --help prints the help
text then exits. I haven't been able to figure that out. I always get a
complaint about the required positional argument.
I think I could use something like nargs='*', but that would push off
detection of the presence of the positional arg to the application.
Shouldn't I be able to tell argparse I'm going to process --verbose, then
exit?
Thx,
Skip
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!k-JSWNRKr8fNARGIFw3z_eh_Kv0ouXZKTDEQfWplA3Y3yrLUl81TmbNLiuDiXGOjgXcmNFPOqU2Ldmsh1VCLvLsxBas$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!k-JSWNRKr8fNARGIFw3z_eh_Kv0ouXZKTDEQfWplA3Y3yrLUl81TmbNLiuDiXGOjgXcmNFPOqU2Ldmsh1VCLvLsxBas$><https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!k-JSWNRKr8fNARGIFw3z_eh_Kv0ouXZKTDEQfWplA3Y3yrLUl81TmbNLiuDiXGOjgXcmNFPOqU2Ldmsh1VCLvLsxBas$%3chttps:/urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!k-JSWNRKr8fNARGIFw3z_eh_Kv0ouXZKTDEQfWplA3Y3yrLUl81TmbNLiuDiXGOjgXcmNFPOqU2Ldmsh1VCLvLsxBas$%3e>
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iuJxNp5rr6BjU2VBDXr3OC1kal6NmqPTePUyYJ3K9gvrkpd-O6LrEW77sZ1Km5k3eglgSURIu991H8zLO9n2APmf$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!iuJxNp5rr6BjU2VBDXr3OC1kal6NmqPTePUyYJ3K9gvrkpd-O6LrEW77sZ1Km5k3eglgSURIu991H8zLO9n2APmf$>
--
https://mail.python.org/mailman/listinfo/python-list