Simple feature for argparse.py
Hello all, Aspiring contributor here. I am not at all certain that this is the right place to discuss this. Do refer me to a better location if I'm out of place. I would like to add a simple feature to the argparse library. I frequently find myself writing small utilities, stylistically similar to unix's "rm" whose main parameter(s) get bundled into a list and it doesn't matter if flags are sprinkled throughout. e.g. "rm foo.txt -f bar.txt" removes both foo.txt and bar.txt forcefully. Doing this in argparse currently is cumbersome at best (though I would be happy to be proved wrong on that), so I rarely implement it as a feature. Of note, calling parser.add_argument with nargs="..." or nargs="A..." or nargs="*" gets close, but will not allow you to intermix other flags between your list. I have a working modification of argparse.py that I could commit, but I know there's several steps between here and there - I just don't know what those steps are. Any direction would be very much appreciated. Thanks!
I believe you want Python 3.7's parse_intermixed_args: https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.pars... A quick test seems to work: import argparse p = argparse.ArgumentParser() p.add_argument('files', nargs='*') p.add_argument('-f', '--force', action='store_true') print(p.parse_intermixed_args()) $ python3 rm.py x -f y Namespace(files=['x', 'y'], force=True) $ On Wed, Oct 9, 2019, 9:50 PM brent bejot <brent.bejot@gmail.com> wrote:
Hello all,
Aspiring contributor here. I am not at all certain that this is the right place to discuss this. Do refer me to a better location if I'm out of place.
I would like to add a simple feature to the argparse library. I frequently find myself writing small utilities, stylistically similar to unix's "rm" whose main parameter(s) get bundled into a list and it doesn't matter if flags are sprinkled throughout.
e.g. "rm foo.txt -f bar.txt" removes both foo.txt and bar.txt forcefully.
Doing this in argparse currently is cumbersome at best (though I would be happy to be proved wrong on that), so I rarely implement it as a feature.
Of note, calling parser.add_argument with nargs="..." or nargs="A..." or nargs="*" gets close, but will not allow you to intermix other flags between your list.
I have a working modification of argparse.py that I could commit, but I know there's several steps between here and there - I just don't know what those steps are.
Any direction would be very much appreciated.
Thanks! _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MPWEGV... Code of Conduct: http://python.org/psf/codeofconduct/
On Wed, Oct 9, 2019 at 11:01 PM Ryan Gonzalez <rymg19@gmail.com> wrote:
I believe you want Python 3.7's parse_intermixed_args: https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.pars...
A quick test seems to work:
import argparse
p = argparse.ArgumentParser() p.add_argument('files', nargs='*') p.add_argument('-f', '--force', action='store_true')
print(p.parse_intermixed_args())
$ python3 rm.py x -f y Namespace(files=['x', 'y'], force=True) $
This is EXACTLY what I've been looking for. Looks like it was added in 2017 and release in 3.7. The box I use where I investigated this has 3.6 on it. Sigh, well at least I know it was a good idea! Thanks so much!
On Wed, Oct 9, 2019, 9:50 PM brent bejot <brent.bejot@gmail.com> wrote:
Hello all,
Aspiring contributor here. I am not at all certain that this is the right place to discuss this. Do refer me to a better location if I'm out of place.
I would like to add a simple feature to the argparse library. I frequently find myself writing small utilities, stylistically similar to unix's "rm" whose main parameter(s) get bundled into a list and it doesn't matter if flags are sprinkled throughout.
e.g. "rm foo.txt -f bar.txt" removes both foo.txt and bar.txt forcefully.
Doing this in argparse currently is cumbersome at best (though I would be happy to be proved wrong on that), so I rarely implement it as a feature.
Of note, calling parser.add_argument with nargs="..." or nargs="A..." or nargs="*" gets close, but will not allow you to intermix other flags between your list.
I have a working modification of argparse.py that I could commit, but I know there's several steps between here and there - I just don't know what those steps are.
Any direction would be very much appreciated.
Thanks! _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/MPWEGV... Code of Conduct: http://python.org/psf/codeofconduct/
On Thu, Oct 10, 2019 at 2:37 PM brent bejot <brent.bejot@gmail.com> wrote:
On Wed, Oct 9, 2019 at 11:01 PM Ryan Gonzalez <rymg19@gmail.com> wrote:
I believe you want Python 3.7's parse_intermixed_args: https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.pars...
A quick test seems to work:
import argparse
p = argparse.ArgumentParser() p.add_argument('files', nargs='*') p.add_argument('-f', '--force', action='store_true')
print(p.parse_intermixed_args())
$ python3 rm.py x -f y Namespace(files=['x', 'y'], force=True) $
This is EXACTLY what I've been looking for. Looks like it was added in 2017 and release in 3.7. The box I use where I investigated this has 3.6 on it. Sigh, well at least I know it was a good idea! Thanks so much!
Welp, I learned something new today too :) Thanks Ryan for pointing that out! What you may want to consider is backporting the exact argparse.py from Python 3.7. It's a fairly straight-forward module and will probably work fine on 3.6. That way, once you upgrade to 3.7, it'll keep working the exact same way. ChrisA
On Oct 9, 2019, at 20:56, Chris Angelico <rosuav@gmail.com> wrote:
What you may want to consider is backporting the exact argparse.py from Python 3.7. It's a fairly straight-forward module and will probably work fine on 3.6. That way, once you upgrade to 3.7, it'll keep working the exact same way.
The argparse module on PyPI (which works on 2.3+/3.1+) says it “tries to stay compatible with the module in the standard library, but also supports older Python versions.” So it may already include this feature. And if not, it may be worth submitting a patch to update it to the 3.7 version. That may be trivial, or it may require some complicated rewriting to work with 2.x (in which case it might even be worth creating a separate backport module that requires, say, 3.4+ but is easier to keep in sync with new features).
brent bejot writes:
Sigh, well at least I know it was a good idea! Thanks so much!
As I was telling potential advisees a few minutes ago, it's not getting the right answer that gets you a Nobel Prize, it's asking the right question! You'll find more good ideas, I'm sure. Steve -- Associate Professor Division of Policy and Planning Science http://turnbull.sk.tsukuba.ac.jp/ Faculty of Systems and Information Email: turnbull@sk.tsukuba.ac.jp University of Tsukuba Tel: 029-853-5175 Tennodai 1-1-1, Tsukuba 305-8573 JAPAN
On Thu, Oct 10, 2019 at 1:50 PM brent bejot <brent.bejot@gmail.com> wrote:
Hello all,
Aspiring contributor here. I am not at all certain that this is the right place to discuss this. Do refer me to a better location if I'm out of place.
This is the perfect place to discuss this! Welcome on in.
I would like to add a simple feature to the argparse library. I frequently find myself writing small utilities, stylistically similar to unix's "rm" whose main parameter(s) get bundled into a list and it doesn't matter if flags are sprinkled throughout.
e.g. "rm foo.txt -f bar.txt" removes both foo.txt and bar.txt forcefully.
Doing this in argparse currently is cumbersome at best (though I would be happy to be proved wrong on that), so I rarely implement it as a feature.
Of note, calling parser.add_argument with nargs="..." or nargs="A..." or nargs="*" gets close, but will not allow you to intermix other flags between your list.
I have a working modification of argparse.py that I could commit, but I know there's several steps between here and there - I just don't know what those steps are.
What are the consequences of this change? How does it interact with flags that take arguments (eg if you have "-x spam" in the middle of a list of files)? Does this change affect the way nargs="*" works, or are you creating a slightly different way to do things? This sounds like a useful feature. Next step: More details. :) ChrisA
participants (5)
-
Andrew Barnert
-
brent bejot
-
Chris Angelico
-
Ryan Gonzalez
-
Stephen J. Turnbull