import argparse def main(formatter): parent_parser = argparse.ArgumentParser(add_help=False, formatter_class=formatter) main_parser = argparse.ArgumentParser(formatter_class=formatter) service_subparsers = main_parser.add_subparsers(title="service", metavar='CMD', dest="service_command", help='command to use') service_parser = service_subparsers.add_parser("add", help="add something", parents=[parent_parser]) action_subparser = service_parser.add_subparsers(title="action", dest="action_command") action_parser = action_subparser.add_parser("file", help="file", parents=[parent_parser]) service_parser2 = service_subparsers.add_parser("remove", help="remove something", parents=[parent_parser]) action_subparser2 = service_parser2.add_subparsers(title="action", dest="action_command") action_parser2 = action_subparser2.add_parser("file", help="file", parents=[parent_parser]) service_parser2 = service_subparsers.add_parser("a-very-long-command", help="command that does something", parents=[parent_parser]) action_subparser2 = service_parser2.add_subparsers(title="action", dest="action_command") action_parser2 = action_subparser2.add_parser("file", help="file", parents=[parent_parser]) main_parser.parse_args() if __name__ == '__main__': main(lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=30))