How do I return a value when @click.option is used to pass a command line argument to a function?

后端 未结 4 2048
慢半拍i
慢半拍i 2021-02-20 04:31

I am trying to use click python package to pass a command line argument to a function. The example from official documentation works as explained. But nowhere in the documentati

4条回答
  •  青春惊慌失措
    2021-02-20 04:58

    Unfortunately, what you're trying to do doesn't make sense. Command-line programs can have an exit code, but that's just a small integer; they can't return text, or arbitrary Python objects.

    There's a quasi-standard for what these integers means; the simple version is 0 for success, 1 for most errors, 2 for invalid command-line arguments. click is trying to make your function into a good command-line citizen, so when you exit your function, it calls sys.exit with the appropriate number (0 if you return, 1 if you raise, and 2 if it failed to parse your arguments).

    So, whatever you return has no effect, and whatever you try to do with the return value at the top level doesn't even get run.

    What programs usually do when they need to "return" text is to print it to standard output, which is exactly what click.echo is for.

提交回复
热议问题