Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is raise SystemExit(exit_code) the right way to return a non-zero exit status? #270

Closed
aconrad opened this issue Dec 12, 2014 · 12 comments
Closed

Comments

@aconrad
Copy link

aconrad commented Dec 12, 2014

I would like to control the exit status of a command. I saw that different exceptions are available from Click which are mostly used for bad command line usage.

I just want a clean exit for a properly ran command (with no Abort! message). I ended up using raise SystemExit(1). Is this the proper way of doing it? I was thinking that it'd be nice if the return value of the command would be the exit status.

@pycobertura.command()
@click.argument('cobertura_file1')
@click.argument('cobertura_file2')
def diff(cobertura_file1, cobertura_file2):
    """compare two coverage files"""
    cobertura1 = Cobertura(cobertura_file1)
    cobertura2 = Cobertura(cobertura_file2)
    reporter = DeltaReporter(cobertura1, cobertura2)

    report = reporter.generate()
    click.echo(report)

    # non-zero exit code if line rate worsened
    exit_code = cobertura1.line_rate() > cobertura2.line_rate()
    raise SystemExit(exit_code)  # or `return exit_code`?
@scharissis
Copy link

Why not use the regular python site module exit()?
It works great for me.

https://docs.python.org/2/library/constants.html#exit

@untitaker
Copy link
Contributor

@scharissis:

They are useful for the interactive interpreter shell and should not be used in programs.

Use sys.exit instead, or just raise the exception manually, either way is fine.

@gst
Copy link

gst commented Jan 4, 2017

Hi,

@scharissis @untitaker

I don't create a new issue as my question is totally linked to this one :

why does click so apparently "force" the return code of the function to 0 and ignore the given return value?

well actually it's not that it force the return value to 0, it doesn't return but executes (force?) sys.exit() itself. with 0 whatever is the return value you actually returned from the function so..

I mean : wouldn't it be ok/better that it uses the returned value from the function to sys.exit() ?

Thx for any info.

@untitaker
Copy link
Contributor

@gst You're mixing up raising SystemExit with function return values here, so it's not really clear what you're asking. The return value of the function was never something that Click honored.

@gst
Copy link

gst commented Jan 5, 2017

hi,

well.. I wasn't 100% clear effectively.

My question is so:

couldn't click use the return value of a click.Command decorated function when it returns, like diff here above, and to sys.exit() that value ? That is just return cobertura1.line_rate() > cobertura2.line_rate() for the above.

I (quickly) checked the documentation and didn't find a clear statement (but I could have missed it) about how to it is supposed to be handled (the process exit code of a click.Command).

Thx.

@untitaker
Copy link
Contributor

untitaker commented Jan 5, 2017 via email

@NiklasRosenstein
Copy link

Yeah click ignores that. You should use sys.exit instead.

I think the question is about why Click ignores that. That would be quite convenient, and IMHO returning the exit code makes the code look cleaner than using sys.exit() (one of the reasons might be that return is usually highlighted in editors and you will notice it much easier than sys.exit() and it gives you a visual indicator of "function can end here").

@untitaker
Copy link
Contributor

untitaker commented Apr 5, 2017 via email

@WeatherGod
Copy link

WeatherGod commented Feb 13, 2019

I am confused. The documentation at https://click.palletsprojects.com/en/7.x/commands/#command-return-values states quite clearly, to me at least, that command return values are honored. Why is it that when I use CliRunner.invoke() that I don't get the exit_code that I returned from my command-decorated function? Perhaps this is a documentation issue?

@WeatherGod
Copy link

relatedly, when one does console_script entry points, you use return to indicate the exit code. So, as someone who has been used to that behavior, this was quite surprising.

@untitaker
Copy link
Contributor

@WeatherGod I think that pretty much depends on how you wired up CliRunner.invoke() with your console_script. From what I can tell CliRunner.invoke() is supposed to return an int

@PREM1980
Copy link

PREM1980 commented Oct 9, 2019

Why not use the regular python site module exit()?
It works great for me.

https://docs.python.org/2/library/constants.html#exit

Hello,

How do I custom handle click.abort exception in my code. If abort exception happens I need to close my queue that i am consuming. Kind of customize the abort exception.

https://stackoverflow.com/questions/58296149/custom-exception-handling-using-python-click

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants