Textualize/trogon

Argument with a default not working when using groups

derlin opened this issue · 0 comments

I get some strange behaviour when using arguments with defaults on command groups. Here is a minimal example:

import click
from trogon import tui

@tui()
@click.group()
def cli():
    pass

@cli.command()
@click.argument("name", default="Bob")
def hello(name):
    """Pring hello."""
    print(f"Hello world and {name}")

if __name__ == "__main__":
    cli()

When running python cli.py tui and selecting the hello command, the command displayed at the bottom is:

cli.py '('"'"'Bob'"'"',)' hello Bob
image

When trying to run the command using ctrl+r, I get the following stacktrace:

Traceback (most recent call last):
  File "/some-dir/test-trogon/cli.py", line 16, in <module>
    cli()
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/trogon/trogon.py", line 286, in wrapped_tui
    Trogon(app, app_name=name, click_context=ctx).run()
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/trogon/trogon.py", line 252, in run
    f"Running [b cyan]{self.app_name} {' '.join(shlex.quote(s) for s in self.post_run_command)}[/]"
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/some-dir/test-trogon/.venv/lib/python3.11/site-packages/trogon/trogon.py", line 252, in <genexpr>
    f"Running [b cyan]{self.app_name} {' '.join(shlex.quote(s) for s in self.post_run_command)}[/]"
                                                ^^^^^^^^^^^^^^
  File "/somewhere/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/shlex.py", line 329, in quote
    if _find_unsafe(s) is None:
       ^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'tuple'

Note that it works if I remove the default on the argument or if I get rid of the group:

@tui()
@click.command(name="world")
@click.argument("name", default="Bob")
def cli(name):
    """Pring hello."""
    print(f"Hello world and {name}")

if __name__ == "__main__":
    cli()