From 84d250c1c2e88e9e826b94043cc71c8585ad5224 Mon Sep 17 00:00:00 2001 From: Kay0u Date: Tue, 26 Jan 2021 12:05:25 +0100 Subject: [PATCH] monkey patch _get_action_name --- moulinette/interfaces/cli.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index 11d219b1..9433b116 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -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