[New-bugs-announce] [issue36664] argparse: parser aliases in subparsers stores alias in dest variable

Peter McEldowney report at bugs.python.org
Thu Apr 18 19:30:32 EDT 2019


New submission from Peter McEldowney <pcm1289 at gmail.com>:

I noticed that I have to add a lot more code to handle contexts in subparsers that I was expecting would be necessary. This is something I feel should be handled by the argparse library. What are your thoughts on this?


If you run the sample code with the commands below, you can see that although I would want them to do the same thing, I have to add more lines into my code to achieve this. This becomes cumbersome/annoying when dealing with subparser trees.

python3 sample.py subsection
python3 sample.py s


Sample code (also attached):

import argparse

def get_args(args=None):
	parser = argparse.ArgumentParser()
	subparser = parser.add_subparsers(dest='context')
	
	sub = subparser.add_parser('subsection', aliases=['s', 'sub', 'subsect'])
	
	return parser.parse_args(args)
	
def my_subsection_function(args):
	print('my subsection was called')
	
def invalid_context(args):
	print('my functon was not called <sadface>')

def main(args=get_args()):	
	return {
		'subsection': my_subsection_function
	}.get(args.context, invalid_context)(args)

if __name__ == "__main__":
	main()

----------
components: Library (Lib)
files: sample.py
messages: 340515
nosy: Peter McEldowney
priority: normal
severity: normal
status: open
title: argparse: parser aliases in subparsers stores alias in dest variable
type: enhancement
versions: Python 3.7
Added file: https://bugs.python.org/file48275/sample.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36664>
_______________________________________


More information about the New-bugs-announce mailing list