HomePhabricator
Diffusion Scap 0d76e5fb54a5

Support sub-commands on cli.Application methods

Description

Support sub-commands on cli.Application methods

Tags: Scap, Release-Engineering-Team

Maniphest Tasks: T142880

Summary:

What's new:

  • cli.Application now supports sub-sub commands
  • @cli.argument applies to any class methods, not just main()
  • @cli.argument can also be applied directly to the class to specify args which are shared among all sub-commands
  • I also significantly refactored the argparse code in arg.py and improved help formatting.
  • Removed deprecated support for calling scap scripts directly (e.g. no more sync-file). I didn't remove the script files, I just added 'sys.exit(1)' to them. It may be time, however, to remove them entirely.

This is needed for my related work on scap swat which is pending review in gerrit: https://gerrit.wikimedia.org/r/#/c/306259/

Demo

https://asciinema.org/a/1x54kw77tvatxiqv45ba6ael7

Example usage

@cli.command('swat', help='Mediawiki SWAT deployment helper.', subcommands=True)
@cli.argument('-b', '--branch', nargs="?",
              help='One or more branches to merge into.'
              + ' Default: active wmf/branches.')
@cli.argument('-m', '--message', nargs=1,
              help='Include a message or comment with your action.')
class Swat(cli.Application):
    '''
    scap swat: cherry-pick and deploy patches for Mediawiki SWAT.
    '''

    @cli.argument('term', nargs='+',
                  help='Gerrit search term(s). E.g: status:open owner:self')
    @cli.argument('-n', type=int, nargs=1, default='10',
                  help='Number of results to show. Default: 10')
    def query(self, args):
        ''' execute a gerrit query '''
        # implement query sub-command here

    @cli.argument('changeid', nargs=1,
                  help='The ChangeId of a patch to revert.')
    def revert(self, changeids):
        ''' revert a change in gerrit '''
        # implement revert sub-command here

Help output: scap swat -h

usage: scap swat [-h] <sub-command> ...

scap swat: cherry-pick and deploy patches for Mediawiki SWAT.

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

swat sub-commands:
  Mediawiki SWAT deployment helper.

  <sub-command>
    query        execute a gerrit query
    revert       revert a change in gerrit

Help output: scap swat query -h

usage: scap swat query [-h] [-b [BRANCH]] [-m MESSAGE] [-n N] term [term ...]

execute a gerrit query

positional arguments:
  term                  Gerrit search term(s). E.g: status:open owner:self

optional arguments:
  -h, --help            show this help message and exit
  -b [BRANCH], --branch [BRANCH]
                        One or more branches to merge into. Default: active
                        wmf/branches.
  -m MESSAGE, --message MESSAGE
                        Include a message or comment with your action.
  -n N                  Number of results to show. Default: 10

Test Plan:
I've tested the arg parsing fairly thoroughly, including autocomplete.
I haven't tested every scap command so it's possible that I broke something.
Testing on beta would definitely be a good idea.

Reviewers: Release-Engineering-Team, thcipriani

Reviewed By: Release-Engineering-Team, thcipriani

Subscribers: bd808, ori

Differential Revision: https://phabricator.wikimedia.org/D349