diff --git a/silkaj/cli_tools.py b/silkaj/cli_tools.py deleted file mode 100644 index 2268e58d4ed9ffb2b85c7ead40cefd2c54023e05..0000000000000000000000000000000000000000 --- a/silkaj/cli_tools.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2016-2022 Maël Azimi <m.a@moul.re> -# -# Silkaj is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Silkaj is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. - -from typing import Any - -from click import Context, Option, UsageError - - -# pylint: disable=too-few-public-methods -class MutuallyExclusiveOption(Option): - def __init__(self, *args: Any, **kwargs: Any) -> None: - self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) - _help = kwargs.get("help", "") - if self.mutually_exclusive: - ex_str = ", ".join(self.mutually_exclusive) - kwargs[ - "help" - ] = f"{_help} NOTE: This argument is mutually exclusive with arguments: [{ex_str}]." - super().__init__(*args, **kwargs) - - def handle_parse_result(self, ctx: Context, opts: Any, args: Any) -> Any: - if self.mutually_exclusive.intersection(opts) and self.name in opts: - arguments = ", ".join(self.mutually_exclusive) - raise UsageError( - f"Usage: `{self.name}` is mutually exclusive with arguments `{arguments}`." - ) - - return super().handle_parse_result(ctx, opts, args) diff --git a/silkaj/money/transfer.py b/silkaj/money/transfer.py index c1b89059949c377e077fe3a18d702e38a66c2209..e7af1ab6d0b9c6b86735215fccd44575b89e1a3c 100644 --- a/silkaj/money/transfer.py +++ b/silkaj/money/transfer.py @@ -31,7 +31,7 @@ from duniterpy.documents import ( ) from duniterpy.key import SigningKey -from silkaj import auth, cli_tools, network, public_key, tools, tui +from silkaj import auth, network, public_key, tools, tui from silkaj.blockchain import tools as bc_tools from silkaj.constants import ( CENT_MULT_TO_UNIT, @@ -67,7 +67,7 @@ NBR_ISSUERS = 1 multiple=True, type=click.FloatRange(MINIMAL_ABSOLUTE_TX_AMOUNT), help=f"Quantitative amount(s):\n-a <amount>\nMinimum amount is {MINIMAL_ABSOLUTE_TX_AMOUNT}.", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amountsud", "allsources", "file_path"], ) @click.option( @@ -77,14 +77,14 @@ NBR_ISSUERS = 1 multiple=True, type=click.FloatRange(MINIMAL_RELATIVE_TX_AMOUNT), help=f"Relative amount(s):\n-d <amount_UD>\nMinimum amount is {MINIMAL_RELATIVE_TX_AMOUNT}", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "allsources", "file_path"], ) @click.option( "--allSources", is_flag=True, help="Send all sources to one recipient", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["amounts", "amountsud", "file_path"], ) @click.option( @@ -96,7 +96,7 @@ NBR_ISSUERS = 1 Sending to many recipients is possible:\n\ * With one amount, all will receive the amount\n\ * With many amounts (one per recipient)", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["file_path"], ) @click.option( @@ -105,7 +105,7 @@ Sending to many recipients is possible:\n\ "-f", help="File’s path containing a list of amounts in absolute or \ relative reference and recipients’ pubkeys", - cls=cli_tools.MutuallyExclusiveOption, + cls=tools.MutuallyExclusiveOption, mutually_exclusive=["recipients", "amounts", "amountsUD", "allsources"], ) @click.option("--comment", "-c", default="", help="Comment") diff --git a/silkaj/tools.py b/silkaj/tools.py index 99b31f2f34a3a65a550c1ba6d33e0129f0660bde..1a5c810c7461fa7a7612c27558264bb490efbaee 100644 --- a/silkaj/tools.py +++ b/silkaj/tools.py @@ -15,6 +15,9 @@ import functools import sys +from typing import Any + +import click from silkaj.blockchain.tools import get_blockchain_parameters from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL, GTEST_SYMBOL @@ -31,3 +34,25 @@ def get_currency_symbol() -> str: def message_exit(message: str) -> None: print(message) sys.exit(FAILURE_EXIT_STATUS) + + +# pylint: disable=too-few-public-methods +class MutuallyExclusiveOption(click.Option): + def __init__(self, *args: Any, **kwargs: Any) -> None: + self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", [])) + _help = kwargs.get("help", "") + if self.mutually_exclusive: + ex_str = ", ".join(self.mutually_exclusive) + kwargs[ + "help" + ] = f"{_help} NOTE: This argument is mutually exclusive with arguments: [{ex_str}]." + super().__init__(*args, **kwargs) + + def handle_parse_result(self, ctx: click.Context, opts: Any, args: Any) -> Any: + if self.mutually_exclusive.intersection(opts) and self.name in opts: + arguments = ", ".join(self.mutually_exclusive) + raise click.UsageError( + f"Usage: `{self.name}` is mutually exclusive with arguments `{arguments}`." + ) + + return super().handle_parse_result(ctx, opts, args)