Verified Commit d53ae4d9 authored by David Runge's avatar David Runge
Browse files

argparse: Extend to show version and making project optional

Change `ArgParseFactory.__init__()` to allow overriding the program name
add a `-V`/`--version` argument which may be used to trigger showing the
version of the program.
Change `ArgParseFactory.promote()` to replace the required `name`
argument with the optional `-p`/`--project` argument and adding an
optional `-r`/`--release` argument which may be used to override the
release version to promote. Print program name and version information
and exit early if version information is requested.

Change tests to accomodate for the new optional arguments.

Replace the use of `` with `args.project`.
parent 51631209
import argparse
from importlib import metadata
from sys import exit
class ArgParseFactory:
......@@ -11,14 +13,20 @@ class ArgParseFactory:
def __init__(self, description: str = "default") -> None:
self.parser = argparse.ArgumentParser(description=description)
def __init__(self, prog: str = "program", description: str = "default") -> None:
self.parser = argparse.ArgumentParser(prog=prog, description=description)
help="verbose output",
help="version information",
def promote(self) -> argparse.ArgumentParser:
......@@ -30,12 +38,32 @@ class ArgParseFactory:
An ArgumentParser instance specific for promotion
instance = self(description="Download release artifacts from a project and promote them")
instance = self(
description="Download release artifacts from a project and promote them",
"the project on a remote to sign (e.g. 'group/project'). "
f"By default {instance.parser.prog} attempts to promote releases for "
"all projects specified in its config"
help="the project on a remote to sign (e.g. 'group/project')",
"the release of a project to sign (e.g. '0.1.0'). "
f"By default {instance.parser.prog} requires user input to select a release."
if instance.parser.parse_args().version:
print(f"{instance.parser.prog} {metadata.version('arch_release_promotion')}")
return instance.parser
......@@ -60,5 +88,5 @@ class ArgParseFactory:
if len(input_) < 1:
raise argparse.ArgumentTypeError("the provided project name can not be zero")
raise argparse.ArgumentTypeError("the provided string can not be empty")
return input_
......@@ -14,7 +14,7 @@ from arch_release_promotion import (
def main() -> None:
args = argparse.ArgParseFactory.promote().parse_args()
project = config.Projects().get_project(
project = config.Projects().get_project(name=args.project)
settings = config.Settings()
upstream = gitlab.Upstream(
from argparse import ArgumentParser, ArgumentTypeError
from contextlib import nullcontext as does_not_raise
from typing import ContextManager
from unittest.mock import Mock, call, patch
from pytest import mark, raises
......@@ -11,8 +12,16 @@ def test_argparse_argparsefactory() -> None:
assert isinstance(argparse.ArgParseFactory(), argparse.ArgParseFactory)
def test_argparse_promote() -> None:
assert isinstance(argparse.ArgParseFactory().promote(), ArgumentParser)
def test_argparse_promote(metadata_mock: Mock, exit_mock: Mock, parse_args_mock: Mock) -> None:
assert isinstance(argparse.ArgParseFactory.promote(), ArgumentParser)
assert call.version("arch_release_promotion") in metadata_mock.mock_calls
parse_args_mock.return_value = Mock(version=False)
assert isinstance(argparse.ArgParseFactory.promote(), ArgumentParser)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment