[Django] #19973: Management commands migration to argparse

75 views
Skip to first unread message

Django

unread,
Mar 3, 2013, 2:26:42 PM3/3/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: master
Component: Core (Management | Keywords: optparse, argparse,
commands) | basecommand
Severity: Normal | Has patch: 0
Triage Stage: Unreviewed | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
The class `BaseCommand`, from which developers subclass their own
management commands, is using the
[http://docs.python.org/2/library/optparse.html `optparse`] module, which
is actually deprecated.

If there are no important reasons to remain using the deprecated module,
perhaps would be a good idea to port the `BaseCommand` code to the new
module [http://docs.python.org/dev/library/argparse.html `argparse`],
which will be used in Python 3.

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

Django

unread,
Mar 3, 2013, 2:50:52 PM3/3/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: master
Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by ramiro):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* easy: 1 => 0
* stage: Unreviewed => Accepted


Comment:

Good idea, has been discussed before (but there isn't anassociated ticket
so thanks for opening this one).

But possible only once the minimal Python version supported by Django gets
to the 2.7 level, such is the version where argparse got added to the
stdlib.

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

Django

unread,
Mar 3, 2013, 3:11:35 PM3/3/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: nobody
Type: | Status: new
Cleanup/optimization | Version: master
Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by jose):

Ok. I think that since I've took part some days ago in fixing the ticket
#19877 and I've got familiar with that part, I can also try to fix this
one. So it can be appended to Django master code when it reaches the 2.7
Python as minimal version supported.

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

Django

unread,
Mar 3, 2013, 3:12:01 PM3/3/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jose):

* status: new => assigned
* owner: nobody => jose


--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:3>

Django

unread,
Mar 6, 2013, 1:05:53 PM3/6/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by carljm):

We'll need to provide a deprecation path and migration strategy for
authors of custom management commands, since optparse is exposed directly
to them via the definition of additional options for a custom command.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:4>

Django

unread,
May 31, 2013, 4:36:44 AM5/31/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jokerejoker):

* cc: hans@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:5>

Django

unread,
Sep 4, 2013, 6:14:03 AM9/4/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by mjtamlyn):

We are now able to do this as Django master is 2.7 only. I'm not having
many great ideas about how to support both at once, my instinct says we'll
need to do something like providing a flag on the Command instance to say
"use_argpase" instead. Even this isn't clear in terms of how it would
work.

Then again, there is no planned removal date for optparse
(http://www.python.org/dev/peps/pep-0389/#deprecation-of-optparse) so it's
not really urgent.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:6>

Django

unread,
Oct 16, 2013, 10:13:08 AM10/16/13
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by claudep):

Before starting with the implementation, there is a design decision to
make. `argparse` doesn't provide the same declarative syntax that
`optparse` used to provide with `make_option`. I see two main ways to go
forward:

1. Keep declarative syntax and mostly the same Command API that we have
now. Instead of importing `make_option` from `optparse`, we would import
it from a Django-provided utility, optionally renaming it `make_argument`.
Internally, this would only build a list of parameters that we would then
pass to `add_argument` calls in `create_parser`.[[BR]]
''Advantages'': nicer declarative syntax, less changes for custom
command writers (and accessorily easier for `call_command` to get default
values without instantiating the parser)

2. Depart from the current declarative syntax and change the documentation
about adding options for custom commands, basically instructing to
subclass `create_parser` (or more probably create a new subclassable
method `BaseCommand.add_arguments(self, parser)`) and to add arguments by
calling `parser.add_argument(<params>`).[[BR]]
''Advantages'': closer approach to standard Python

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:7>

Django

unread,
Apr 28, 2014, 5:58:57 PM4/28/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Adys):

So on Pootle we've been working on moving away from optparse in all our
backend scripts but due to the dependency django has on optparse for
specifically this, we are forced to use both optparse and argparse in the
code which makes kittens cry.

@claudep I would go with option 2. The advantage here is that it is much
more powerful right out of the box. Subcommands would create their own
parser and would be able to inherit the parent parser simply by calling
super(). This allows easy creation of things such as subcommands and such.

Being declarative is nice in principle but doesn't really work out for
argument parsing - there is a reason python changed the logic there.

Are you planning to work on this bug? I would love to help, FWIW.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:8>

Django

unread,
Apr 30, 2014, 3:01:23 AM4/30/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by claudep):

Thanks Adys for your input. Feel free to start hacking on it, and share
your work.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:9>

Django

unread,
Jun 6, 2014, 9:40:06 AM6/6/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by claudep):

@Adys, I made some progress on this issue. If you have also worked on
this, we might share our work, otherwise just wait that my branch is in a
publishable state...

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:10>

Django

