From 6e29f7e99bf5d34b8ab2dbeeecc259e6a12af8be Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Fri, 25 Jan 2019 19:58:43 -0500 Subject: [PATCH] user is shown help when typing lbrynet help --- lbrynet/extras/cli.py | 10 +++++++-- tests/unit/test_cli.py | 47 +++++++++++++----------------------------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/lbrynet/extras/cli.py b/lbrynet/extras/cli.py index 3bb32333f..96ff1b761 100644 --- a/lbrynet/extras/cli.py +++ b/lbrynet/extras/cli.py @@ -141,6 +141,10 @@ class ArgumentParser(argparse.ArgumentParser): self, action, "Commands", lambda parser: 'group' not in parser._defaults ) + def error(self, message): + self.print_help(argparse._sys.stderr) + self.exit(2, '\n'+message+'\n') + class HelpFormatter(argparse.HelpFormatter): @@ -164,7 +168,9 @@ def add_command_parser(parent, command): def get_argument_parser(): - main = ArgumentParser('lbrynet') + main = ArgumentParser( + 'lbrynet', description='An interface to the LBRY Network.' + ) main.add_argument( '-v', '--version', dest='cli_version', action="store_true", help='Show lbrynet CLI version and exit.' @@ -175,7 +181,7 @@ def get_argument_parser(): start = sub.add_parser( 'start', usage='lbrynet start [--config FILE] [--data-dir DIR] [--wallet-dir DIR] [--download-dir DIR] ...', - help='Start lbrynet API server.' + help='Start lbrynet network interface.' ) start.add_argument( '--quiet', dest='quiet', action="store_true", diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 5cf643cc6..d713ecee6 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -55,48 +55,29 @@ class CLITest(AsyncioTestCase): self.assertEqual(3, normalize_value('3', key="some_other_thing")) def test_help(self): - self.assertIn( - 'Usage: lbrynet [-v] [--api HOST:PORT]', self.shell(['--help']) - ) + self.assertIn('lbrynet [-v] [--api HOST:PORT]', self.shell(['--help'])) # start is special command, with separate help handling - self.assertIn( - '--share-usage-data', self.shell(['start', '--help']) - ) + self.assertIn('--share-usage-data', self.shell(['start', '--help'])) # publish is ungrouped command, returns usage only implicitly - self.assertIn( - 'publish ( | --name=)', self.shell(['publish']) - ) + self.assertIn('publish ( | --name=)', self.shell(['publish'])) # publish is ungrouped command, with explicit --help - self.assertIn( - 'Make a new name claim and publish', self.shell(['publish', '--help']) - ) + self.assertIn('Make a new name claim and publish', self.shell(['publish', '--help'])) # account is a group, returns help implicitly - self.assertIn( - 'Return the balance of an account', - self.shell(['account']) - ) + self.assertIn('Return the balance of an account', self.shell(['account'])) # account is a group, with explicit --help - self.assertIn( - 'Return the balance of an account', - self.shell(['account', '--help']) - ) + self.assertIn('Return the balance of an account', self.shell(['account', '--help'])) # account add is a grouped command, returns usage implicitly - self.assertIn( - 'account_add ( | --account_name=)', - self.shell(['account', 'add']) - ) + self.assertIn('account_add ( | --account_name=)', self.shell(['account', 'add'])) # account add is a grouped command, with explicit --help - self.assertIn( - 'Add a previously created account from a seed,', self.shell(['account', 'add', '--help']) - ) + self.assertIn('Add a previously created account from a seed,', self.shell(['account', 'add', '--help'])) + + def test_help_error_handling(self): + # person tries `help` command, then they get help even though that's invalid command + self.assertIn('--config FILE', self.shell(['help'])) # help for invalid command, with explicit --help - self.assertIn( - "invalid choice: 'publish1'", self.shell(['publish1', '--help']) - ) + self.assertIn('--config FILE', self.shell(['nonexistant', '--help'])) # help for invalid command, implicit - self.assertIn( - "invalid choice: 'publish1'", self.shell(['publish1']) - ) + self.assertIn('--config FILE', self.shell(['nonexistant'])) def test_version_command(self): self.assertEqual(