On Tue, 29 Nov 2022 at 12:37, Loris Bennett wrote:
>
> Mats Wichmann writes:
>
> > On 11/27/22 16:40, Skip Montanaro wrote:
> >> 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
On Sun, 27 Nov 2022 22:23:16 -0600, Karen Park declaimed
the following:
>I figured it out…there was a logistics file given with the assignment! I
>thought it was supposed to be a download included with the python
>download…oops!
>
I think you made this response in the wrong thread...
Mats Wichmann writes:
> On 11/27/22 16:40, Skip Montanaro wrote:
>> 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
>>
Thanks. It occurs to me that instead of providing two special actions
("help" and "version"), it might be worthwhile to provide a standard way of
saying, "if present, process this option and exit before considering other
details of the command line." Matt's example action works well enough for
my n
hon-list on
behalf of Weatherby,Gerard
Date: Sunday, November 27, 2022 at 10:29 PM
To: Skip Montanaro , Python
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
I figured it out…there was a logistics file given with the assignment! I
thought it was supposed to be a download included with the python download…oops!
Thanks,
Karen
> On Nov 27, 2022, at 9:34 PM, Skip Montanaro wrote:
>
>
>>
>>
>> ummm, hate to say this, but have you checked the docu
>
> ummm, hate to say this, but have you checked the documentation? this
> case is supported using an action named 'version' without doing very much.
>
Thanks, Mats.
I actually searched all over the argparse docs. (There's a lot to digest.
Honestly, if I wasn't attempting to be sort of up-to-dat
)
args = parser.parse_args()
# double argument
print(args.positional * 2)
From: Python-list on
behalf of Skip Montanaro
Date: Sunday, November 27, 2022 at 6:42 PM
To: Python
Subject: argparse — adding a --version flag in the face of positional args
*** Attention: This is an external email.
On 11/27/22 16:40, Skip Montanaro wrote:
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 positi
> class VersionAction(argparse.Action):
> def __call__(self, parser, namespace, values, option_string):
> print(VERSION)
> exit()
...
> parser.add_argument("-v", "--version", nargs=0, action=VersionAction)
Thanks. An action class didn't occur to me. I looked briefly at the
code
I wondered whether subparsers might work, but they don't quite fit here.
This seems to fit the bill fairly well, though I agree it would be
nice if there were a neater option:
import argparse
import sys
VERSION = 0.1
def main(args):
parser.parse_args(args)
class VersionAction(argparse.Act
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 somethi
12 matches
Mail list logo