From d5f71bdb9640517fb49129a32f22de491a00b9bb Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 30 Aug 2023 22:02:16 +0200 Subject: [PATCH 1/4] Allow to provide the version to antsibull_core.args.get_toplevel_parser. --- changelogs/fragments/96-version.yml | 2 ++ src/antsibull_core/args.py | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/96-version.yml diff --git a/changelogs/fragments/96-version.yml b/changelogs/fragments/96-version.yml new file mode 100644 index 0000000..5d73d60 --- /dev/null +++ b/changelogs/fragments/96-version.yml @@ -0,0 +1,2 @@ +minor_changes: + - "Allow to overwrite the version when using ``antsibull_core.args.get_toplevel_parser()`` (https://github.com/ansible-community/antsibull-core/pull/96)." diff --git a/src/antsibull_core/args.py b/src/antsibull_core/args.py index af4bb3e..0ee937f 100644 --- a/src/antsibull_core/args.py +++ b/src/antsibull_core/args.py @@ -16,20 +16,27 @@ class InvalidArgumentError(Exception): """A problem parsing or validating a command line argument.""" -def get_toplevel_parser(package, **kwargs) -> argparse.ArgumentParser: +def get_toplevel_parser( + package: str, *, package_version: str | None = None, **kwargs +) -> argparse.ArgumentParser: """ Create a toplevel argument parser with options common across all scripts. :arg package: The Python package containing this CLI program. + :arg package_version: If provided, use this instead of the version for ``package`` + provided by ``importlib.metadata.version()`` :args kwargs: This function takes any keyword arguments and passes them directly on to the :class:`argparse.ArgumentParser` constructor. :returns: :class:`argparse.ArgumentParser` with common script arguments added. """ - try: - version = metadata.version(package) - except metadata.PackageNotFoundError: - # If there's no metadata foun, assume we're running from source - version = "source" + if package_version is None: + try: + version = metadata.version(package) + except metadata.PackageNotFoundError: + # If there's no metadata foun, assume we're running from source + version = "source" + else: + version = package_version toplevel_parser = argparse.ArgumentParser(**kwargs) toplevel_parser.add_argument( From d87c4996b98eb8daf92b19d518065e1779e5859a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 31 Aug 2023 06:24:56 +0200 Subject: [PATCH 2/4] Simplify code. Also allow to override the program name 'antsibull'. --- changelogs/fragments/96-version.yml | 2 +- src/antsibull_core/args.py | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/changelogs/fragments/96-version.yml b/changelogs/fragments/96-version.yml index 5d73d60..4fd8daa 100644 --- a/changelogs/fragments/96-version.yml +++ b/changelogs/fragments/96-version.yml @@ -1,2 +1,2 @@ minor_changes: - - "Allow to overwrite the version when using ``antsibull_core.args.get_toplevel_parser()`` (https://github.com/ansible-community/antsibull-core/pull/96)." + - "Allow to overwrite the version and the program name when using ``antsibull_core.args.get_toplevel_parser()`` (https://github.com/ansible-community/antsibull-core/pull/96)." diff --git a/src/antsibull_core/args.py b/src/antsibull_core/args.py index 0ee937f..cb8012b 100644 --- a/src/antsibull_core/args.py +++ b/src/antsibull_core/args.py @@ -17,33 +17,41 @@ class InvalidArgumentError(Exception): def get_toplevel_parser( - package: str, *, package_version: str | None = None, **kwargs + package: str, + *, + package_version: str | None = None, + program_name: str | None = None, + **kwargs, ) -> argparse.ArgumentParser: """ Create a toplevel argument parser with options common across all scripts. :arg package: The Python package containing this CLI program. :arg package_version: If provided, use this instead of the version for ``package`` - provided by ``importlib.metadata.version()`` + provided by ``importlib.metadata.version()``. + :arg program_name: If provided, show a more concrete description for this program. :args kwargs: This function takes any keyword arguments and passes them directly on to the :class:`argparse.ArgumentParser` constructor. :returns: :class:`argparse.ArgumentParser` with common script arguments added. """ if package_version is None: try: - version = metadata.version(package) + package_version = metadata.version(package) except metadata.PackageNotFoundError: # If there's no metadata foun, assume we're running from source - version = "source" - else: - version = package_version + package_version = "source" + + if program_name is None: + program_name = package toplevel_parser = argparse.ArgumentParser(**kwargs) toplevel_parser.add_argument( "--version", action="version", - version=version, - help="Print the antsibull version", + version=package_version, + help=f"Print the {program_name} version" + if program_name + else "Print the program's version", ) toplevel_parser.add_argument( "--config-file", From 80fddc857ee38388216205542b4b2ff84d6ebe90 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 31 Aug 2023 07:57:42 +0200 Subject: [PATCH 3/4] Remove fallback. --- src/antsibull_core/args.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/antsibull_core/args.py b/src/antsibull_core/args.py index cb8012b..9091c6d 100644 --- a/src/antsibull_core/args.py +++ b/src/antsibull_core/args.py @@ -41,9 +41,6 @@ def get_toplevel_parser( # If there's no metadata foun, assume we're running from source package_version = "source" - if program_name is None: - program_name = package - toplevel_parser = argparse.ArgumentParser(**kwargs) toplevel_parser.add_argument( "--version", From 5be4c3437ade91401d85ba9eae9f070df60cf1d7 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 1 Sep 2023 22:23:32 +0200 Subject: [PATCH 4/4] Fix typo. Co-authored-by: Maxwell G --- src/antsibull_core/args.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/antsibull_core/args.py b/src/antsibull_core/args.py index 9091c6d..98d4b23 100644 --- a/src/antsibull_core/args.py +++ b/src/antsibull_core/args.py @@ -38,7 +38,7 @@ def get_toplevel_parser( try: package_version = metadata.version(package) except metadata.PackageNotFoundError: - # If there's no metadata foun, assume we're running from source + # If there's no metadata found, assume we're running from source package_version = "source" toplevel_parser = argparse.ArgumentParser(**kwargs)