Config: Implement custom argparse TextHelpFormatter to keep line return

This commit is contained in:
Benjamin Renard 2021-11-19 16:13:36 +01:00
parent 9c35ead28e
commit e6ce412db0

View file

@ -8,8 +8,10 @@ from getpass import getpass
from logging.config import fileConfig
import logging
import os
import re
import stat
import sys
import textwrap
import argcomplete
import keyring
@ -388,6 +390,25 @@ class ConfigSection:
return '\n'.join(lines)
class RawWrappedTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
def _split_lines(self, text, width):
result = []
for line in textwrap.dedent(text).splitlines():
# Keep empty line
if line == "":
result.append(line)
continue
# Split ident prefix and line text
m = re.match('^( *)(.*)$', line)
ident = m.group(1)
line_text = m.group(2)
# Wrap each lines and add in result with ident prefix
for l in textwrap.wrap(line_text, width - len(ident)):
result.append(ident + l)
return result
class Config:
""" Configuration helper """
@ -543,6 +564,7 @@ class Config:
self.options_parser = argparse.ArgumentParser(
description=kwargs.pop('description', self.appname),
formatter_class=RawWrappedTextHelpFormatter,
**kwargs)
self.options_parser.add_argument(