Skip to content
Snippets Groups Projects
Commit 20e00de3 authored by Moul's avatar Moul
Browse files

Move MutuallyExclusiveOption from cli_tools to tools (#330)

parent 7fc59772
No related branches found
No related tags found
No related merge requests found
# 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 ( ...@@ -31,7 +31,7 @@ from duniterpy.documents import (
) )
from duniterpy.key import SigningKey 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.blockchain import tools as bc_tools
from silkaj.constants import ( from silkaj.constants import (
CENT_MULT_TO_UNIT, CENT_MULT_TO_UNIT,
...@@ -67,7 +67,7 @@ NBR_ISSUERS = 1 ...@@ -67,7 +67,7 @@ NBR_ISSUERS = 1
multiple=True, multiple=True,
type=click.FloatRange(MINIMAL_ABSOLUTE_TX_AMOUNT), type=click.FloatRange(MINIMAL_ABSOLUTE_TX_AMOUNT),
help=f"Quantitative amount(s):\n-a <amount>\nMinimum amount is {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"], mutually_exclusive=["amountsud", "allsources", "file_path"],
) )
@click.option( @click.option(
...@@ -77,14 +77,14 @@ NBR_ISSUERS = 1 ...@@ -77,14 +77,14 @@ NBR_ISSUERS = 1
multiple=True, multiple=True,
type=click.FloatRange(MINIMAL_RELATIVE_TX_AMOUNT), type=click.FloatRange(MINIMAL_RELATIVE_TX_AMOUNT),
help=f"Relative amount(s):\n-d <amount_UD>\nMinimum amount is {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"], mutually_exclusive=["amounts", "allsources", "file_path"],
) )
@click.option( @click.option(
"--allSources", "--allSources",
is_flag=True, is_flag=True,
help="Send all sources to one recipient", help="Send all sources to one recipient",
cls=cli_tools.MutuallyExclusiveOption, cls=tools.MutuallyExclusiveOption,
mutually_exclusive=["amounts", "amountsud", "file_path"], mutually_exclusive=["amounts", "amountsud", "file_path"],
) )
@click.option( @click.option(
...@@ -96,7 +96,7 @@ NBR_ISSUERS = 1 ...@@ -96,7 +96,7 @@ NBR_ISSUERS = 1
Sending to many recipients is possible:\n\ Sending to many recipients is possible:\n\
* With one amount, all will receive the amount\n\ * With one amount, all will receive the amount\n\
* With many amounts (one per recipient)", * With many amounts (one per recipient)",
cls=cli_tools.MutuallyExclusiveOption, cls=tools.MutuallyExclusiveOption,
mutually_exclusive=["file_path"], mutually_exclusive=["file_path"],
) )
@click.option( @click.option(
...@@ -105,7 +105,7 @@ Sending to many recipients is possible:\n\ ...@@ -105,7 +105,7 @@ Sending to many recipients is possible:\n\
"-f", "-f",
help="File’s path containing a list of amounts in absolute or \ help="File’s path containing a list of amounts in absolute or \
relative reference and recipients’ pubkeys", relative reference and recipients’ pubkeys",
cls=cli_tools.MutuallyExclusiveOption, cls=tools.MutuallyExclusiveOption,
mutually_exclusive=["recipients", "amounts", "amountsUD", "allsources"], mutually_exclusive=["recipients", "amounts", "amountsUD", "allsources"],
) )
@click.option("--comment", "-c", default="", help="Comment") @click.option("--comment", "-c", default="", help="Comment")
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
import functools import functools
import sys import sys
from typing import Any
import click
from silkaj.blockchain.tools import get_blockchain_parameters from silkaj.blockchain.tools import get_blockchain_parameters
from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL, GTEST_SYMBOL from silkaj.constants import FAILURE_EXIT_STATUS, G1_SYMBOL, GTEST_SYMBOL
...@@ -31,3 +34,25 @@ def get_currency_symbol() -> str: ...@@ -31,3 +34,25 @@ def get_currency_symbol() -> str:
def message_exit(message: str) -> None: def message_exit(message: str) -> None:
print(message) print(message)
sys.exit(FAILURE_EXIT_STATUS) 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