larryhastings / appeal

Command-line parsing library for Python 3.
Other
122 stars 7 forks source link

appeal steals all stdout (bug?) #19

Open sotte opened 5 months ago

sotte commented 5 months ago

First of all, I really like appeal!

I wanted to use cog to update the help text in the README of a CLI that I build with appeal. For this I just wanted to capture stdout. However, appeal seems to capture all stdout and does not give it back.

Here is a little debug script.

import io
from contextlib import redirect_stdout

from my_package.__main__ import main  # this is the appeal cli

# main()  # if this is enabled, appeal steals all output. Should this be the case?

print("before")
temp_out = io.StringIO()
with redirect_stdout(temp_out):
    print("hello")
    # main()  # this is really what I want to capture to get the help text
print("after")

print(temp_out.getvalue())

Output:

before
after
hello

If I uncomment the first main(), I only get the help of main, nothing else. If I uncomment the second main(), I only get

before

I'm not sure if this is the intended behavior or if there is a better way to do what I wanted to do or if this is a bug.


Note: with click I normally use the CLIRunner to conveniently captures stdout and then update the readme:

import cog
from click.testing import CliRunner
runner = CliRunner()
result = runner.invoke(my_cli.cli, ["--help"])
cog.out(resut.output)