Complementing cli commands in bash 62/36662/7
authorMateusz Malicki <m.malicki2@samsung.com>
Wed, 11 Mar 2015 11:57:02 +0000 (12:57 +0100)
committerJan Olszak <j.olszak@samsung.com>
Thu, 12 Mar 2015 09:11:43 +0000 (02:11 -0700)
[Bug/Feature]   Complementing cli commands in bash
[Cause]         N/A
[Solution]      N/A
[Verification]  Check completion (you must first enable bash completion or
                execute: source /etc/bash_completion.d/vasum-cli-completion.sh)

Change-Id: I256ed064a128f03874b2e4655ccb818058a13897

cli/CMakeLists.txt
cli/command-line-interface.cpp
cli/main.cpp
cli/support/vasum-cli-completion.sh.in [new file with mode: 0755]
packaging/vasum.spec

index f28e65b..2ffdcc4 100644 (file)
@@ -33,6 +33,11 @@ INCLUDE_DIRECTORIES(${CLIENT_FOLDER})
 INCLUDE_DIRECTORIES(${COMMON_FOLDER})
 TARGET_LINK_LIBRARIES(${CLI_CODENAME} ${LIB_DEPS_LIBRARIES} ${PROJECT_NAME})
 
+CONFIGURE_FILE(support/vasum-cli-completion.sh.in
+               ${CMAKE_BINARY_DIR}/vasum-cli-completion.sh
+               @ONLY)
 
 ## Install #####################################################################
 INSTALL(TARGETS ${CLI_CODENAME} DESTINATION bin)
+INSTALL(FILES   ${CMAKE_BINARY_DIR}/vasum-cli-completion.sh
+        DESTINATION ${SYSCONF_INSTALL_DIR}/bash_completion.d)
index 77e9dc5..ecf6b9c 100644 (file)
@@ -174,10 +174,12 @@ void CommandLineInterface::printUsage(std::ostream& out) const
 {
     out << mUsage << "\n\n"
         << "\tDescription\n"
-        << "\t\t" << mUsageInfo << "\n\n"
-        << "\tOptions\n";
-    for (const auto& args : mArgsSpec) {
-        out << "\t\t" << args.first << " -- " << args.second << "\n";
+        << "\t\t" << mUsageInfo << "\n";
+    if (!mArgsSpec.empty()) {
+        out << "\n\tOptions\n";
+        for (const auto& args : mArgsSpec) {
+            out << "\t\t" << args.first << " -- " << args.second << "\n";
+        }
     }
     out << "\n";
 }
index 5f073be..a30146e 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdexcept>
 #include <string>
 #include <iostream>
+#include <algorithm>
 
 using namespace vasum::cli;
 
@@ -241,7 +242,7 @@ std::map<std::string, CommandLineInterface> commands = {
 
 void printUsage(std::ostream& out, const std::string& name)
 {
-    out << "Usage: " << name << " [command [args]]\n\n"
+    out << "Usage: " << name << " [command [-h|args]]\n\n"
         << "command can be one of the following:\n";
 
     for (const auto& command : commands) {
@@ -263,6 +264,12 @@ int main(const int argc, const char** argv)
     }
 
     CommandLineInterface& command = commands[argv[1]];
+    auto it = std::find(argv, argv+argc, std::string("-h"));
+    if (it != argv + argc) {
+            command.printUsage(std::cout);
+            return EXIT_SUCCESS;
+    }
+
     try {
         command.execute(1, argc, argv);
     } catch (const std::runtime_error& ex) {
diff --git a/cli/support/vasum-cli-completion.sh.in b/cli/support/vasum-cli-completion.sh.in
new file mode 100755 (executable)
index 0000000..79a5dad
--- /dev/null
@@ -0,0 +1,15 @@
+# Check for bash
+[ -z "$BASH_VERSION" ] && return
+
+__@PROJECT_NAME@_cli() {
+    local cur="${COMP_WORDS[COMP_CWORD]}"
+
+    COMPREPLY=()
+    if [ "$COMP_CWORD" == "1" ]; then
+        COMPREPLY=($(compgen -W "$(@CLI_CODENAME@ | grep -e '^\S' | tail -n +3 | cut -f1 -d' ')" -- $cur))
+    elif [ "$COMP_CWORD" == "2" ]; then
+        COMPREPLY=($(compgen -W "-h" -- $cur))
+    fi
+}
+
+complete -F __@PROJECT_NAME@_cli @CLI_CODENAME@
index bd7e9f1..6cea869 100644 (file)
@@ -198,6 +198,17 @@ Command Line Interface for vasum.
 %defattr(644,root,root,755)
 %attr(755,root,root) %{_bindir}/vasum-cli
 
+%package cli-completion
+Summary:          Vasum Command Line Interface bash completion
+Group:            Security/Other
+Requires:         vasum-cli = %{version}-%{release}
+#Requires:         bash-completion
+
+%description cli-completion
+Command Line Interface bash completion.
+
+%files cli-completion
+%attr(755,root,root) %{_sysconfdir}/bash_completion.d/vasum-cli-completion.sh
 
 ## Test Package ################################################################
 %package tests