I would like to make sure at least one of the sub commands is selected. But there is no required option for add_subparsers() how can I enforce at least one subparser is selected?
Currently I did this to mimic the effect:
subparsers = parser.add_subparsers( title='sub commands', help='valid sub commands', ) subparser1 = subparsers.add_parser('subcmd1') subparser1.set_defaults(which_subcmd='subcmd1') subparser2 = subparsers.add_parser('subcmd2') subparser2.set_defaults(which_subcmd='subcmd2') parsedargs = parser.parse_args() if 'which_subcmd' not in parsedargs: parser.print_help() But I want an official way to do this and make the help content display something like {subcmd1 | subcmd2}
Update:
according to @hpaulj, in 3.7 there is required option. But I want some work around can work in python 3.5 and 3.6
requiredparameter, stackoverflow.com/questions/23349349/…destas well.