[Django] #27247: Official way to create custom admin commands with subcommands in py2 / py3

4 ogledi
Preskoči na prvo neprebrano sporočilo

Django

neprebran,
19. sep. 2016, 12:19:5819. 9. 16
do django-...@googlegroups.com
#27247: Official way to create custom admin commands with subcommands in py2 / py3
--------------------------------------------+--------------------
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
--------------------------------------------+--------------------
Hi,

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:
{{{#!py
# -*- 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"])
else:
print("no command")

def add_arguments(self, parser):
parser.formatter_class = argparse.RawDescriptionHelpFormatter
subparsers = parser.add_subparsers(metavar='command',
dest='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

D:\util>
}}}

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)

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

Django

neprebran,
19. sep. 2016, 12:51:1519. 9. 16
do django-...@googlegroups.com
#27247: Official way to create custom admin commands with subcommands in py2 / py3
-------------------------------------+-------------------------------------
Reporter: stephanm | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 1.10
commands) |
Severity: Normal | Resolution: invalid
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 timgraham):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

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.

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

Odgovori vsem
Odgovori avtorju
Posreduj
0 novih sporočil