Re: argparse — adding a --version flag in the face of positional args

2022-11-28 Thread Chris Angelico
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 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?
> >
> > ummm, hate to say this, but have you checked the documentation?  this
> > case is supported using an action named 'version' without doing very
> > much.
>
> I hadn't noticed the action 'version'.  I just use
>
> parser.add_argument(
> "-v", "--version", action="store_true", dest="version",
> help="print version"
> )
>

That's still going to validate the rest of the args though - notably,
you can't omit any mandatory arguments (like a subcommand).

The version and help actions are implemented pretty simply, actually.
They're just little action classes that do their action immediately on
getting triggered. It should be easy enough to make any action you
want that way.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-28 Thread Dennis Lee Bieber
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...


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-28 Thread Loris Bennett
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
>> 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?
>
> ummm, hate to say this, but have you checked the documentation?  this
> case is supported using an action named 'version' without doing very
> much.

I hadn't noticed the action 'version'.  I just use

parser.add_argument(
"-v", "--version", action="store_true", dest="version",
help="print version"
)

...

   if args.version:
print(f"Version {my_module.__version__}")
sys.exit(0)

where the version is specified in a pyproj.toml file and __init__.py
contains

try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata

__version__ = importlib_metadata.version(__name__)

I use poetry to then build the corresponding versioned package.

What am I missing by not using the action 'version'?  Do I just save
having to explicitly test for the version arg?

Cheers,

Loris

-- 
This signature is currently under constuction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-28 Thread Skip Montanaro
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 needs, but it would be nice if more than one such option could be given.
For example:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("url")
parser.add_argument("--version", version="777", action="version")

args = parser.parse_args(["--help", "--version"])

Which option is processed depends on their order on the command line. I
don't believe it's possible to run the script and see them both processed.
That's probably a secondary consideration though. My script is working well
enough in this regard now.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-28 Thread Weatherby,Gerard
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  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_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  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. 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!iuJxNp5rr6BjU2VBDXr3OC1kal6NmqPTePUyYJ3K9gvrkpd-O6LrEW77sZ1Km5k3eglgSURIu991H8zLO9n2APmf$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Karen Park
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 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-date I'd just continue
> using getopt.) It never occurred to me that "version" would be special, so
> I didn't specifically search for it, or realize there would be a specific
> action devoted to it. I knew (the default) "help" was special, so I focused
> my search for it. Obviously, "--help" is a pretty bad search term.
> 
> Skip
> 
>> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Skip Montanaro
>
> 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-date I'd just continue
using getopt.) It never occurred to me that "version" would be special, so
I didn't specifically search for it, or realize there would be a specific
action devoted to it. I knew (the default) "help" was special, so I focused
my search for it. Obviously, "--help" is a pretty bad search term.

Skip

>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Weatherby,Gerard
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  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. 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://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Mats Wichmann

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 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?


ummm, hate to say this, but have you checked the documentation?  this 
case is supported using an action named 'version' without doing very much.




--
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Skip Montanaro
> 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 for argparse to see how it handled --help. The added argument
seemed normal, so gave up, figuring there was some special handling of
that option.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Matt Wheeler
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.Action):
def __call__(self, parser, namespace, values, option_string):
print(VERSION)
exit()


parser = argparse.ArgumentParser()
parser.add_argument("-v", "--version", nargs=0, action=VersionAction)
parser.add_argument("pos", nargs=1)


if __name__ == "__main__":
main(sys.argv[1:])

On Sun, 27 Nov 2022 at 23: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 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://mail.python.org/mailman/listinfo/python-list



-- 
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


argparse — adding a --version flag in the face of positional args

2022-11-27 Thread Skip Montanaro
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://mail.python.org/mailman/listinfo/python-list