cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

Help text formatting question

Open xuoguoto opened this issue 1 year ago • 5 comments

Hello all,

I am looking for some help in formatting the help text automatically generated by the cmd2. I have a program which produces a help message as follows:

(Cmd) help foo
Usage: foo [-h] {ro-community, ip-address} ...

Commands for configuration

optional arguments:
  -h, --help            show this help message and exit

List of sub commands:
  {ro-community, ip-address}
    ro-community        Remove ro-community string
    ip-address          Remove ip-address

Is it possible to format the help as follows:

(Cmd) help foo
Usage: foo [-h] (ro-community | ip-address) ...

Commands for configuration

optional arguments:
  -h, --help            show this help message and exit

List of sub commands:
    ro-community        Remove ro-community string
    ip-address          Remove ip-address

Or even:

(Cmd) help foo
Usage: foo [-h] COMMANDS OPTIONS

Commands for configuration

optional arguments:
  -h, --help            show this help message and exit

List of sub commands:
    ro-community        Remove ro-community string
    ip-address          Remove ip-address

An ideal scenario would be displaying all the help in a single screen some thing like the following

(Cmd) help foo
Usage: foo [-h] COMMANDS OPTIONS

Commands for configuration

optional arguments:
  -h, --help            show this help message and exit

List of sub commands:
    ro-community        [-h] <community-name>
    ip-address          [-h] <ip-address>

Also for this command help:

(Cmd) help foo ip-address 
Usage: foo ip-address [-h] ip-address

positional arguments:
  ip-address  ip-address to remove

optional arguments:
  -h, --help  show this help message and exit

Is it possible to format this as:

(Cmd) help foo ip-address 
Usage: foo ip-address [-h] <ip-address>

positional arguments:
  ip-address  ip-address to remove

optional arguments:
  -h, --help  show this help message and exit

The sample program I am testing with is as follows:

import cmd2
from cmd2 import (
    CommandSet,
    Cmd2ArgumentParser,
    with_argparser,
    with_default_category
)


class BasicApp(cmd2.Cmd):
    def __init__(self):
        super().__init__()

    # Commands                                                                                                                                  
    listen_ip_parser = Cmd2ArgumentParser(
        description="Set listen ip address", add_help=False
    )
    listen_ip_parser.add_argument("ip", help="ip address")
    @with_argparser(listen_ip_parser)
    def do_listen_ip(self, args):
        """Exit from harware interface."""
        self._cmd.cmd_logger.info(f"listen-ip {args.ip}")

    # foo command and its sub commands                                                                                                          
    remove_parser = Cmd2ArgumentParser()
    remove_subparsers = remove_parser.add_subparsers(title="List of sub commands")

    parser_ro_community = remove_subparsers.add_parser("ro-community", help="Remove ro-community string")
    parser_ro_community.add_argument("community-name",help="ro-community name to remove")

    def remove_ro_community(self, args):
        """Remove ro-community user"""
        self._cmd.cmd_logger.info(f"foo ro-community {args.get('community-name')}")

    parser_ro_community.set_defaults(func=remove_ro_community)

    parser_ip_address = remove_subparsers.add_parser("ip-address", help="Remove ip-address")
    parser_ip_address.add_argument("ip-address",help="ip-address to remove")

    def remove_ip_address(self, args):
        """Remove ip-address user"""
        self._cmd.cmd_logger.info(f"foo ip-address {args.get('ip-address')}")

    parser_ip_address.set_defaults(func=remove_ip_address)

    @with_argparser(remove_parser)
    def do_foo(self,args):
        """Commands for configuration"""
        func = getattr(args, 'func', None)
        if func is not None:
            # Call whatever subcommand function was selected                                                                                    
            return func(self, args)
        else:
            # Foo subcommand was provided, so call help                                                                                         
            return self._cmd.do_help('foo')


if __name__ == '__main__':
    app = BasicApp()
    app.cmdloop()

xuoguoto avatar Jun 25 '24 09:06 xuoguoto