zsh-workers
 help / color / mirror / code / Atom feed
* [RFC][PATCH] Try calling command with help flags in run-help
@ 2021-05-25 20:47 Marlon Richert
  2021-05-25 20:57 ` Bart Schaefer
  0 siblings, 1 reply; 20+ messages in thread
From: Marlon Richert @ 2021-05-25 20:47 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 139 bytes --]

When there isn't a man page, try calling the command with --help or -h.
Additionally, be a bit smarter about showing function source code.

[-- Attachment #2: 0001-Try-calling-command-with-help-flags-in-run-help.txt --]
[-- Type: text/plain, Size: 4160 bytes --]

From ba0fa464c73b10decb33582156aa214953cdd861 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Tue, 25 May 2021 23:45:28 +0300
Subject: [PATCH] Try calling command with help flags in run-help

When there isn't a man page, try calling the command with --help or -h.
Additionally, be a bit smarter about showing function source code.
---
 Functions/Misc/run-help | 108 ++++++++++++++++++++++++++++++----------
 1 file changed, 81 insertions(+), 27 deletions(-)

diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index e351dd6a6..dc0490342 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -8,6 +8,17 @@
 #	autoload -Uz run-help
 #
 
+.run-help.eval() {
+  output="$( eval "COLUMNS=$COLUMNS $1" 2>&1 )" ||
+      return
+
+  [[ -n $output ]] ||
+      return
+
+  print "$output" | ${=PAGER:-more}
+}
+
+run-help() {
 emulate -RL zsh
 
 local HELPDIR="${HELPDIR:-@runhelpdir@}"
@@ -64,13 +75,6 @@ do
 	[[ ${what[(w)6]:t} != ${what[(w)1]} ]] &&
 	  run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t}
 	;;
-    (*( is a * function))
-	case ${what[(w)1]} in
-	(comp*) man zshcompsys;;
-	(zf*) man zshftpsys;;
-	(run-help) man zshcontrib;;
-	(*) builtin functions ${what[(w)1]} | ${=PAGER:-more};;
-	esac;;
     (*( is a * builtin))
 	case ${what[(w)1]} in
 	(compctl) man zshcompctl;;
@@ -92,26 +96,73 @@ do
     (*( is a reserved word))
 	man zshmisc
 	;;
-    (*)
-	if ((! didman++))
-	then
-	    if whence "run-help-$1:t" >/dev/null
-	    then
-		local cmd_args
-		builtin getln cmd_args
-		builtin print -z "$cmd_args"
-		cmd_args=( ${(z)cmd_args} )
-		# Discard environment assignments, etc.
-		while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]]
-		do
-		    shift cmd_args || return 1
-		done
-		eval "run-help-$1:t ${(q@)cmd_args[2,-1]}"
-	    else
-		POSIXLY_CORRECT=1 man $@:t
-	    fi
-	fi
-	;;
+    ( comp*( is a* function)* )
+      man zshcompsys
+    ;;
+    ( zf*( is a* function)* )
+      man zshzftpsys
+    ;;
+    ( ((run-help*|which-command) is a* function)* )
+      man zshcontrib
+    ;;
+    ( * )
+      if (( ! didman++ )); then
+        local cmd_args help
+        builtin read -zr cmd_args     # Get the original command line.
+        builtin print -z "$cmd_args"  # Put it back on the buffer stack.
+
+        # Retain only subcommands & options.
+        cmd_args=( ${${(z)cmd_args}[(r)${run_help_orig_cmd:-$1},(r)(-|--)]} )
+        (( $#cmd_args )) &&
+            shift cmd_args
+
+        whence "run-help-$1:t" >/dev/null &&
+            eval "run-help-$1:t ${(@q)cmd_args}" &&
+            return
+
+        # For safety, skip all option flags & anything that looks like a file.
+        while [[ $#cmd_args -gt 0 &&
+            ( -e $~cmd_args[1] || $cmd_args[1] == [-+]* ) ]]; do
+          shift cmd_args
+        done
+
+        # Try if we're dealing with a subcommand and can get help on that.
+        if [[ -n $cmd_args[1] ]]; then
+          # The order in which we try these matters.
+          for help in "$cmd_args[1] "{--help,-h} {-h,--help}" $cmd_args[1]"; do
+            .run-help.eval "$1:t $help" &&
+                return
+          done
+        fi
+
+        # Try the man page.
+        POSIXLY_CORRECT=1 man $1:t 2>/dev/null &&
+            return
+
+        # Try getting help on the main command.
+        for help in -h --help; do
+          .run-help.eval "$1:t $help" &&
+              return
+        done
+
+        if [[ $what = *( is a* function)* ]]; then
+          local func=$what[(w)1]
+
+          # Try to show function source from file, because parsed functions
+          # don't contain comments.
+          autoload +X -Uz $func
+          [[ -n $functions_source[$func] ]] &&
+              ${=PAGER:-more} -- $functions_source[$func] &&
+              return
+
+          builtin functions $func | ${=PAGER:-more} &&
+              return
+        fi
+
+        print -u2 "run-help: no help found for '$what[(w)1]'"
+        return 1
+      fi
+    ;;
     esac
     if ((i < $#places && ! didman))
     then
@@ -124,3 +175,6 @@ done
 } always {
   unset run_help_orig_cmd
 }
+}
+
+run-help "$@"
-- 
2.31.1


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2021-07-30 16:56 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-25 20:47 [RFC][PATCH] Try calling command with help flags in run-help Marlon Richert
2021-05-25 20:57 ` Bart Schaefer
2021-06-02 18:26   ` [PATCH] Let run-help try to show function source from file (was Re: [RFC][PATCH] Try calling command with help flags in run-help) Marlon Richert
2021-06-20 21:23     ` Lawrence Velázquez
2021-07-18 18:38       ` Lawrence Velázquez
2021-07-28  2:03       ` Bart Schaefer
2021-06-02 20:58   ` Let run-help filter cmd_args before calling run-help-<command> " Marlon Richert
2021-06-03  4:34     ` Bart Schaefer
2021-06-03  4:38       ` Bart Schaefer
2021-06-03 21:26       ` Marlon Richert
2021-06-03 21:45         ` Lawrence Velázquez
2021-06-03 21:52           ` Bart Schaefer
2021-06-03 22:00             ` Lawrence Velázquez
2021-06-03 23:33         ` Bart Schaefer
2021-06-05 19:15           ` Marlon Richert
2021-06-20 18:01             ` Lawrence Velázquez
2021-07-18 18:45               ` Lawrence Velázquez
2021-07-28 17:58                 ` Bart Schaefer
2021-07-29 12:11                   ` Marlon Richert
2021-07-30 16:55                     ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).