1############################################################################### 2# # 3# This program is free software: you can redistribute it and/or modify # 4# it under the terms of the GNU General Public License as published by # 5# the Free Software Foundation, either version 3 of the License, or # 6# (at your option) any later version. # 7# # 8# This program is distributed in the hope that it will be useful, # 9# but WITHOUT ANY WARRANTY; without even the implied warranty of # 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 11# GNU General Public License for more details. # 12# # 13# You should have received a copy of the GNU General Public License # 14# along with this program. If not, see <http://www.gnu.org/licenses/>. # 15# # 16############################################################################### 17 18__author__ = 'Donovan Parks' 19__copyright__ = 'Copyright 2014' 20__credits__ = ['Donovan Parks'] 21__license__ = 'GPL3' 22__maintainer__ = 'Donovan Parks' 23__email__ = 'donovan.parks@gmail.com' 24 25import argparse 26 27 28class CustomHelpFormatter(argparse.HelpFormatter): 29 """Provide a customized format for help output. 30 31 http://stackoverflow.com/questions/9642692/argparse-help-without-duplicate-allcaps 32 """ 33 34 def _split_lines(self, text, width): 35 return text.splitlines() 36 37 def _get_help_string(self, action): 38 h = action.help 39 if '%(default)' not in action.help: 40 if action.default != '' and action.default != [] and action.default != None and action.default != False: 41 if action.default is not argparse.SUPPRESS: 42 defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE] 43 44 if action.option_strings or action.nargs in defaulting_nargs: 45 if '\n' in h: 46 lines = h.splitlines() 47 lines[0] += ' (default: %(default)s)' 48 h = '\n'.join(lines) 49 else: 50 h += ' (default: %(default)s)' 51 return h 52 53 def _fill_text(self, text, width, indent): 54 return ''.join([indent + line for line in text.splitlines(True)]) 55 56 def _format_action_invocation(self, action): 57 if not action.option_strings: 58 default = self._get_default_metavar_for_positional(action) 59 metavar, = self._metavar_formatter(action, default)(1) 60 return metavar 61 62 else: 63 parts = [] 64 65 # if the Optional doesn't take a value, format is: 66 # -s, --long 67 if action.nargs == 0: 68 parts.extend(action.option_strings) 69 70 # if the Optional takes a value, format is: 71 # -s ARGS, --long ARGS 72 else: 73 default = self._get_default_metavar_for_optional(action) 74 args_string = self._format_args(action, default) 75 for option_string in action.option_strings: 76 parts.append(option_string) 77 78 return '%s %s' % (', '.join(parts), args_string) 79 80 return ', '.join(parts) 81 82 def _get_default_metavar_for_optional(self, action): 83 return action.dest.upper() 84 85 def _get_default_metavar_for_positional(self, action): 86 return action.dest 87