Message340515
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() |
|
Date |
User |
Action |
Args |
2019-04-18 23:30:32 | Peter McEldowney | set | recipients:
+ Peter McEldowney |
2019-04-18 23:30:32 | Peter McEldowney | set | messageid: <1555630232.08.0.518571967107.issue36664@roundup.psfhosted.org> |
2019-04-18 23:30:32 | Peter McEldowney | link | issue36664 messages |
2019-04-18 23:30:31 | Peter McEldowney | create | |
|