Skip to content
Snippets Groups Projects
Commit 0ea32364 authored by Moul's avatar Moul
Browse files

Move MutuallyExclusiveOption from cli_tools to tools (#330)

parent 2282de8a
No related branches found
No related tags found
1 merge request!216#330: Restructure repository
# 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)
......@@ -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")
......
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment