Nach dem Tauchen wirklich tief in argparse Quellcode habe ich konstruiert ein hacken die redundante {cmd1,...}
Auswahlliste zu entfernen.
Der Hack implementiert einen benutzerdefinierten Hilfeformatierer, der die Formatierungsmethoden HelpFormatter
im Umgang mit subparsers Aktion ändert. Insbesondere werden die Unterparser metavar
und help
Zeile in der Unterbefehlsargumentgruppe entfernt und die zusätzliche Einrückung dieser Unterbefehle entfernt.
Bitte vorsichtig verwenden.
Die Python 3-Version getestet, mit python3.6
from argparse import ArgumentParser, HelpFormatter, _SubParsersAction
class NoSubparsersMetavarFormatter(HelpFormatter):
def _format_action(self, action):
result = super()._format_action(action)
if isinstance(action, _SubParsersAction):
# fix indentation on first line
return "%*s%s" % (self._current_indent, "", result.lstrip())
return result
def _format_action_invocation(self, action):
if isinstance(action, _SubParsersAction):
# remove metavar and help line
return ""
return super()._format_action_invocation(action)
def _iter_indented_subactions(self, action):
if isinstance(action, _SubParsersAction):
try:
get_subactions = action._get_subactions
except AttributeError:
pass
else:
# remove indentation
yield from get_subactions()
else:
yield from super()._iter_indented_subactions(action)
parser = ArgumentParser(formatter_class=NoSubparsersMetavarFormatter)
subparsers = parser.add_subparsers(title="Commands")
foo = subparsers.add_parser("foo", help="- foo does foo")
bar = subparsers.add_parser("bar", help="- bar does bar")
parser.parse_args(['-h'])
Die Python 2-Version, getestet mit python2.7
from argparse import ArgumentParser, HelpFormatter, _SubParsersAction
class NoSubparsersMetavarFormatter(HelpFormatter):
def _format_action(self, action):
result = super(NoSubparsersMetavarFormatter,
self)._format_action(action)
if isinstance(action, _SubParsersAction):
return "%*s%s" % (self._current_indent, "", result.lstrip())
return result
def _format_action_invocation(self, action):
if isinstance(action, _SubParsersAction):
return ""
return super(NoSubparsersMetavarFormatter,
self)._format_action_invocation(action)
def _iter_indented_subactions(self, action):
if isinstance(action, _SubParsersAction):
try:
get_subactions = action._get_subactions
except AttributeError:
pass
else:
for subaction in get_subactions():
yield subaction
else:
for subaction in super(NoSubparsersMetavarFormatter,
self)._iter_indented_subactions(action):
yield subaction
parser = ArgumentParser(formatter_class=NoSubparsersMetavarFormatter)
subparsers = parser.add_subparsers(title="Commands")
foo = subparsers.add_parser("foo", help="- foo does foo")
bar = subparsers.add_parser("bar", help="- bar does bar")
parser.parse_args(['-h'])
Beispielausgabe:
usage: a.py [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
Commands:
foo - foo does foo
bar - bar does bar
Perfect, genau was ich brauchte! Vielen Dank. –
Es ist immer noch unmöglich, diesen Header vollständig zu entfernen. 'argparse' verlässt die leere Zeile. –
@anatolytechtonik Siehe meinen Hack unten. – Naitree