From dc275d186d48f068dea32c03527f1ed3164483b2 Mon Sep 17 00:00:00 2001 From: dennisvang <29799340+dennisvang@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:27:26 +0100 Subject: [PATCH] add --version option to CLI --- src/tufup/__init__.py | 8 +++++++- src/tufup/repo/cli.py | 4 ++++ tests/test_repo_cli.py | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/tufup/__init__.py b/src/tufup/__init__.py index 9056471..65520ea 100644 --- a/src/tufup/__init__.py +++ b/src/tufup/__init__.py @@ -11,7 +11,9 @@ def main(args=None): - print(f'tufup version: {__version__}') + # show version before anything else + print(f'tufup {__version__}') + # default to --help if args is None: args = sys.argv[1:] or ['--help'] @@ -19,6 +21,10 @@ def main(args=None): # parse command line arguments options = cli.get_parser().parse_args(args=args) + # exit if version is requested (printed above) + if options.version: + return + # cli debugging if options.debug: logging.basicConfig(level=logging.DEBUG, stream=sys.stdout, force=True) diff --git a/src/tufup/repo/cli.py b/src/tufup/repo/cli.py index f5f5953..b7fa6ba 100644 --- a/src/tufup/repo/cli.py +++ b/src/tufup/repo/cli.py @@ -11,6 +11,7 @@ logger = logging.getLogger(__name__) HELP = dict( + version='show version and exit', common_key_dirs='Directories to search for private and/or public keys.', targets_add=( 'Add app bundle to the repository. Creates archive and patch from' @@ -64,6 +65,9 @@ def get_parser() -> argparse.ArgumentParser: # https://docs.python.org/3/library/argparse.html#sub-commands # https://docs.python.org/3/library/argparse.html#parents parser = argparse.ArgumentParser() + parser.add_argument( + '-v', '--version', action='store_true', required=False, help=HELP['version'] + ) subparsers = parser.add_subparsers() # add debug option debug_parser = argparse.ArgumentParser(add_help=False) diff --git a/tests/test_repo_cli.py b/tests/test_repo_cli.py index 9812e4a..73d79d3 100644 --- a/tests/test_repo_cli.py +++ b/tests/test_repo_cli.py @@ -13,6 +13,7 @@ class ParserTests(unittest.TestCase): def test_get_parser(self): parser = tufup.repo.cli.get_parser() for cmd in [ + '--version', 'init', 'init --debug', 'targets add 1.0 c:\\my_bundle_dir c:\\private_keys', @@ -38,7 +39,11 @@ def test_get_parser(self): self.assertTrue(hasattr(options, 'subcommand')) if args[:2] == ['targets', 'add']: self.assertTrue(hasattr(options, 'meta')) - self.assertEqual(expected_func_name, options.func.__name__) + if args[0] == '--version': + self.assertTrue(options.version) + else: + self.assertFalse(options.version) + self.assertEqual(expected_func_name, options.func.__name__) def test_get_parser_incomplete_commands(self): parser = tufup.repo.cli.get_parser()