Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#30305 closed Uncategorized (invalid)

Feature to set the returncode of a django custom command

Reported by: stephanm Owned by: nobody
Component: Uncategorized Version: master
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

In the docs:

https://docs.djangoproject.com/en/2.1/howto/custom-management-commands/#django.core.management.BaseCommand.handle

I can see that the handle() function will return a string which goes to stdout.

But I would like to set (supplementary) an integer returncode like its done in sys.exit() see https://docs.python.org/3/library/sys.html#sys.exit

I didn't find any "official" way to do this, perhaps this is a new feature.

Thanks.

Change History (3)

comment:1 Changed 16 months ago by felixxm

Resolution: invalid
Status: newclosed
Version: 2.1master

Thanks for the report, however answer for your question is already in the ticket description, i.e. you can use sys.exit(custom_number) in the handle().

comment:2 Changed 16 months ago by stephanm

Resolution: invalid
Status: closednew

Hi,

I stepped a little through the code after the handle() function returns.

Here I see in that I pass through the function
django.core.management.base.BaseCommand.run_from_argv()
which itself calls a connections.close_all() function at the end,
so using sys.exit() inside the handle() function will not give the exact same
behaviour as a "controlled" exit.

Or do I miss something?

See: run_from_argv(self, argv)

def run_from_argv(self, argv):
    try:
        self.execute(*args, **cmd_options)
    except Exception as e:
       #....
    finally:
        try:
            connections.close_all()
        except ImproperlyConfigured:
            # Ignore if connections aren't setup at this point (e.g. no
            # configured settings).
            pass

comment:4 Changed 16 months ago by felixxm

Resolution: invalid
Status: newclosed

sys.exit() works fine, e.g.

class Command(BaseCommand):
    def handle(self, *args, **options):
        sys.exit(43)
> python manage.py test_command
> echo $?
43

All cleanup actions are honored (see sys.exit documentation). Please use one of support channels if you have more questions.

Note: See TracTickets for help on using tickets.
Back to Top