Skip to content

Commit

Permalink
add --version option to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisvang committed Mar 12, 2024
1 parent 58f8294 commit dc275d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/tufup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@


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']

# 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)
Expand Down
4 changes: 4 additions & 0 deletions src/tufup/repo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_repo_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
Expand Down

0 comments on commit dc275d1

Please sign in to comment.