Skip to content

Instantly share code, notes, and snippets.

@lusis
Created October 13, 2010 20:03
Show Gist options
  • Save lusis/624782 to your computer and use it in GitHub Desktop.
Save lusis/624782 to your computer and use it in GitHub Desktop.
import argparse
from mock import Mock
m = Mock()
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
query_group = subparsers.add_parser('query')
add_group = subparsers.add_parser('add')
update_group = subparsers.add_parser('update')
subparsers = query_group.add_subparsers()
host_query = subparsers.add_parser('host')
host_query.add_argument('host_name')
host_query.set_defaults(func=m.query_host)
profile_query = subparsers.add_parser('profile')
profile_query.add_argument('profile_name')
profile_query.set_defaults(func=m.query_profile)
environment_query = subparsers.add_parser('environment')
environment_query.add_argument('environment_name')
environment_query.set_defaults(func=m.query_environment)
subparsers = add_group.add_subparsers()
host_add = subparsers.add_parser('host')
host_add.add_argument('host_name')
host_add.set_defaults(func=m.add_host)
profile_add = subparsers.add_parser('profile')
profile_add.add_argument('profile_name')
profile_add.set_defaults(func=m.add_profile)
environment_add = subparsers.add_parser('environment')
environment_add.add_argument('environment_name')
environment_add.set_defaults(func=m.add_environment)
subparsers = update_group.add_subparsers()
host_update = subparsers.add_parser('host')
host_update.add_argument('host_name')
host_update.set_defaults(func=m.update_host)
profile_update = subparsers.add_parser('profile')
profile_update.add_argument('profile_name')
profile_update.set_defaults(func=m.update_profile)
environment_update = subparsers.add_parser('environment')
environment_update.add_argument('environment_name')
environment_update.set_defaults(func=m.update_environment)
options = parser.parse_args()
options.func(options)
print m.method_calls
@lusis
Copy link
Author

lusis commented Oct 13, 2010

$ ./test.py -h
usage: test.py [-h] {query,add,update} ...

positional arguments:
  {query,add,update}

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

$ ./test.py query -h
usage: test.py query [-h] {profile,environment,host} ...

positional arguments:
  {profile,environment,host}

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

$ ./test.py query profile -h
usage: test.py query profile [-h] profile_name

positional arguments:
  profile_name

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

$ ./test.py query profile qa
[('query_profile', (Namespace(func=<Mock name='mock.query_profile' id='3082886348'>, profile_name='qa'),), {})]

@Kif11
Copy link

Kif11 commented Jul 17, 2017

Great example. Thank you. 👍

@twskipper
Copy link

thank you

@debayanray
Copy link

thank you!

@GreatBahram
Copy link

Thanks!

@dominiquesydow
Copy link

Thank you!

@ZhymabekRoman
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment