Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion multiversx_sdk_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _do_main(cli_args: list[str]):
parser = setup_parser(cli_args)
argcomplete.autocomplete(parser)

_handle_log_level_argument(cli_args)
_handle_verbose_argument(cli_args)
args = parser.parse_args(cli_args)

Expand All @@ -65,8 +66,9 @@ def _do_main(cli_args: list[str]):
handlers=[RichHandler(show_time=False, rich_tracebacks=True)],
)
else:
level: str = args.log_level
logging.basicConfig(
level="INFO",
level=level.upper(),
format="%(name)s: %(message)s",
handlers=[RichHandler(show_time=False, rich_tracebacks=True)],
)
Expand Down Expand Up @@ -112,6 +114,13 @@ def setup_parser(args: list[str]):
version=f"MultiversX Python CLI (mxpy) {version}",
)
parser.add_argument("--verbose", action="store_true", default=False)
parser.add_argument(
"--log-level",
type=str,
default=config.get_log_level_from_config(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

choices=["debug", "info", "warning", "error"],
help="default: %(default)s",
)

subparsers = parser.add_subparsers()
commands: list[Any] = []
Expand Down Expand Up @@ -166,6 +175,17 @@ def _handle_verbose_argument(args: list[str]):
args.insert(0, verbose_arg)


def _handle_log_level_argument(args: list[str]):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have handled both --verbose and --log-level in a single function of the kind e.g. _handle_global_arguments or _handle_universal_arguments. Just an opinion.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, moved the logic in _handle_global_arguments().

log_level_arg = "--log-level"

if log_level_arg in args:
index = args.index(log_level_arg)
Copy link

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a safety check to ensure that a value follows '--log-level' in the args list to prevent potential index errors if the argument is provided without an accompanying value.

Suggested change
index = args.index(log_level_arg)
index = args.index(log_level_arg)
if index + 1 >= len(args) or args[index + 1].startswith("--"):
raise ValueError(f"Argument '{log_level_arg}' must be followed by a log level value.")

Copilot uses AI. Check for mistakes.
log_arg = args.pop(index)
log_value = args.pop(index)
args.insert(0, log_value)
args.insert(0, log_arg)


if __name__ == "__main__":
ret = main(sys.argv[1:])
sys.exit(ret)
9 changes: 9 additions & 0 deletions multiversx_sdk_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ def get_defaults() -> dict[str, Any]:
"dependencies.testwallets.urlTemplate.osx": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz",
"dependencies.testwallets.urlTemplate.windows": "https://github.com/multiversx/mx-sdk-testwallets/archive/{TAG}.tar.gz",
"github_api_token": "",
"log_level": "info",
}


def get_log_level_from_config():
log_level = get_value("log_level")
if log_level not in ["debug", "info", "warning", "error"]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List could have been in constants.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

raise errors.LogLevelError(log_level)

return log_level


def get_deprecated_entries_in_config_file():
default_config_keys = set(get_defaults().keys())
current_config_keys = set(get_active().keys())
Expand Down
5 changes: 5 additions & 0 deletions multiversx_sdk_cli/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,8 @@ def __init__(self, message: str):
class NetworkProviderError(KnownError):
def __init__(self, url: str, error: str):
super().__init__(f"Url = [{url}], error = {error}")


class LogLevelError(KnownError):
def __init__(self, log_level: str):
super().__init__(f"Log level not accepted: {log_level}. Choose between ['debug', 'info', 'warning', 'error'].")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List could have been in constants.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Loading