zsh-workers
 help / color / mirror / 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; 12+ 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] 12+ messages in thread

* Re: [RFC][PATCH] Try calling command with help flags in run-help
  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-02 20:58   ` Let run-help filter cmd_args before calling run-help-<command> " Marlon Richert
  0 siblings, 2 replies; 12+ messages in thread
From: Bart Schaefer @ 2021-05-25 20:57 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh hackers list

On Tue, May 25, 2021 at 1:49 PM Marlon Richert <marlon.richert@gmail.com> 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.


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

* [PATCH] Let run-help try to show function source from file (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-05-25 20:57 ` Bart Schaefer
@ 2021-06-02 18:26   ` Marlon Richert
  2021-06-02 20:58   ` Let run-help filter cmd_args before calling run-help-<command> " Marlon Richert
  1 sibling, 0 replies; 12+ messages in thread
From: Marlon Richert @ 2021-06-02 18:26 UTC (permalink / raw)
  To: Bart Schaefer, Zsh hackers list

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

Here's part of workers 48926 as a separate patch.

On Tue, May 25, 2021 at 11:57 PM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> On Tue, May 25, 2021 at 1:49 PM Marlon Richert <marlon.richert@gmail.com> 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.

[-- Attachment #2: 0001-Let-run-help-try-to-show-function-source-from-file.txt --]
[-- Type: text/plain, Size: 1276 bytes --]

From 4cbb4642bb9c4f06fbc6729ecf2995e498cf3d61 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Wed, 2 Jun 2021 21:22:59 +0300
Subject: [PATCH] Let run-help try to show function source from file
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

…because parsed functions don't contain comments.
---
 Functions/Misc/run-help | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index e351dd6a6..b5e692198 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -69,7 +69,18 @@ do
 	(comp*) man zshcompsys;;
 	(zf*) man zshftpsys;;
 	(run-help) man zshcontrib;;
-	(*) builtin functions ${what[(w)1]} | ${=PAGER:-more};;
+        ( * )
+          local func=$what[(w)1]
+
+          # Try to show function source from file, because parsed functions
+          # don't contain comments.
+          autoload +X -Uz $func
+          if [[ -n $functions_source[$func] ]]; then
+            ${=PAGER:-more} -- $functions_source[$func]
+          else
+            builtin functions $func | ${=PAGER:-more}
+          fi
+        ;;
 	esac;;
     (*( is a * builtin))
 	case ${what[(w)1]} in
-- 
2.30.1 (Apple Git-130)


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

* Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  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-02 20:58   ` Marlon Richert
  2021-06-03  4:34     ` Bart Schaefer
  1 sibling, 1 reply; 12+ messages in thread
From: Marlon Richert @ 2021-06-02 20:58 UTC (permalink / raw)
  To: Bart Schaefer, Zsh hackers list

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

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

On Tue, May 25, 2021 at 11:57 PM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> On Tue, May 25, 2021 at 1:49 PM Marlon Richert <marlon.richert@gmail.com> 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.

[-- Attachment #2: 0001-Let-run-help-filter-cmd_args-before-calling-run-help.txt --]
[-- Type: text/plain, Size: 3646 bytes --]

From 29d2c303c43039a333e9ad3a5c495f33af85d955 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
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)


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  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
  0 siblings, 2 replies; 12+ messages in thread
From: Bart Schaefer @ 2021-06-03  4:34 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh hackers list

On Wed, Jun 2, 2021 at 1:58 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> And here's another part of workers 48926 as a separate patch.

run-help:115: parse error near `]'

Typo?
+                shift -p (( $#cmd_args + 1 - cmd_args[(i)(-|--|;)] )) cmd_args

This change also breaks the run-help-ssh example in the documentation,
and does so in a way I think it's impossible to fix.

Stupid example, but:

% ssh -- localhost date

Without the patch, run-help displays help for the "date" command.
With it, help for "ssh".

It would not surprise me to find that attempting to generically parse
arguments in run-help would also break helpers for commands like "su"
and "sudo",  some commands that have git-style sub-commands, or .


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-06-03  4:34     ` Bart Schaefer
@ 2021-06-03  4:38       ` Bart Schaefer
  2021-06-03 21:26       ` Marlon Richert
  1 sibling, 0 replies; 12+ messages in thread
From: Bart Schaefer @ 2021-06-03  4:38 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh hackers list

On Wed, Jun 2, 2021 at 9:34 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> and "sudo",  some commands that have git-style sub-commands, or .

A keyboard shortcut or something sent that in the middle of an edit.
Should read:

and "sudo",  or some commands that have git-style sub-commands.


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  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 23:33         ` Bart Schaefer
  1 sibling, 2 replies; 12+ messages in thread
From: Marlon Richert @ 2021-06-03 21:26 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

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

On Thu, Jun 3, 2021 at 7:34 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Wed, Jun 2, 2021 at 1:58 PM Marlon Richert <marlon.richert@gmail.com> wrote:
> > And here's another part of workers 48926 as a separate patch.
>
> run-help:115: parse error near `]'
>
> Typo?
> +                shift -p (( $#cmd_args + 1 - cmd_args[(i)(-|--|;)] )) cmd_args

Apparently. But I've removed that line now in the new version of the
patch (attached).

> This change also breaks the run-help-ssh example in the documentation,
> and does so in a way I think it's impossible to fix.
>
> Stupid example, but:
>
> % ssh -- localhost date
>
> Without the patch, run-help displays help for the "date" command.
> With it, help for "ssh".

That's weird. For me, it gets help for 'ssh', both before and after
the patch (on commit bd328a2).

> It would not surprise me to find that attempting to generically parse
> arguments in run-help would also break helpers for commands like "su"
> and "sudo",

I tried it, but it doesn't seem to be the case.

> or some commands that have git-style sub-commands.

My patch actually fixes an annoying case with 'run-help git'. Try
pressing ^[h on, for example, 'git -C /path/to/zsh log'. Without my
patch, it gives

% run-help git
git is /usr/bin/git
error: invalid key: alias.-C
No manual entry for git--C

With my patch, it correctly displays help for 'git log'.

Anyway, here's a new version of my patch. Let me know what you think.

[-- Attachment #2: 0001-Let-run-help-filter-cmd_args-before-calling-run-help.txt --]
[-- Type: text/plain, Size: 3191 bytes --]

From 1d2349c0d9dcaaa8b464741ed5a608c6f1eb32d1 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Fri, 4 Jun 2021 00:22:59 +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       | 15 +++++++++------
 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, 12 insertions(+), 17 deletions(-)

diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index e351dd6a6..919899a13 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -101,12 +101,15 @@ 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 the command itself & everything before it.
+                shift $cmd_args[(i)${run_help_orig_cmd:-$1}] cmd_args ||
+                    return
+                
+                # Discard options, parameter assignments & paths.
+                cmd_args=( ${cmd_args[@]:#([-+]*|*=*|*/*|\~*)} )
+                
+                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)


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  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 23:33         ` Bart Schaefer
  1 sibling, 1 reply; 12+ messages in thread
From: Lawrence Velázquez @ 2021-06-03 21:45 UTC (permalink / raw)
  To: Marlon Richert, Bart Schaefer; +Cc: zsh-workers

On Thu, Jun 3, 2021, at 5:26 PM, Marlon Richert wrote:
> On Thu, Jun 3, 2021 at 7:34 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
> > Stupid example, but:
> >
> > % ssh -- localhost date
> >
> > Without the patch, run-help displays help for the "date" command.
> > With it, help for "ssh".
> 
> That's weird. For me, it gets help for 'ssh', both before and after
> the patch (on commit bd328a2).

Same for me, on the 5.8 release.

> > or some commands that have git-style sub-commands.
> 
> My patch actually fixes an annoying case with 'run-help git'. Try
> pressing ^[h on, for example, 'git -C /path/to/zsh log'. Without my
> patch, it gives
> 
> % run-help git
> git is /usr/bin/git
> error: invalid key: alias.-C
> No manual entry for git--C
> 
> With my patch, it correctly displays help for 'git log'.

This already works correctly for me on the 5.8 release, but I don't
have any zsh or git aliases configured.

-- 
vq


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-06-03 21:45         ` Lawrence Velázquez
@ 2021-06-03 21:52           ` Bart Schaefer
  2021-06-03 22:00             ` Lawrence Velázquez
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2021-06-03 21:52 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Marlon Richert, Zsh hackers list

On Thu, Jun 3, 2021 at 2:46 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> On Thu, Jun 3, 2021, at 5:26 PM, Marlon Richert wrote:
> > On Thu, Jun 3, 2021 at 7:34 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
> > > Stupid example, but:
> > >
> > > % ssh -- localhost date
> > >
> > > Without the patch, run-help displays help for the "date" command.
> > > With it, help for "ssh".
> >
> > That's weird. For me, it gets help for 'ssh', both before and after
> > the patch (on commit bd328a2).
>
> Same for me, on the 5.8 release.

The run-help-ssh function isn't actually in the distribution anywhere
except as an example in the doc.  You have to copy-paste it into your
shell before attempting run-help.


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-06-03 21:52           ` Bart Schaefer
@ 2021-06-03 22:00             ` Lawrence Velázquez
  0 siblings, 0 replies; 12+ messages in thread
From: Lawrence Velázquez @ 2021-06-03 22:00 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Marlon Richert, zsh-workers

On Thu, Jun 3, 2021, at 5:52 PM, Bart Schaefer wrote:
> The run-help-ssh function isn't actually in the distribution anywhere
> except as an example in the doc.  You have to copy-paste it into your
> shell before attempting run-help.

Oops, I missed that you're talking about run-help-X, which I didn't
know about.  Carry on!

-- 
vq


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-06-03 21:26       ` Marlon Richert
  2021-06-03 21:45         ` Lawrence Velázquez
@ 2021-06-03 23:33         ` Bart Schaefer
  2021-06-05 19:15           ` Marlon Richert
  1 sibling, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2021-06-03 23:33 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh hackers list

On Thu, Jun 3, 2021 at 2:27 PM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> On Thu, Jun 3, 2021 at 7:34 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
> > Stupid example, but:
> >
> > % ssh -- localhost date
> >
> > Without the patch, run-help displays help for the "date" command.
> > With it, help for "ssh".
>
> That's weird. For me, it gets help for 'ssh', both before and after
> the patch (on commit bd328a2).

As mentioned elsewhere, you do have to manually load run-help-ssh into
your test shell, it is not an autoloadable file.

> My patch actually fixes an annoying case with 'run-help git'. Try
> pressing ^[h on, for example, 'git -C /path/to/zsh log'.

This works for me, with or without your patch, using the git
completion in the current repository head.  Are you sure you're
loading functions from the right path?

> Anyway, here's a new version of my patch. Let me know what you think.

I don't see any obvious problems with it but I don't know how to
construct a test case for any of the helpers you've edited.  It would
be nice to know that this does not break the un-edited versions of
those helpers, because if those changes are required rather than just
redundant code removal, other helpers in the wild would also require
editing.


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

* Re: Let run-help filter cmd_args before calling run-help-<command> (was Re: [RFC][PATCH] Try calling command with help flags in run-help)
  2021-06-03 23:33         ` Bart Schaefer
@ 2021-06-05 19:15           ` Marlon Richert
  0 siblings, 0 replies; 12+ messages in thread
From: Marlon Richert @ 2021-06-05 19:15 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list


[-- Attachment #1.1: Type: text/plain, Size: 768 bytes --]

On Fri, Jun 4, 2021 at 2:33 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> > Anyway, here's a new version of my patch. Let me know what you think.
>
> I don't see any obvious problems with it but I don't know how to
> construct a test case for any of the helpers you've edited.  It would
> be nice to know that this does not break the un-edited versions of
> those helpers, because if those changes are required rather than just
> redundant code removal, other helpers in the wild would also require
> editing.
>

New patch here with test cases, plus a rewrite of run-help-git. All the
tests succeed for both the new and old versions of the run-help-* functions
(except for the old run-help-git, because the new run-help-git uses `git
help` instead of `man`).

[-- Attachment #1.2: Type: text/html, Size: 1111 bytes --]

[-- Attachment #2: 0001-Let-run-help-filter-cmd_args-before-calling-run-help.txt --]
[-- Type: text/plain, Size: 5998 bytes --]

From 21257c087fdf8896cc7362ed432a76de7f4a1fff Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Sat, 5 Jun 2021 22:06:50 +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       |  15 +++--
 Functions/Misc/run-help-btrfs |   4 --
 Functions/Misc/run-help-git   |  10 +---
 Functions/Misc/run-help-ip    |   4 --
 Functions/Misc/run-help-p4    |   2 +-
 Functions/Misc/run-help-svk   |   2 +-
 Functions/Misc/run-help-svn   |   2 +-
 Test/Z03run-help.ztst         | 103 ++++++++++++++++++++++++++++++++++
 8 files changed, 116 insertions(+), 26 deletions(-)
 create mode 100644 Test/Z03run-help.ztst

diff --git a/Functions/Misc/run-help b/Functions/Misc/run-help
index e351dd6a6..d52c1b032 100644
--- a/Functions/Misc/run-help
+++ b/Functions/Misc/run-help
@@ -101,12 +101,15 @@ 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 the command itself & everything before it.
+                shift $cmd_args[(i)${run_help_orig_cmd:-$1}] cmd_args ||
+                    return
+
+                # Discard options, parameter assignments & paths.
+                cmd_args=( ${cmd_args[@]:#([-+]*|*=*|*/*|\~*)} )
+
+                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-git b/Functions/Misc/run-help-git
index ce94d0d02..a841f89d6 100644
--- a/Functions/Misc/run-help-git
+++ b/Functions/Misc/run-help-git
@@ -1,9 +1 @@
-if [ $# -eq 0 ]; then
-    man git
-else
-    local al
-    if al=$(git config --get "alias.$1"); then
-        1=${al%% *}
-    fi
-    man git-$1
-fi
+git help ${1:-git}
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}
diff --git a/Test/Z03run-help.ztst b/Test/Z03run-help.ztst
new file mode 100644
index 000000000..1f280fea7
--- /dev/null
+++ b/Test/Z03run-help.ztst
@@ -0,0 +1,103 @@
+%prep
+  PAGER=cat
+  unalias run-help
+  autoload +X -Uz $PWD/../Functions/Misc/run-help*
+  builtin() {
+    case "$1 $2" in
+      ( 'getln cmd_args' )
+        cmd_args="$BUFFER_STACK"
+      ;;
+      ( 'print -z' )
+      ;;
+      ( 'whence -va' )
+        print -l "$3 is WHENCE:{$3}"
+      ;;
+      ( * )
+        eval $@
+      ;;
+    esac
+  }
+  man() {
+    [[ $1 == -w && -n $NO_SUBCMD_MANUALS ]] &&
+        return 1
+    print "MAN:{${(qq)@}}"
+  }
+  git svn () {
+    print "${(U)0}:{${(qq)@}}"
+  }
+  
+  
+%test
+
+  BUFFER_STACK='btrfs --help'
+  run-help btrfs
+0:btrfs with option flag, no subcmd
+>btrfs is WHENCE:{btrfs}
+>MAN:{'btrfs'}
+
+  BUFFER_STACK='btrfs subvolume snapshot –r /btrfs/SV1 /btrfs/SV1-rosnap'
+  run-help btrfs
+0:btrfs with subcmd
+>btrfs is WHENCE:{btrfs}
+>MAN:{'btrfs-subvolume'}
+
+  BUFFER_STACK="sudo $BUFFER_STACK"
+  run-help btrfs
+0:sudo btrfs with subcmd
+>btrfs is WHENCE:{btrfs}
+>MAN:{'btrfs-subvolume'}
+
+  BUFFER_STACK='ip addr add 192.168.50.5 dev eth1'
+  run-help ip
+0:ip with subcmd
+>ip is WHENCE:{ip}
+>MAN:{'ip-address'}
+
+  NO_SUBCMD_MANUALS=1
+  run-help ip
+  unset NO_SUBCMD_MANUALS
+0:ip with subcmd, but no subcmd manuals
+>ip is WHENCE:{ip}
+>MAN:{'ip'}
+
+  BUFFER_STACK='ip -s -s link ls up'
+  run-help ip
+0:ip with options and subcmd
+>ip is WHENCE:{ip}
+>MAN:{'ip-link'}
+
+  BUFFER_STACK="sudo $BUFFER_STACK"
+  run-help ip
+0:sudo ip with options and subcmd
+>ip is WHENCE:{ip}
+>MAN:{'ip-link'}
+
+  BUFFER_STACK='svn -vq'
+  run-help svn
+0:svn with options
+>svn is WHENCE:{svn}
+>SVN:{'help'}
+
+  BUFFER_STACK+=' commit -m "log messages"'
+  run-help svn
+0:svn with options and subcmd
+>svn is WHENCE:{svn}
+>SVN:{'help' 'commit'}
+
+  BUFFER_STACK='git --exec-path'
+0:git with option
+>git is WHENCE:{git}
+>GIT:{'help' 'git'}
+
+  BUFFER_STACK='git -C $PWD/.. difftool --no-prompt --tool opendiff --dir-diff'
+0:git with option, file & subcmd
+>git is WHENCE:{git}
+>GIT:{'help' 'difftool'}
+
+  BUFFER_STACK='git -c http.proxy=someproxy clone https://github.com/user/repo.git'
+0:git with option, assignment & subcmd
+>git is WHENCE:{git}
+>GIT:{'help' 'clone'}
+
+
+%clean
-- 
2.30.1 (Apple Git-130)


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

end of thread, other threads:[~2021-06-05 19:16 UTC | newest]

Thread overview: 12+ 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-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

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ http://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git