Skip to content

Intended way to define a mutually exclusive group inside a normal argument group with with_annotated #1687

Closed

Description

@neoniobium

Question, Bug report or Feature Request

Is there an intended way to define a mutually exclusive argument group inside a normal argument group when using the with_annotated decorator?

I think this is supported by builtin argparse, while nested argument groups and normal argument groups inside mutually exclusive argument groups are not supported at least since Python 3.11. See argparse

Activity

  1. KelvinChung2000 commented on Jun 10, 2026

    @KelvinChung2000
    Contributor

    My current thinking is to keep supporting it and align it with argparse. However, I also think we should ban it, since this is never the intended use case for argparse. What is your thought @tleonhardt ?

  2. tleonhardt commented on Jun 10, 2026

    @tleonhardt
    Member

    I'd say as long as we have a way of supporting a mutually exclusive group I think that is fine. I don't see the need to support a mutually exclusive group inside a normal argument group. But perhaps I am missing something?

  3. neoniobium commented on Jun 10, 2026

    @neoniobium
    ContributorAuthor

    I think this is the intended way to use a mutually exclusive group. This example is taken directly from the argparse documentation.

    parser = argparse.ArgumentParser(prog='PROG')
    group = parser.add_argument_group('Group title', 'Group description')
    exclusive_group = group.add_mutually_exclusive_group(required=True)
    exclusive_group.add_argument('--foo', help='foo help')
    exclusive_group.add_argument('--bar', help='bar help')
    parser.print_help()
  4. KelvinChung2000 commented on Jun 10, 2026

    @KelvinChung2000
    Contributor

    For your specific example, you can do it in the following way

    from typing import Annotated
    
    import cmd2
    from cmd2.annotated import Group, Option, with_annotated
    
    
    class App(cmd2.Cmd):
      @with_annotated(
          mutually_exclusive_groups=(Group("foo", "bar", required=True, title="Group title", description="Group description"),),
      )
      def do_prog(
          self,
          foo: Annotated[str | None, Option(help_text="foo help")] = None,
          bar: Annotated[str | None, Option(help_text="bar help")] = None,
      ) -> None:
          self.poutput(f"foo={foo} bar={bar}")
    
    
    if __name__ == "__main__":
      App().cmdloop()
    Image
  5. KelvinChung2000 commented on Jun 10, 2026

    @KelvinChung2000
    Contributor

    I just noticed the title and description are not propagating to the help correctly. I will have this fixed.

  6. neoniobium commented on Jun 11, 2026

    @neoniobium
    ContributorAuthor

    To my understanding, argparse is intended to support any number of arguments and/or mutually exclusive groups within an argument group. This makes sense to me: normal argument groups label arguments, while mutually exclusive groups change the behavior of the parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions