Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)



And here's another part of workers 48926 as a separate patch.

On Tue, May 25, 2021 at 11:57 PM Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> On Tue, May 25, 2021 at 1:49 PM Marlon Richert <marlon.richert@xxxxxxxxx> wrote:
> >
> > When there isn't a man page, try calling the command with --help or -h.
>
> This is not a good idea, because we can't predict what will happen if
> a given command does not support those options, or interprets them
> differently.
From 29d2c303c43039a333e9ad3a5c495f33af85d955 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@xxxxxxxxx>
Date: Wed, 2 Jun 2021 22:58:42 +0300
Subject: [PATCH] Let run-help filter cmd_args before calling
 run-help-<command>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

…to make it easier to write run-help-<command> functions.
---
 Functions/Misc/run-help       | 27 +++++++++++++++++++++------
 Functions/Misc/run-help-btrfs |  4 ----
 Functions/Misc/run-help-ip    |  4 ----
 Functions/Misc/run-help-p4    |  2 +-
 Functions/Misc/run-help-svk   |  2 +-
 Functions/Misc/run-help-svn   |  2 +-
 6 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index e351dd6a6..19f51a7f8 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -101,12 +101,27 @@ do
 		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]}"
+                
+                # Discard environment assignments, etc.
+                while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]]; do
+                  shift cmd_args || 
+                      return 1
+                done
+                
+                # Discard the command itself.
+                shift cmd_args
+
+                # Discard the first -, -- or ; and everything after it.
+                shift -p (( $#cmd_args + 1 - cmd_args[(i)(-|--|;)] )) cmd_args
+
+                # Discard options, parameter assignments & paths.
+                while [[ $#cmd_args[@] -gt 0 && 
+                    ( $cmd_args[1] == ([-+]|*=*|*/*|~*) || 
+                        -e ${(e)~cmd_args[1]} ) ]]; do
+                  shift cmd_args
+                done
+                
+                eval "run-help-$1:t ${(@q)cmd_args}"
 	    else
 		POSIXLY_CORRECT=1 man $@:t
 	    fi
diff --git a/Functions/Misc/run-help-btrfs b/Functions/Misc/run-help-btrfs
index 0dc1dabcb..cb139e9b7 100644
--- a/Functions/Misc/run-help-btrfs
+++ b/Functions/Misc/run-help-btrfs
@@ -1,7 +1,3 @@
-while [[ $# != 0 && $1 == -* ]]; do
-    shift
-done
-
 case $1 in
     (b*)    man btrfs-balance          ;;
     (c*)    man btrfs-check            ;;
diff --git a/Functions/Misc/run-help-ip b/Functions/Misc/run-help-ip
index 8807f9ef1..b811ce352 100644
--- a/Functions/Misc/run-help-ip
+++ b/Functions/Misc/run-help-ip
@@ -14,10 +14,6 @@ if ! man -w ip-address >/dev/null 2>&1; then
     return
 fi
 
-while [[ $# != 0 && $1 == -* ]]; do
-    shift
-done
-
 case $1 in
     (addrl*) man ip-addrlabel ;;
     (a*) man ip-address ;;
diff --git a/Functions/Misc/run-help-p4 b/Functions/Misc/run-help-p4
index 662ce94fe..e48a4d068 100644
--- a/Functions/Misc/run-help-p4
+++ b/Functions/Misc/run-help-p4
@@ -2,4 +2,4 @@ if (( ! $# )); then
   p4 help commands
 else
   p4 help $1
-fi | ${=PAGER:-less}
+fi | ${=PAGER:-more}
diff --git a/Functions/Misc/run-help-svk b/Functions/Misc/run-help-svk
index 92438a53f..782538246 100644
--- a/Functions/Misc/run-help-svk
+++ b/Functions/Misc/run-help-svk
@@ -1 +1 @@
-svk help ${${@:#-*}[1]} | ${=PAGER:-more}
+svk help $1 | ${=PAGER:-more}
diff --git a/Functions/Misc/run-help-svn b/Functions/Misc/run-help-svn
index 5d1068588..d55a493a6 100644
--- a/Functions/Misc/run-help-svn
+++ b/Functions/Misc/run-help-svn
@@ -1 +1 @@
-svn help ${${@:#-*}[1]} | ${=PAGER:-more}
+svn help $1 | ${=PAGER:-more}
-- 
2.30.1 (Apple Git-130)



Messages sorted by: Reverse Date, Date, Thread, Author