Resolve argparse alias back to the original command

2024/10/10 2:17:12

I'm using a subparser/subcommand that has an alias.

I'm using the dest option for the subparser to store the name of the subcommand so I can get it later.

Currently if the subcommand's name is reallyLongName and the alias is r (say) then the dest option stores either reallyLongName or r exactly - whatever I typed in gets stored. This is annoying because I now have to check for the name of the command or any of its aliases in order to identify the command.

Is there a way to get argparse to store the subcommand's name in the dest field in some sort of single, canonical text string?

For example, given the following code:

import argparseparser = argparse.ArgumentParser()subparsers = parser.add_subparsers(dest='command', help='sub-command help')parser_ag = subparsers.add_parser(  'mySubcommand',aliases=['m'],help='Subcommand help')print(parser.parse_args('mySubcommand'.split()))print(parser.parse_args('m'.split()))

the following output is produced:

Namespace(command='mySubcommand')
Namespace(command='m')

Desired result: command has a single, canonical value for both, for example:

Namespace(command='mySubcommand')
Namespace(command='mySubcommand')
Answer

There was a Python bug/issue requesting this - saving the 'base' name, rather than the alias. You can't change that without changing argparse.py code. I think the change would limited to the Action subclass that handles subparsers. https://bugs.python.org/issue36664

But I point out that there's simpler way of handling this. Just use set_defaults as documented near the end of the https://docs.python.org/3/library/argparse.html#sub-commands section. There

parser_foo.set_defaults(func=foo)

is used to set a subparser specific function, but it could just as well be used to set the 'base' name.

parser_foo.set_defaults(name='theIncrediblyLongAlias')
https://en.xdnf.cn/q/69947.html

Related Q&A

Django: How do I make fields non-editable by default in an inline model formset?

I have an inline model formset, and Id like to make fields non-editable if those fields already have values when the page is loaded. If the user clicks an "Edit" button on that row, it would…

How to store result of an operation (like TOPK) per epoch in keras

I have written a custom layer in keras. in part of this custom layer lets say I have a matrix like this: c = tf.cast(tf.nn.top_k(tf.nn.top_k(n, tf.shape(n)[1])[1][:, ::-1], tf.shape(n)[1])[1][:, ::-1],…

How to start Firefox with with specific profile Selenium Python geckodriver

Here is my code:profile = webdriver.FirefoxProfile(C:\\Users\\Administrator\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\kvycjolb.Prdel) driver = webdriver.Firefox(profile)Im not getting any error an…

Beautiful Soup find elements having hidden style

My simple need. How do I find elements that are not visible on the webpage currently? I am guessing style="visibility:hidden" or style="display:none" are simple ways to hide an ele…

Fastest way to extract dictionary of sums in numpy in 1 I/O pass

Lets say I have an array like:arr = np.array([[1,20,5],[1,20,8],[3,10,4],[2,30,6],[3,10,5]])and I would like to form a dictionary of the sum of the third column for each row that matches each value in …

How to group by and dummies in pandas

I have a pandas dataframe: key valA 1A 2B 1B 3C 1C 4I want to get do some dummies like this:A 1100b 1010c 1001

Iterate over a dict except for x item items

I have a dict in this format:d_data = {key_1:value_1,key_2:value_2,key_3:value_3,key_x:value_x,key_n:value_n}and I have to iterate over its items:for key,value in columns.items():do somethingexcept for…

Best way to do a case insensitive replace but match the case of the word to be replaced?

So far Ive come up with the method below but my question is is there a shorter method out there that has the same result?My Code :input_str = "myStrIngFullOfStUfFiWannAReplaCE_StUfFs" …

Given a list of numbers, find all matrices such that each column and row sum up to 264

Lets say I have a list of 16 numbers. With these 16 numbers I can create different 4x4 matrices. Id like to find all 4x4 matrices where each element in the list is used once, and where the sum of each …

How can I access tablet pen data via Python?

I need to access a windows tablet pen data (such as the surface) via Python. I mainly need the position, pressure, and tilt values.I know how to access the Wacom pen data but the windows pen is differe…