diff options
Diffstat (limited to '__init__.py')
-rw-r--r-- | __init__.py | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/__init__.py b/__init__.py index 27847d1..0796ac7 100644 --- a/__init__.py +++ b/__init__.py @@ -1,8 +1,32 @@ # coding: utf-8 + +def _convert_version(tup): + """create a PEP 386 pseudo-format conformant string from tuple tup""" + ret_val = str(tup[0]) # first is always digit + next_sep = "." # separator for next extension, can be "" or "." + for x in tup[1:]: + if isinstance(x, int): + ret_val += next_sep + str(x) + next_sep = '.' + continue + first_letter = x[0].lower() + next_sep = '' + if first_letter in 'abcr': + ret_val += 'rc' if first_letter == 'r' else first_letter + elif first_letter in 'pd': + ret_val += '.post' if first_letter == 'p' else '.dev' + return ret_val + +version_info = (0, 2, "alpha", 1) +__version__ = _convert_version(version_info) + +del _convert_version + import argparse from argparse import ArgumentParser + class SubParsersAction(argparse._SubParsersAction): """support aliases, based on differences of 3.3 and 2.7 """ @@ -17,13 +41,15 @@ class SubParsersAction(argparse._SubParsersAction): metavar=metavar) def add_parser(self, name, **kwargs): - # remove aliases and help kwargs so orginal add_parser does not get them + # remove aliases and help kwargs so the orginal add_parser + # does not get them aliases = kwargs.pop('aliases', ()) help = kwargs.pop('help', None) parser = argparse._SubParsersAction.add_parser(self, name, **kwargs) if help is not None: - choice_action = self._AliasesChoicesPseudoAction(name, aliases, help) + choice_action = self._AliasesChoicesPseudoAction(name, aliases, + help) self._choices_actions.append(choice_action) if aliases is not None: for alias in aliases: @@ -34,3 +60,41 @@ class SubParsersAction(argparse._SubParsersAction): from _action.checksinglestore import CheckSingleStoreAction from _action.count import CountAction from _action.splitappend import SplitAppendAction + + +class SmartFormatter(argparse.HelpFormatter): + """ + you can only specify one formatter in standard argparse, so you cannot + both have pre-formatted description (RawDescriptionHelpFormatter) + and ArgumentDefaultsHelpFormatter. + The SmartFormatter has sensible defaults (RawDescriptionFormatter) and + the individual help text can be marked ( help="R|" ) for + variations in formatting. + """ + def __init__(self, *args, **kw): + self._add_defaults = False + super(SmartFormatter, self).__init__(*args, **kw) + + def _fill_text(self, text, width, indent): + return ''.join([indent + line for line in text.splitlines(True)]) + + + def _split_lines(self, text, width): + #print 'TEXT', text + if text.startswith('D|'): + self._add_defaults = True + text = text[2:] + if text.startswith('R|'): + return text[2:].splitlines() + return argparse.HelpFormatter._split_lines(self, text, width) + + def _get_help_string(self, action): + if not self._add_defaults: + return argparse.HelpFormatter._get_help_string(self, action) + help = action.help + if '%(default)' not in action.help: + if action.default is not argparse.SUPPRESS: + defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE] + if action.option_strings or action.nargs in defaulting_nargs: + help += ' (default: %(default)s)' + return help |