This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Peter McEldowney
Recipients Peter McEldowney
Date 2019-04-18.23:30:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1555630232.08.0.518571967107.issue36664@roundup.psfhosted.org>
In-reply-to
Content
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()
History
Date User Action Args
2019-04-18 23:30:32Peter McEldowneysetrecipients: + Peter McEldowney
2019-04-18 23:30:32Peter McEldowneysetmessageid: <1555630232.08.0.518571967107.issue36664@roundup.psfhosted.org>
2019-04-18 23:30:32Peter McEldowneylinkissue36664 messages
2019-04-18 23:30:31Peter McEldowneycreate