unread,
Jun 9, 2014, 4:16:24 AM6/9/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 0 |
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"96e4b52ab2c6fae3e46a197d44c42cb6ebde7d9c"]:
{{{
#!CommitTicketReference repository=""
revision="96e4b52ab2c6fae3e46a197d44c42cb6ebde7d9c"
Converted Django scripts to argparse

Refs #19973.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:11>

Django

unread,
Jun 9, 2014, 11:06:29 AM6/9/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: optparse, argparse, | Patch needs improvement: 0
basecommand | UI/UX: 0
Has patch: 1 |

Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by claudep):

* has_patch: 0 => 1


Comment:

Here is the pull request with my work so far:
https://github.com/django/django/pull/2780

Comments welcome.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:12>

Django

unread,
Jun 13, 2014, 12:51:35 PM6/13/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master
Component: Core (Management | Resolution:
commands) | Triage Stage: Ready for
Severity: Normal | checkin
Keywords: optparse, argparse, | Needs documentation: 0
basecommand | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0

Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:13>

Django

unread,
Jun 13, 2014, 3:23:21 PM6/13/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Ready for
Severity: Normal | checkin
Keywords: optparse, argparse, | Needs documentation: 0
basecommand | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by timo):

Claude, do you know if this will obsolete #11407?

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:14>

Django

unread,
Jun 14, 2014, 3:22:48 AM6/14/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: assigned
Cleanup/optimization | Version: master

Component: Core (Management | Resolution:
commands) | Triage Stage: Ready for
Severity: Normal | checkin
Keywords: optparse, argparse, | Needs documentation: 0
basecommand | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by claudep):

Hopefully, yes, but in any case, I'll revisit #11407 after committing this
one.

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:15>

Django

unread,
Jun 14, 2014, 7:53:30 AM6/14/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: closed
Cleanup/optimization | Version: master
Component: Core (Management | Resolution: fixed

commands) | Triage Stage: Ready for
Severity: Normal | checkin
Keywords: optparse, argparse, | Needs documentation: 0
basecommand | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by claudep):

* status: assigned => closed
* resolution: => fixed


Comment:

Fixed in:
* 856863860352aa1f0288e6c9168a0e423c4f5184 Fixed #19973 -- Replaced
optparse by argparse in management commands
* cbff097bd91fad42c7231026968f686598b1d7a2 Documented optparse to
argparse changes for management commands
* 4b4524291adbc78ab880317124803fc37a2e414a Converted test management
command to argparse
* f17b24e407385eb18651bf023a187347aa9c1f75 Converted remaining
management commands to argparse

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:16>

Django

unread,
Jun 14, 2014, 12:55:52 PM6/14/14
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: closed
Cleanup/optimization | Version: master

Component: Core (Management | Resolution: fixed
commands) | Triage Stage: Ready for
Severity: Normal | checkin
Keywords: optparse, argparse, | Needs documentation: 0
basecommand | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"5949c2118d7cc7cffe86cd9db1820b186b88bfc0"]:
{{{
#!CommitTicketReference repository=""
revision="5949c2118d7cc7cffe86cd9db1820b186b88bfc0"
Restored command error behavior when called from command line

Refs #19973.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:17>

Django

unread,
Sep 23, 2015, 7:54:47 PM9/23/15
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: closed
Cleanup/optimization |
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed
Keywords: optparse, argparse, | Triage Stage: Ready for
basecommand | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"6a70cb53971a19f2d9e71d5ee24bfb0e844b4d9d" 6a70cb53]:
{{{
#!CommitTicketReference repository=""
revision="6a70cb53971a19f2d9e71d5ee24bfb0e844b4d9d"
Refs #19973 -- Removed optparse support in management commands per
deprecation timeline.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:18>

Django

unread,
Aug 2, 2016, 7:13:39 PM8/2/16
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: closed
Cleanup/optimization |
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed
Keywords: optparse, argparse, | Triage Stage: Ready for
basecommand | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"ccd5a23fba91e66656bb19b60bb6d737cab728f5" ccd5a23f]:
{{{
#!CommitTicketReference repository=""
revision="ccd5a23fba91e66656bb19b60bb6d737cab728f5"
Fixed #27000 -- Removed BaseCommand.usage() per deprecation timeline (refs
#19973).
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:19>

Django

unread,
Aug 2, 2016, 7:20:44 PM8/2/16
to django-...@googlegroups.com
#19973: Management commands migration to argparse
-------------------------------------+-------------------------------------
Reporter: jose | Owner: jose
Type: | Status: closed
Cleanup/optimization |
Component: Core (Management | Version: master
commands) |
Severity: Normal | Resolution: fixed
Keywords: optparse, argparse, | Triage Stage: Ready for
basecommand | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"7dd8e53cc834b65a2665d0bb63662db4051469b4" 7dd8e53c]:
{{{
#!CommitTicketReference repository=""
revision="7dd8e53cc834b65a2665d0bb63662db4051469b4"
[1.10.x] Fixed #27000 -- Removed BaseCommand.usage() per deprecation
timeline (refs #19973).

Backport of ccd5a23fba91e66656bb19b60bb6d737cab728f5 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/19973#comment:20>

Reply all
Reply to author
Forward
0 new messages