Add extend_const action to argparse
Forgive me if this idea has been discussed before, I searched the mailing lists, the CPython repo, and the issue tracker and was unable to find anything. I have found myself a few times in a position where I have a repeated argument that uses the `append` action, along with some convenience arguments that append a specific const to that same dest (eg: `--filter-x` being made equivalent to `--filter x` via `append_const`). This is particularly useful in cli apps that expose some kind of powerful-but-verbose filtering capability, while also providing shorter aliases for common invocations. I'm sure there are other use cases, but this is the one I'm most familiar with. The natural extension to this filtering idea are convenience args that set two const values (eg: `--filter x --filter y` being equivalent to `--filter-x-y`), but there is no `extend_const` action to enable this. While this is possible (and rather straight forward) to add via a custom action, I feel like this should be a built-in action instead. `append` has `append_const`, it seems intuitive and reasonable to expect `extend` to have `extend_const` too (my anecdotal experience the first time I came across this need was that I simply tried using `extend_const` without checking the docs, assuming it already existed). Please see this gist for a working example that may help explain the idea and intended use case more clearly: https://gist.github.com/roganartu/7c2ec129d868ecda95acfbd655ef0ab2
On Apr 22, 2020, at 15:04, python@roganartu.com wrote:
The natural extension to this filtering idea are convenience args that set two const values (eg: `--filter x --filter y` being equivalent to `--filter-x-y`), but there is no `extend_const` action to enable this.
While this is possible (and rather straight forward) to add via a custom action, I feel like this should be a built-in action instead. `append` has `append_const`, it seems intuitive and reasonable to expect `extend` to have `extend_const` too (my anecdotal experience the first time I came across this need was that I simply tried using `extend_const` without checking the docs, assuming it already existed).
I’m pretty sure I’ve run into the exact same situation (well, not accumulating filters, but accumulating something and wanting to add multiple constants from one flag), had the same “Really? It’s not there?” reaction as you, and then just muttered and worked around it. It makes sense to me to fix it, exactly the way you propose. My only comment is that when you write the example(s) for the docs, it might be worth using a tuple rather than a list for the const value. It doesn’t really make a difference, but people might be momentarily confused by a mutable list called “const”. Also, looking at the _copy_items function you’re calling: it has a comment saying it’s only used by append and append_const, but that’s wrong as it’s also used by extend. And of course you’re adding extend_const. I don’t know if that’s worth fixing separately, but if not it seems to me it’s probably worth fixing in your patch.
I finally got around to submitting a PR for this. Linking here to close the loop should anyone stumble across this thread in future. https://bugs.python.org/issue43160
participants (3)
-
Andrew Barnert
-
python@roganartu.com
-
Tony Lykke