#27247 closed Bug (invalid)

Official way to create custom admin commands with subcommands in py2 / py3

Reported by: stephanm Owned by: nobody
Component: Core (Management commands) Version: 1.10
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



I found a example on the internet showing how to create custom admin commands with subcommands.

My app is named "repo", I use django 1.10.1 on Windows and I am just trying
to migrate from python 2.7.12 (32bit) to python 3.5.2 (64bit) on windows 7 64bit.

I did also a big switch from optparse to argparse since optparse is gone in django 1.10.

The file repo_sample.py looks like:

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from django.core.management.base import BaseCommand
import argparse

class Command(BaseCommand):
    help = "this is a sample"
    def handle(self, *args, **options):
        if "command" in options:
            print("command: %s" % options["command"])
            print("no command")
    def add_arguments(self, parser):
        parser.formatter_class = argparse.RawDescriptionHelpFormatter
        subparsers = parser.add_subparsers(metavar='command',
                                           help='sub-command help')
        parent_parser = argparse.ArgumentParser(add_help=False)
        subparsers.add_parser("command_1", parents=[parent_parser], cmd=self, 
                              help="This is command_1 no subcommands")
        parser_service = subparsers.add_parser("service", parents=[parent_parser], cmd=self, 
                                               help="Manage a windows service")
        parser_service.add_argument("serviceCmd", choices=["install", "delete", "start", "stop", "status"],
                                     help="Create or deletes a windows service..")

Now there is a difference if I run it with python 2 or python 3:

D:\util>manage.py repo_sample
2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
usage: manage.py repo_sample [-h] [--version] [-v {0,1,2,3}]
                             [--settings SETTINGS] [--pythonpath PYTHONPATH]
                             [--traceback] [--no-color]
                             command ...
manage.py repo_sample: error: too few arguments

D:\util>py -3 manage.py repo_sample
3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]
command: None


As you see, the first call with python 2 gives an error while the python 3 call behaves differently,
but I think it should at least behave the same way (so I marked it as bug).

Now I am not sure if argparse behaves differently between python 2.7 and python 3.5 and if I made an error.

But here is my question(s):

  • What is the official ("correct") way to create custom admin commands with subcommands?
  • Do you have some examples in the docs? (I didnt find something)

Change History (1)

comment:1 Changed 18 months ago by Tim Graham

Resolution: invalid
Status: newclosed

It's better to ask "is it a bug?" questions on our support channels. See TicketClosingReasons/UseSupportChannels. I'm not aware of any documentation about subcommands. Maybe it makes sense to add something. I'm not sure if that topic is a more Python related than Django related though. If you learn something, think it's appropriate for the Django docs, and want to write it up, feel free to open a ticket.

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