cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

Add required args to subcommand program

Open rdhammond15 opened this issue 1 year ago • 6 comments

Issue

When a positional comes before a subcommand, the help output for the subcommand will be missing the positional in the usage. This does not match up with argparse functionality.

Expected Output

(Cmd) top middle test test lower1 -h
Usage: top middle required required2 lower1 [-h]

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

Actual Output

(Cmd) top middle test test lower1 -h
Usage: top middle lower1 [-h]

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

Sample Test Program

import cmd2


class App(cmd2.Cmd):

    top_parser = cmd2.Cmd2ArgumentParser(description="Top level parser")
    top_subparser = top_parser.add_subparsers(dest="middle", required=True, title="Middle level command")

    @cmd2.with_argparser(top_parser)
    def do_top(self, opts):
        print("In top")
        opts.cmd2_handler.get()(opts)


@cmd2.with_default_category('middle')
class Middle(cmd2.CommandSet):

    middle_parser = cmd2.Cmd2ArgumentParser(description="Middle level command to top")
    middle_parser.add_argument("required", help="Required arg between subparsers")
    middle_parser.add_argument("required2", help="Required arg between subparsers")
    middle_subparser = middle_parser.add_subparsers(dest="lower", required=True, title="Lower level commands")
    middle_subparser.add_parser("lower1", help="First lower command")
    middle_subparser.add_parser("lower2", help="Second lower command")

    @cmd2.as_subcommand_to('top', 'middle', middle_parser, help="Middle and lower commands")
    def do_middle(self, opts):
        print("Lower commands")


if __name__ == "__main__":
    app = App()
    app.cmdloop()

rdhammond15 avatar Feb 15 '24 12:02 rdhammond15

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (be15939) 98.56% compared to head (2bc8b6b) 98.56%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1293      +/-   ##
==========================================
- Coverage   98.56%   98.56%   -0.01%     
==========================================
  Files          22       22              
  Lines        5780     5776       -4     
==========================================
- Hits         5697     5693       -4     
  Misses         83       83              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Feb 15 '24 16:02 codecov[bot]

Please run black and add type annotations to address the mypy errors. The changes look reasonable to me. Thanks for finding and addressing this!

Seems like flake8 and black are competing against each other? Format passes now, but Lint fails due to the formatting fixes.

rdhammond15 avatar Feb 16 '24 15:02 rdhammond15

@anselor Do you have any guidance for the conflicting flake8 and black rules?

rdhammond15 avatar Apr 09 '24 11:04 rdhammond15

Sorry, this slipped out of my brain and I didn't notice until now. It looks like the build log is gone and I'm not sure how to rerun the build...

anselor avatar Jul 02 '24 00:07 anselor

@anselor I've repushed to kick off the workflow again.

rdhammond15 avatar Jul 04 '24 13:07 rdhammond15

@rdhammond15 It looks like flake8 recently become opinionated about that. I disagree with E704 being an antipattern. Just go ahead and disable E704 for flake8 globally.

anselor avatar Jul 04 '24 14:07 anselor