From 1e79ff547171d32dc6ff585a5c704bb7f0809bc7 Mon Sep 17 00:00:00 2001
From: LingxianKong <konglingxian@huawei.com>
Date: Mon, 18 May 2015 09:24:09 +0800
Subject: [PATCH] Add bash-completion command support

Implement a helper command 'mistral bash-completion' to generate available
commands and options supported by mistral.

Change-Id: Id0e52daf4ee3f7c1ba9fefaa4f103bd74e66d97d
Partically Implements: blueprint bash-completion-script-optimization
---
 mistralclient/shell.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/mistralclient/shell.py b/mistralclient/shell.py
index fcf30d95..89df2fda 100644
--- a/mistralclient/shell.py
+++ b/mistralclient/shell.py
@@ -31,6 +31,7 @@ import mistralclient.commands.v2.workflows
 from mistralclient.openstack.common import cliutils as c
 
 from cliff import app
+from cliff import command
 from cliff import commandmanager
 
 import argparse
@@ -80,6 +81,22 @@ class HelpAction(argparse.Action):
         sys.exit(0)
 
 
+class BashCompletionCommand(command.Command):
+    """Prints all of the commands and options for bash-completion."""
+
+    def take_action(self, parsed_args):
+        commands = set()
+        options = set()
+
+        for option, _action in self.app.parser._option_string_actions.items():
+            options.add(option)
+
+        for command_name, _cmd in self.app.command_manager:
+            commands.add(command_name)
+
+        print(' '.join(commands | options))
+
+
 class MistralShell(app.App):
 
     def __init__(self):
@@ -257,6 +274,7 @@ class MistralShell(app.App):
     @staticmethod
     def _get_commands_v2():
         return {
+            'bash-completion': BashCompletionCommand,
             'workbook-list': mistralclient.commands.v2.workbooks.List,
             'workbook-get': mistralclient.commands.v2.workbooks.Get,
             'workbook-create': mistralclient.commands.v2.workbooks.Create,