[Django] #25419: Management commands: allow to set exit code, without calling sys.exit

9 views
Skip to first unread message

Django

unread,
Sep 17, 2015, 7:16:35 AM9/17/15
to django-...@googlegroups.com
#25419: Management commands: allow to set exit code, without calling sys.exit
--------------------------------------------+--------------------
Reporter: blueyed | Owner: nobody
Type: New feature | Status: new
Component: Core (Management commands) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------------+--------------------
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()`.

--
Ticket URL: <https://code.djangoproject.com/ticket/25419>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 17, 2015, 8:41:11 AM9/17/15
to django-...@googlegroups.com
#25419: Management commands: allow to set exit code, without calling sys.exit
-------------------------------------+-------------------------------------

Reporter: blueyed | Owner: nobody
Type: New feature | Status: new
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25419#comment:1>

Django

unread,
Sep 18, 2015, 2:32:43 AM9/18/15
to django-...@googlegroups.com
#25419: Management commands: allow to set exit code, without calling sys.exit
-------------------------------------+-------------------------------------
Reporter: blueyed | Owner: nobody
Type: New feature | Status: closed

Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: new => closed
* resolution: => wontfix


--
Ticket URL: <https://code.djangoproject.com/ticket/25419#comment:2>

Reply all
Reply to author
Forward
0 new messages