#35926: Support capturing the remainder of a command-line arguments in 
BaseCommand
-------------------------------------+-------------------------------------
     Reporter:  Daniel Quinn         |                    Owner:  (none)
         Type:  New feature          |                   Status:  closed
    Component:  Core (Management     |                  Version:  dev
  commands)                          |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:  unknown args         |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

 * keywords:  management command, remainder => unknown args
 * resolution:   => wontfix
 * status:  new => closed
 * version:  5.1 => dev

Comment:

 Hello Daniel, thank you for taking the time to create this ticket. I see
 two sides in your request:

 == Side A ==
 As a Django Fellow, I think that the provided example seems a very
 specific need arising from a niche use case. I don't think this applies to
 the broader ecosystem, and Django is a framework designed to offer robust
 and accurate solutions for common scenarios. Furthermore, as a seasoned
 developer, I think that allowing any amount of unknown named params in a
 command is a bad programming pattern, so I would advice against that
 pattern.

 What you could do instead, is to allow any number of arguments associated
 with a single argument name that is defined in your parser, something
 similar to what [https://docs.djangoproject.com/en/5.1/howto/custom-
 management-commands/ the docs shows for custom management commands] when
 defining nargs for `polls_ids`:

 {{{#!python
     def add_arguments(self, parser):
         parser.add_argument("poll_ids", nargs="+", type=int)
 }}}

 (Yes, this limits the type of the nargs but I think that is a good thing!)

 == Side B ==
 Assuming that we consider the provided use case and want to build a
 solution, I think the best approach would be, in your code, to monkeypatch
 `CommandParser` to be replaced with a custom class that reimplements
 `parse_args` and stores the unknown args to do something with them when
 needed:

 {{{#!diff
 diff --git a/django/core/management/base.py
 b/django/core/management/base.py
 index 6232b42bd4..d8ea38ac3c 100644
 --- a/django/core/management/base.py
 +++ b/django/core/management/base.py
 @@ -65,7 +65,9 @@ class CommandParser(ArgumentParser):
              args or any(not arg.startswith("-") for arg in args)
          ):
              self.error(self.missing_args_message)
 -        return super().parse_args(args, namespace)
 +        known, unknown = super().parse_known_args(args, namespace)
 +        # Do something with unknown args.
 +        return known

      def error(self, message):
 }}}

 Given the above, I'll close the ticket accordingly, but if you disagree,
 you can consider starting a new conversation on the
 [https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
 reach a wider audience and likely get extra feedback. More information in
 [https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
 features/#requesting-features the documented guidelines for requesting
 features].
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35926#comment:1>
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019349f67268-145b47bd-5402-47ab-89b5-5df9b8906678-000000%40eu-central-1.amazonses.com.

Reply via email to