1

When I run the code below I never see the print output. Take the "click" code out and I do. So, is there a way to prevent "click" from hijacking stdout? Running python 3.10.x on Windows from within PyCharm.

import click
my_cfg = { 'domains': [], 'def_domains': ['stackoverflow.com', 'google.com'] } 

@click.command()
@click.option('--domain', '-d', multiple=True, type=str, nargs=1, default=my_cfg['def_domains'])
def domains(domain):
    click.echo('\n'.join(domain))
    my_cfg['domains'].append(domain)
domains()

print(f"my_cfg: {my_cfg}")
### Bunch of code here that I cannot easily move into domains() ###

Edit1: I'm trying to bolt click into an existing script that I'm adding command line options to. I have a bunch of code that follows domains() that I cannot easily move into domains().

Edit2: I put a breakpoint on the print() in pycharm but it doesn't seem to halt on it. And I never saw an error for using an undefined variable. So maybe click takes over?

8
  • You never even define the cfg variable. Is this code the same as what you're testing? Also, calling click-decorated functions directly never works right, it's a documented issue. Commented Jun 10, 2022 at 20:04
  • I renamed it just in case there was a conflict. I didn't even see an error, so maybe click hijacks stderr too? Or maybe that code is never called? I put a breakpoint on the print in pycharm but the debugger never halts on it. Commented Jun 10, 2022 at 20:24
  • Does this answer your question? Why does this call not require 2 arguments as defined? Commented Jun 10, 2022 at 20:55
  • No, in that example there were two different run() functions. I think I'm asking why can't I put more code after the bottom most run(). It seems like click needs all the work a program is going to do inside a decorator function? Commented Jun 11, 2022 at 21:10
  • @angel1200 Where are you getting that from? The questioner's code only has one function. Commented Jun 11, 2022 at 21:26

1 Answer 1

-3

A click command exits at the end. Put your print inside your domains function.

Sign up to request clarification or add additional context in comments.

1 Comment

I have a bunch of code that follows that I cannot just move into domains(). I'm adding cli options to an existing script, and argparse doesn't have enough flexibility, so I'm trying to brute force click into the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.