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.
This commit is contained in:
Jim Porter 2022-01-24 21:08:50 -08:00 committed by Lars Ingebrigtsen
parent dea24a0f7d
commit 44ffd6a825
3 changed files with 29 additions and 10 deletions

View file

@ -57,13 +57,12 @@
(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 @@ Become another USER during a login session.")
(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

View file

@ -283,6 +283,9 @@ triggered to say that the switch is unrecognized."
(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 @@ triggered to say that the switch is unrecognized."
(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)

View file

@ -182,6 +182,27 @@
(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