Coder Social home page Coder Social logo

Comments (6)

keleshev avatar keleshev commented on June 2, 2024

I'm ok with this change, if you provide the code.

I suggest to modify DocoptExit to be able to select the exit code and whether to write to stderr or stdout (likely to depend on exit code).

from docopt.

 avatar commented on June 2, 2024

Turns out this is actually impossible; SystemExit's ability to terminate the Python interpreter without printing a stack trace, as well as how it determines the exit code to use, are all hardcoded details of the Python implementation itself. For my purposes, however, I can temporarily redirect sys.stdout to a cStringIO instance; as docopt.extras() is the only function in the module that can raise a vanilla SystemExit, catching it and calling cstringio_instance.getvalue() is pretty much equivalent to what I was hoping to accomplish with a custom exception.

from docopt.

keleshev avatar keleshev commented on June 2, 2024

Actually raise SystemExit(42) will exit the program with code 42. So that is possible. The hard part (or, even, maybe, impossible part) is to print something to stderr while exiting using this exception.

from docopt.

keleshev avatar keleshev commented on June 2, 2024

The following script:

import sys


class MyExit(SystemExit):

    def __init__(self, msg):
        self.message = msg
        sys.stderr.write(self.message)
        SystemExit.__init__(self, 2)


raise MyExit('bye')

returns exit status 2 and prints "bye" to stderr. Although it is not exactly what we want. We want to print after the exception was uncaught.

from docopt.

 avatar commented on June 2, 2024

Exactly. What I tried to communicate with "how it determines the exit code" was how, if SystemExit (or sys.exit) is raised with an integer and not caught, it uses the given integer as the exit status; if it is raised with anything else, it exits with status 1 and prints the given argument to stderr. In CPython, this print-to-stderr-if-uncaught logic is written directly in pythonrun.c (more relevantly, in the function handle_system_exit on line 1077.)

from docopt.

keleshev avatar keleshev commented on June 2, 2024

You're right. Looks like a language limitation—they should have not mixed exit code and error message.

Also, I wanted docopt to return code 2 instead of 1 in case of DocoptExit (looks like 2 is more conventional for input/user errors in Unix), but that makes it a bit harder.

from docopt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.