Skip to content

Commit

Permalink
Merge pull request #220 from BrianPugh/version-help-flag-parameters
Browse files Browse the repository at this point in the history
Use last command's help/version flags.
  • Loading branch information
BrianPugh authored Aug 27, 2024
2 parents 729f2b9 + 8035bcc commit 34df4c7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
27 changes: 19 additions & 8 deletions cyclopts/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from contextlib import suppress
from copy import copy
from functools import partial
from itertools import chain
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -311,7 +312,7 @@ def _delete_commands(self, commands: Iterable[str], default=None):
for command in commands:
with suppress(KeyError):
if default:
if self[command].default == self.version_print:
if self[command].default == default:
del self[command]
else:
del self[command]
Expand Down Expand Up @@ -460,8 +461,8 @@ def __iter__(self) -> Iterator[str]:
def meta(self) -> "App":
if self._meta is None:
self._meta = type(self)(
help_flags=copy(self.help_flags),
version_flags=copy(self.version_flags),
help_flags=self.help_flags,
version_flags=self.version_flags,
group_commands=copy(self.group_commands),
group_arguments=copy(self.group_arguments),
group_parameters=copy(self.group_parameters),
Expand Down Expand Up @@ -571,16 +572,20 @@ def command(
raise ValueError("Cannot supplied additional configuration when registering a sub-App.")
else:
validate_command(obj)
kwargs.setdefault("help_flags", [])
kwargs.setdefault("version_flags", [])

kwargs.setdefault("help_flags", self.help_flags)
kwargs.setdefault("version_flags", self.version_flags)

if "group_commands" not in kwargs:
kwargs["group_commands"] = copy(self.group_commands)
if "group_parameters" not in kwargs:
kwargs["group_parameters"] = copy(self.group_parameters)
if "group_arguments" not in kwargs:
kwargs["group_arguments"] = copy(self.group_arguments)
app = App(default_command=obj, **kwargs) # pyright: ignore
# app.name is handled below

for flag in chain(kwargs["help_flags"], kwargs["version_flags"]): # pyright: ignore
app[flag].show = False

if app._name_transform is None:
app.name_transform = self.name_transform
Expand Down Expand Up @@ -689,6 +694,12 @@ def parse_known_args(
command_chain, apps, unused_tokens = self.parse_commands(tokens)
command_app = apps[-1]

# We don't want the command_app to be the version/help handler.
with suppress(IndexError):
if set(command_app.name) & set(apps[-2].help_flags + apps[-2].version_flags): # pyright: ignore
apps = apps[:-1]
command_app = apps[-1]

try:
parent_app = apps[-2]
except IndexError:
Expand All @@ -704,7 +715,7 @@ def parse_known_args(
# Special flags (help/version) get intercepted by the root app.
# Special flags are allows to be **anywhere** in the token stream.

for help_flag in self.help_flags:
for help_flag in command_app.help_flags:
try:
help_flag_index = tokens.index(help_flag)
break
Expand All @@ -720,7 +731,7 @@ def parse_known_args(
command = meta_parent.help_print
bound = cyclopts.utils.signature(command).bind(tokens, console=console)
unused_tokens = []
elif any(flag in tokens for flag in self.version_flags):
elif any(flag in tokens for flag in command_app.version_flags):
# Version
command = self.version_print
while meta_parent := meta_parent._meta_parent:
Expand Down
2 changes: 1 addition & 1 deletion cyclopts/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def format_usage(
for command in command_chain:
app = app[command]

if app._commands:
if any(x.show for x in app._commands.values()):
usage.append("COMMAND")

if app.default_command:
Expand Down
2 changes: 1 addition & 1 deletion tests/apps/test_burgery.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ def test_create_burger_3():


if __name__ == "__main__":
test_create_burger_1()
app()
33 changes: 33 additions & 0 deletions tests/test_version_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""From issue #219."""

import pytest


@pytest.mark.parametrize(
"cmd",
[
"foo --version 1.2.3",
"foo --version=1.2.3",
],
)
def test_version_subapp_version_parameter(app, assert_parse_args, cmd):
@app.command(version_flags=[])
def foo(version: str):
pass

assert_parse_args(foo, cmd, version="1.2.3")


@pytest.mark.parametrize(
"cmd",
[
"foo --help 1.2.3",
"foo --help=1.2.3",
],
)
def test_version_subapp_help_parameter(app, assert_parse_args, cmd):
@app.command(help_flags=[])
def foo(help: str):
pass

assert_parse_args(foo, cmd, help="1.2.3")

0 comments on commit 34df4c7

Please sign in to comment.