monkey patch _get_action_name

This commit is contained in:
Kay0u 2021-01-26 12:05:25 +01:00
parent 9077569c22
commit 84d250c1c2
No known key found for this signature in database
GPG key ID: AAFEEB16CFA2AE2D

View file

@ -6,6 +6,39 @@ import getpass
import locale
import logging
from argparse import SUPPRESS
# Monkeypatch _get_action_name function because there is an annoying bug
# Explained here: https://bugs.python.org/issue29298
# Fixed by: https://github.com/python/cpython/pull/3680
# To reproduce the bug, just launch a command line without action
# For example:
# yunohost firewall
# It should display:
# usage: yunohost firewall {list,reload,allow,disallow,upnp,stop} ... [-h]
# yunohost firewall: error: the following arguments are required: {list,reload,allow,disallow,upnp,stop}
# But it display instead:
# Error: unable to parse arguments 'firewall' because: sequence item 0: expected str instance, NoneType found
import argparse
def monkey_get_action_name(argument):
if argument is None:
return None
elif argument.option_strings:
return "/".join(argument.option_strings)
elif argument.metavar not in (None, SUPPRESS):
return argument.metavar
elif argument.dest not in (None, SUPPRESS):
return argument.dest
elif argument.choices:
return "{" + ",".join(argument.choices) + "}"
else:
return None
argparse._get_action_name = monkey_get_action_name
from collections import OrderedDict
from datetime import date, datetime