import argparse class MyFormatter(argparse.HelpFormatter): """ Corrected _max_action_length for the indenting of subactions """ def add_argument(self, action): if action.help is not argparse.SUPPRESS: # find all invocations get_invocation = self._format_action_invocation invocations = [get_invocation(action)] current_indent = self._current_indent for subaction in self._iter_indented_subactions(action): # compensate for the indent that will be added indent_chg = self._current_indent - current_indent added_indent = 'x'*indent_chg invocations.append(added_indent+get_invocation(subaction)) # print('inv', invocations) # update the maximum item length invocation_length = max([len(s) for s in invocations]) action_length = invocation_length + self._current_indent self._action_max_length = max(self._action_max_length, action_length) # add the item to the list self._add_item(self._format_action, [action]) # call class with alternate parameters formatter_class=lambda prog: MyFormatter(prog, max_help_position=40,width=100) parser = argparse.ArgumentParser(formatter_class=formatter_class) parser.add_argument('-l','--long', help='help after long option strings') subparsers = parser.add_subparsers(title="Commands", metavar="") cmd_parser = subparsers.add_parser('long_long_cmd', help='longish command', formatter_class=formatter_class, aliases=['long', 'long_cmd']) # newer arpgarse take aliases sht_parser = subparsers.add_parser('short', help = 'short cmd') args = parser.parse_args(['-h']) """ which produces: 1355:~/mypy$ python3.5 issue25297.py usage: issue25297.py [-h] [-l LONG] ... optional arguments: -h, --help show this help message and exit -l LONG, --long LONG help after long option strings Commands: long_long_cmd (long, long_cmd) longish command short short cmd """