controllers_with_same_label.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. .. _controllers_with_same_label:
  2. Multiple Controllers With Same Label
  3. ------------------------------------
  4. There are many ways to use controllers. In some circumstances you might find
  5. that you want to have two controllers with the same label but stacked on
  6. different parent controllers. This is a problem because controller labels
  7. must be unique.
  8. Take for example the situation where you want to have a 'list' controller,
  9. rather than a 'list' function of another controller. You might call this
  10. as:
  11. .. code-block:: text
  12. $ myapp <controller1> <list_controller>
  13. $ myapp <controller2> <some_other_list_controller>
  14. In both cases, you would probably want the 'sub-controller' or sub-command to
  15. be 'list'. This is possible with the use of the 'aliases' and 'aliases_only'
  16. Meta options. Take the following code as an example where we have a 'users'
  17. and a 'hosts' controller and we want to have a 'list' sub-command under both:
  18. .. code-block:: python
  19. from cement.core.foundation import CementApp
  20. from cement.core.controller import CementBaseController, expose
  21. # define application controllers
  22. class MyAppBaseController(CementBaseController):
  23. class Meta:
  24. label = 'base'
  25. class UsersController(CementBaseController):
  26. class Meta:
  27. label = 'users'
  28. description = "this is the users controller"
  29. stacked_on = 'base'
  30. stacked_type = 'nested'
  31. class HostsController(CementBaseController):
  32. class Meta:
  33. label = 'hosts'
  34. description = "this is the hosts controller"
  35. stacked_on = 'base'
  36. stacked_type = 'nested'
  37. class UsersListController(CementBaseController):
  38. class Meta:
  39. label = 'users_list'
  40. description = 'list all available users'
  41. aliases = ['list']
  42. aliases_only = True
  43. stacked_on = 'users'
  44. stacked_type = 'nested'
  45. @expose(hide=True)
  46. def default(self):
  47. print "Inside UsersListController.default()"
  48. class HostsListController(CementBaseController):
  49. class Meta:
  50. label = 'hosts_list'
  51. description = 'list all available hosts'
  52. aliases = ['list']
  53. aliases_only = True
  54. stacked_on = 'hosts'
  55. stacked_type = 'nested'
  56. @expose(hide=True)
  57. def default(self):
  58. print "Inside HostsListController.default()"
  59. class MyApp(CementApp):
  60. class Meta:
  61. label = 'myapp'
  62. handlers = [
  63. MyAppBaseController,
  64. UsersController,
  65. HostsController,
  66. UsersListController,
  67. HostsListController,
  68. ]
  69. def main():
  70. with MyApp() as app:
  71. app.run()
  72. if __name__ == '__main__':
  73. main()
  74. .. code-block:: text
  75. $ python myapp.py --help
  76. usage: myapp.py (sub-commands ...) [options ...] {arguments ...}
  77. Base Controller
  78. commands:
  79. hosts
  80. this is the hosts controller
  81. users
  82. this is the users controller
  83. optional arguments:
  84. -h, --help show this help message and exit
  85. --debug toggle debug output
  86. --quiet suppress all output
  87. $ python myapp.py users --help
  88. usage: myapp.py (sub-commands ...) [options ...] {arguments ...}
  89. this is the users controller
  90. commands:
  91. list
  92. list all available users
  93. optional arguments:
  94. -h, --help show this help message and exit
  95. --debug toggle debug output
  96. --quiet suppress all output
  97. $ python myapp.py users list
  98. Inside UsersListController.default()
  99. $ python myapp.py hosts list
  100. Inside HostsListController.default()