From: Marlon Richert <marlon.richert@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [RFC][PATCH] Try calling command with help flags in run-help
Date: Tue, 25 May 2021 23:47:42 +0300 [thread overview]
Message-ID: <CAHLkEDsDG7X9+oN8AowUoqcPU731tHALXJGvaqkxV8XnR2kULg@mail.gmail.com> (raw)
[-- 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
next reply other threads:[~2021-05-25 20:48 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-25 20:47 Marlon Richert [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAHLkEDsDG7X9+oN8AowUoqcPU731tHALXJGvaqkxV8XnR2kULg@mail.gmail.com \
--to=marlon.richert@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).