New submission from Peter McEldowney <pcm1...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue36664>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to