Set subparser argument required

When subparser argument is not provided by user
argparse return an error message not really useful for user:

    'Namespace' object has no attribute 'func'

This is due to the fact that when we launch the pbr in cli mode
the subparser argument is not mandatory (required) and directly
we try to execute a undefined function.

Set the subparser required is more helpful for users due to the
fact that argparse display the helping message with the available
sub-commands that users can use

These changes provides the following output if the argument is not
passed:

usage: pbr [-h] [-v] {sha,info,freeze} ...
main.py: error: too few arguments

Change-Id: I7982f9d40cb0979ddb89d7bc53964167f8e4b269
This commit is contained in:
Hervé Beraud 2019-03-25 15:34:56 +01:00 committed by Sorin Sbarnea
parent 30926f6dbe
commit 713aff286b
1 changed files with 3 additions and 1 deletions

View File

@ -86,7 +86,9 @@ def main():
version=str(pbr.version.VersionInfo('pbr')))
subparsers = parser.add_subparsers(
title='commands', description='valid commands', help='additional help')
title='commands', description='valid commands', help='additional help',
dest='cmd')
subparsers.required = True
cmd_sha = subparsers.add_parser('sha', help='print sha of package')
cmd_sha.set_defaults(func=get_sha)