From c233296dc7b544abb7f2ea950a179000ff90ef6c Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Tue, 12 Dec 2023 18:28:04 +0000 Subject: [PATCH 1/2] print help message on error for subcommands --- bittensor/cli.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/bittensor/cli.py b/bittensor/cli.py index b0e16f9f75..e8090dc2d8 100644 --- a/bittensor/cli.py +++ b/bittensor/cli.py @@ -135,6 +135,20 @@ } +class CLIErrorParser(argparse.ArgumentParser): + """ + Custom ArgumentParser for better error messages. + """ + + def error(self, message): + """ + This method is called when an error occurs. It prints a custom error message. + """ + sys.stderr.write(f"Error: {message}\n") + self.print_help() + sys.exit(2) + + class cli: """ Implementation of the Command Line Interface (CLI) class for the Bittensor protocol. @@ -191,7 +205,7 @@ def __create_parser__() -> "argparse.ArgumentParser": argparse.ArgumentParser: An argument parser object for Bittensor CLI. """ # Define the basic argument parser. - parser = argparse.ArgumentParser( + parser = CLIErrorParser( description=f"bittensor cli v{bittensor.__version__}", usage="btcli ", add_help=True, @@ -283,4 +297,4 @@ def run(self): console.print( f":cross_mark:[red]Unknown command: {self.config.command}[/red]" ) - sys.exit() + sys.exit() \ No newline at end of file From b11a5c1e37c77d2d7f6c40310f93208c788b626e Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Tue, 12 Dec 2023 18:32:09 +0000 Subject: [PATCH 2/2] black --- bittensor/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bittensor/cli.py b/bittensor/cli.py index e8090dc2d8..a8d51fe40c 100644 --- a/bittensor/cli.py +++ b/bittensor/cli.py @@ -297,4 +297,4 @@ def run(self): console.print( f":cross_mark:[red]Unknown command: {self.config.command}[/red]" ) - sys.exit() \ No newline at end of file + sys.exit()