[PATCH] Add support for showing help from extensions

HacKan hackan at gmail.com
Wed Mar 22 23:58:00 CET 2017


When help command is issued, extensions installed are listed and, if
they support it, help from them is displayed; if they don't, a message
stating that there's no help available for them is displayed.
---
 src/password-store.sh | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index bad8d4f..2a9cad6 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -142,6 +142,18 @@ check_sneaky_paths() {
 	done
 }
 
+in_array() {
+	local seeking="$1"; shift
+	local in=1
+	for element; do
+		if [[ "$element" == "$seeking" ]]; then
+			in=0
+			break
+		fi
+	done
+	return $in
+}
+
 #
 # END helper functions
 #
@@ -293,8 +305,38 @@ cmd_usage() {
 	    $PROGRAM version
 	        Show version information.
 
-	More information may be found in the pass(1) man page.
 	_EOF
+	
+	if [[ (-n "$SYSTEM_EXTENSION_DIR" && -n "$(ls "$SYSTEM_EXTENSION_DIR"/*.bash 2>/dev/null)" ) || ($PASSWORD_STORE_ENABLE_EXTENSIONS == true && -n "$(ls "$EXTENSIONS"/*.bash 2>/dev/null)") ]]; then
+		echo "From extensions:"
+		local -a extdirs=( "$EXTENSIONS" "$SYSTEM_EXTENSION_DIR" )
+		local extdir ext extname exthelp extensions_called=()
+		for extdir in "${extdirs[@]}"; do
+			for ext in "$extdir"/*.bash; do
+				if [[ -r "$ext" && -x "$ext" ]]; then
+					extname="$(basename "$ext")"
+					# If extension was called from one dir, do not call it again
+					in_array "$extname" "${extensions_called[@]}" && continue
+					
+					echo "    $PROGRAM ${extname%.*}"
+					# Extract help function, that must be called as help_extensionname()
+					exthelp="$(sed -nE "/^(function)?\s?help_${extname%.*}\(\)/,/^}/p" "$ext")"
+					if [[ -z "$exthelp" ]]; then
+						# Function inexistent
+						echo "        (no help available)"
+					else
+						# Call it
+						eval "${exthelp}; help_${extname%.*}"
+					fi
+					extensions_called+=("$extname")
+				fi
+			done
+		done
+		[[ ${#extensions_called[@]} -eq 0 ]] && echo "    (no extensions enabled)"
+		echo
+	fi
+	
+	echo "More information may be found in the ${PROGRAM}(1) man page."
 }
 
 cmd_init() {
-- 
2.11.0



More information about the Password-Store mailing list