#29152: Allow more control over ArgumentParser initialization in management commands -------------------------------------+------------------------------------- Reporter: Dmitry | Owner: Type: | humbertotm Cleanup/optimization | Status: assigned Component: Core (Management | Version: 2.0 commands) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by humbertotm): This is the fix I'm proposing based on my understanding of the issue. As far as I understand, the requested enhancement requires that `CommandParser` can ultimately receive any additional args that it can pass to its parent class `ArgumentParser`. What I did was adding an additional positional argument in the form of `kwargs` to the `create_parser()` method in `BaseCommand` as follows: {{{ def create_parser(self, prog_name, subcommand, **kwargs): """ Create and return the ``ArgumentParser`` which will be used to parse the arguments to this command. Hi. """ default_kwargs = { 'prog': '%s %s' % (os.path.basename(prog_name), subcommand), 'description': self.help or None, 'formatter_class': DjangoHelpFormatter, 'missing_args_message': getattr(self, 'missing_args_message', None), 'called_from_command_line': getattr(self, '_called_from_command_line', None) } kwargs.update(default_kwargs) parser = CommandParser(**kwargs) ... }}} This way, the `kwargs` provided to the method call are merged with the default kwargs into a single dictionary to be passed on to `CommandParser`. These changes do not break the test suite, but additional regression tests are still pending. Let me know if I'm failing to see or understand something by following this approach. Thanks! -- Ticket URL: <https://code.djangoproject.com/ticket/29152#comment:10> 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 https://groups.google.com/d/msgid/django-updates/069.63418b618dee99e65d7f596b0f3f8634%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.