Opened 9 years ago

Closed 9 years ago

#25419 closed New feature (wontfix)

Management commands: allow to set exit code, without calling sys.exit

Reported by: Daniel Hahler Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With a Command like the following, it would be nice to be able to specify the exit code in case of errors:

class Command(LabelCommand):
    def handle_label(self, username, **options):
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            self.stderr.write("{}: user not found, skipping.".format(username))
            self.exitcode = 5
            return
        result = do_something()
        self.stdout.write("{}: {}".format(username, result))

While you could call sys.exit(5) directly in this case, the use case would be to handle all existing users from the labels, but exit non-zero in case of any errors.

I could imagine having exitcode = 0 on the BaseCommand class and then use this in the end with sys.exit().

Change History (2)

comment:1 by Aymeric Augustin, 9 years ago

This seems a bit specialized to me.

You can implement it as follows:

class ExitCodeCommand(BaseCommand):

    def handle(self, *args, **kwargs):
        self.exit_code = 0
        self.handle_internal(*args, **kwargs)  # may set self.exit_code
        sys.exit(self.exit_code)

comment:2 by Claude Paroz, 9 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top