From 21d1b727413e8649436431806ec6a2731c9fd28b Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Mon, 24 Jan 2022 21:08:50 -0800 Subject: [PATCH 2/2] Treat "-" as a positional arg in 'eshell-eval-using-options' * lisp/eshell/esh-opt.el (eshell--process-args): Treat "-" as a positional arg. * lisp/eshell/em-tramp.el (eshell/su): Simplify checking for "-". * test/lisp/eshell/esh-opt-tests.el (esh-opt-test/eval-using-options-stdin): New test. --- lisp/eshell/em-tramp.el | 9 ++------- lisp/eshell/esh-opt.el | 9 ++++++--- test/lisp/eshell/esh-opt-tests.el | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lisp/eshell/em-tramp.el b/lisp/eshell/em-tramp.el index 791458822d..2afd4fe066 100644 --- a/lisp/eshell/em-tramp.el +++ b/lisp/eshell/em-tramp.el @@ -57,13 +57,12 @@ eshell-tramp-initialize (autoload 'eshell-parse-command "esh-cmd") -(defun eshell/su (&rest arguments) +(defun eshell/su (&rest args) "Alias \"su\" to call TRAMP. Uses the system su through TRAMP's su method." - (setq arguments (eshell-stringify-list (flatten-tree arguments))) (eshell-eval-using-options - "su" arguments + "su" args '((?h "help" nil nil "show this usage screen") (?l "login" nil login "provide a login environment") (? nil nil login "provide a login environment") @@ -77,10 +76,6 @@ eshell/su (prefix (file-remote-p default-directory))) (dolist (arg args) (if (string-equal arg "-") (setq login t) (setq user arg))) - ;; `eshell-eval-using-options' tries to handle "-" as a - ;; short option; double-check whether the original - ;; arguments include it. - (when (member "-" arguments) (setq login t)) (when login (setq dir "~/")) (if (and prefix (or diff --git a/lisp/eshell/esh-opt.el b/lisp/eshell/esh-opt.el index 8c29fff809..0961e214f4 100644 --- a/lisp/eshell/esh-opt.el +++ b/lisp/eshell/esh-opt.el @@ -283,6 +283,9 @@ eshell--process-args (memq :parse-leading-options-only options)))) (setq arg (nth ai eshell--args)) (if (not (and (stringp arg) + ;; A string of length 1 can't be an option; (if + ;; it's "-", that generally means stdin). + (> (length arg) 1) (string-match "^-\\(-\\)?\\(.*\\)" arg))) ;; Positional argument found, skip (setq ai (1+ ai) @@ -295,9 +298,9 @@ eshell--process-args (if (> (length switch) 0) (eshell--process-option name switch 1 ai options opt-vals) (setq ai (length eshell--args))) - (while (> (length switch) 0) - (setq switch (eshell--process-option name switch 0 - ai options opt-vals))))))) + (while (> (length switch) 0) + (setq switch (eshell--process-option name switch 0 + ai options opt-vals))))))) (nconc (mapcar #'cdr opt-vals) eshell--args))) (provide 'esh-opt) diff --git a/test/lisp/eshell/esh-opt-tests.el b/test/lisp/eshell/esh-opt-tests.el index 4331c02ff5..5b30de414a 100644 --- a/test/lisp/eshell/esh-opt-tests.el +++ b/test/lisp/eshell/esh-opt-tests.el @@ -182,6 +182,27 @@ esh-opt-test/eval-using-options-short-single-token (should (equal ignore-pattern "*.txt")) (should (equal args '("/some/path"))))) +(ert-deftest esh-opt-test/eval-using-options-stdin () + "Test that \"-\" is a positional arg in `eshell-eval-using-options'." + (eshell-eval-using-options + "cat" '("-") + '((?A "show-all" nil show-all + "show all characters")) + (should (eq show-all nil)) + (should (equal args '("-")))) + (eshell-eval-using-options + "cat" '("-A" "-") + '((?A "show-all" nil show-all + "show all characters")) + (should (eq show-all t)) + (should (equal args '("-")))) + (eshell-eval-using-options + "cat" '("-" "-A") + '((?A "show-all" nil show-all + "show all characters")) + (should (eq show-all t)) + (should (equal args '("-"))))) + (ert-deftest esh-opt-test/eval-using-options-terminate-options () "Test that \"--\" terminates options in `eshell-eval-using-options'." (eshell-eval-using-options -- 2.25.1