feat(cli): add stdout implicit arg type

CLIs can now use this for implicit validation for options that take a
file path or - to signal "print to stdout", like so:

  (defcli! (doom command) ((outfile ("--out" (file stdout))))
    (if (equal outfile "-")
        (print! "output")
      (with-temp-file outfile
        (insert "output"))))

If OUTFILE is not an existing file path or a -, you'll see an this
helpful error:

  Error: -o/--file received invalid value "FOO"

  Validation errors:
  - Must be a dash to signal stdout.
  - File does not exist.

  See 'doom h[elp] make codeowners' or 'doom make codeowners {-?,--help}' for documentation.
This commit is contained in:
Henrik Lissner 2022-08-07 17:22:56 +02:00
parent 26bc4ea150
commit d53e08767d
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -62,6 +62,10 @@ See argument types in `doom-cli-argument-types', and `defcli!' for usage.")
:read doom-path :read doom-path
:error "File does not exist" :error "File does not exist"
:zshcomp "_files") :zshcomp "_files")
(stdout :test ,(lambda (str) (equal str "-"))
:read identity
:error "Must be a dash to signal stdout"
:zshcomp "(-)")
(path :read expand-file-name :zshcomp "_files") (path :read expand-file-name :zshcomp "_files")
(form :read read) (form :read read)
(regexp :test ,(lambda (str) (always (string-match-p str "")))) (regexp :test ,(lambda (str) (always (string-match-p str ""))))