Skip to content

In v2.5.0 if you override do_help() and don't use the @with_argparser decorator you get an error #1383

Closed

Description

@kotfu

I have an app which uses cmd2 called tomcatmanager. This app creates and uses it's own argument parsers, and does not use the with_argparser decorator provided by cmd2. tomcatmanager does not use CommandSets. In 2.5.0, there is an undocumented breaking change which changes the way cmd2 finds and displays help messages. The result: cmd2 can not find or display any of the help messages from commands defined in my app.

I'll have to continue to dig, but it appears that the new code which creates and populates self._command_parsers behaves quite differently that the code in v2.4.3.

Initial research shows that self._command_parsers doesn't contain any of my command functions or argument parsers

Activity

  1. tleonhardt commented on Nov 19, 2024

    @tleonhardt
    Member

    @kmvanbrunt You have the most insight into the changes made in 2.5.0. Would you have any insight into what changed in terms of finding help text to display?

  2. kmvanbrunt commented on Nov 19, 2024

    @kmvanbrunt
    Member

    This is the error I get when testing tomcatmanager.

    if arg_parser is None:
        # This shouldn't be possible to reach
        raise ValueError(f'No argument parser found for {command_name}')  # pragma: no cover

    Since kotfu is overriding do_help() as a non-argparse decorated method, Cmd._register_command_parser() never creates a parser for cmd2's do_help(). Therefore, when kotfu calls super().do_help(), the decorator runs and finds no parser.

    We need to make sure Cmd._register_command_parser() creates a parser for each of the base class do_xxx methods.

  3. kotfu commented on Nov 19, 2024

    @kotfu
    ContributorAuthor

    Other options I could consider include:

    a) using the @with_argparser decorator on my overridden do_help() method
    b) completely reimiplementing do_help(), ie don't ever call super().do_help()

    Also, with this new information, I'm rewriting the issue description....

  4. changed the title [-]v2.5.0 introduces an undocumented breaking change for custom arg parsers[/-] [+]In v2.5.0 if you override `do_help()` and don't use the `@with_argparser` decorator you get an error[/+] on Nov 19, 2024
  5. kmvanbrunt commented on Nov 19, 2024

    @kmvanbrunt
    Member

    @kotfu You don't need to change anything. It's a bug in cmd2.

    The same problem will occur if you override do_help(), decorate it using with_argparser, and supply a parser with different arguments than what super().do_help() is expecting. The moment you call super().do_help(), it will look up the parser by command name and get the parser for your do_help().

    A major improvement to cmd2 2.5.0 is making parsers instance variables instead of class variables. The concept is backward compatible, but unfortunately the implementation needs a fix. The problem is the parser dictionary is keyed by command name. To handle overriding functions, it needs to be keyed by the function the parser belongs to.

    Give us a few days and the fix will be in cmd2 2.5.7.

    Sorry for breaking tomcatmanager.

  6. kmvanbrunt commented on Nov 23, 2024

    @kmvanbrunt
    Member

    @kotfu cmd2 2.5.7 is out.

    I tested it with tomcatmanager and the help commands worked.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions