#27247: Official way to create custom admin commands with subcommands in py2 / 
     Reporter:  stephanm                    |      Owner:  nobody
         Type:  Bug                         |     Status:  new
    Component:  Core (Management commands)  |    Version:  1.10
     Severity:  Normal                      |   Keywords:
 Triage Stage:  Unreviewed                  |  Has patch:  0
Easy pickings:  0                           |      UI/UX:  0

 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
 to migrate from python 2.7.12 (32bit) to python 3.5.2 (64bit) on windows 7

 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],
                               help="This is command_1 no subcommands")
         parser_service = subparsers.add_parser("service",
 parents=[parent_parser], cmd=self,
                                                help="Manage a windows
         parser_service.add_argument("serviceCmd", choices=["install",
 "delete", "start", "stop", "status"],
                                      help="Create or deletes a windows


 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
 usage: manage.py repo_sample [-h] [--version] [-v {0,1,2,3}]
                              [--settings SETTINGS] [--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
 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

 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)

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

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
For more options, visit https://groups.google.com/d/optout.

Reply via email to