#29152: management commands rigid ArgumentParser initialization -------------------------------------+------------------------------------- Reporter: Dmitry | Owner: nobody Type: New | Status: new feature | Component: Core | Version: 2.0 (Management commands) | Severity: Normal | Keywords: Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Hello everyone. Noticed today that there is no way to customize CommandParser (ArgumentParser subclass), initialized in management commands (https://github.com/django/django/blob/master/django/core/management/base.py#L227). There is no option (or i was unable to find it - please correct me in this case) to pass any kwargs in CommandParser constructor. The public method create_parser contains some django-related hard-coded add_arguments calls, so overriding it directly leads to some copy paste.
I needed to use some custom formatter for ArgumentParser (https://docs.python.org/3/library/argparse.html?highlight=argparse#argparse.ArgumentDefaultsHelpFormatter) to add default values to help page, but was unable to do this properly. There is also some other usefull keyword arguments available in ArgumentParser. As a proposal a new method could be added to BaseCommand (let's say BaseCommand.get_parser_instance), for example like this: {{{ def get_parser_instance(self): return CommandParser( self, prog="%s %s" % (os.path.basename(prog_name), subcommand), description=self.help or None, ) def create_parser(self, prog_name, subcommand):: parser = self.get_parser_instance() ... etc }}} This should not break anything in BaseCommand, but will allow have more control on parser's creation overriding just get_parser_instance method. Please sorry for mistakes. -- Ticket URL: <https://code.djangoproject.com/ticket/29152> 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/054.56f0fd8b8c65e072e9bead8d9f6c5076%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.