From efb3ae6d490df43ff23eb7647aa516e17b9db8ce Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 20 Sep 2021 20:56:45 +0200 Subject: [PATCH] Add `path is` shorthand for `path filter -q` This replaces `test -e` and such. --- doc_src/cmds/path.rst | 22 ++++++++++++++++++++++ src/builtin_path.cpp | 13 ++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/doc_src/cmds/path.rst b/doc_src/cmds/path.rst index b1f87a881..af877d195 100644 --- a/doc_src/cmds/path.rst +++ b/doc_src/cmds/path.rst @@ -12,6 +12,7 @@ Synopsis path dir [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] path extension [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] path filter [(-z | --null-in)] [(-Z | --null-out)] [(-v | --invert)] [(-q | --quiet)] [(-t | --type) TYPE] [(-p | --perm) PERMISSION] [PATH...] + path is [(-z | --null-in)] [(-Z | --null-out)] [(-v | --invert)] [(-q | --quiet)] [(-t | --type) TYPE] [(-p | --perm) PERMISSION] [PATH...] path normalize [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] path real [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] path strip-extension [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] @@ -179,6 +180,27 @@ Examples # "-x" is short for "--perm=exec" and "-w" short for "--perm=write"! /home/me +"is" subcommand +-------------------- + +:: + + path is [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [(-t | --type) TYPE] [(-p | --perm) PERMISSION] [PATH...] + +``path is`` is short for ``path filter -q``. It returns true if any of the given files passes the filter. + +Examples +^^^^^^^^ + +:: + + >_ path is /usr/bin /usr/argagagji + # /usr/bin exists, so this returns a status of 0 (true). + >_ path is /usr/argagagji + # /usr/argagagji does not, so this returns a status of 1 (false). + >_ path is -fx /bin/sh + # /bin/sh is usually an executable file, so this returns true. + "normalize" subcommand ----------------------- diff --git a/src/builtin_path.cpp b/src/builtin_path.cpp index 5080d0970..6a81cfa75 100644 --- a/src/builtin_path.cpp +++ b/src/builtin_path.cpp @@ -644,7 +644,7 @@ static int path_real(parser_t &parser, io_streams_t &streams, int argc, const wc // All strings are taken to be filenames, and if they match the type/perms/etc (and exist!) // they are passed along. -static int path_filter(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { +static int path_filter(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv, bool is_is) { options_t opts; opts.type_valid = true; opts.perm_valid = true; @@ -652,6 +652,8 @@ static int path_filter(parser_t &parser, io_streams_t &streams, int argc, const int optind; int retval = parse_opts(&opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; + // If we have been invoked as "path is", which is "path filter -q". + if (is_is) opts.quiet = true; int n_transformed = 0; arg_iterator_t aiter(argv, optind, streams, opts.null_in); @@ -673,6 +675,14 @@ static int path_filter(parser_t &parser, io_streams_t &streams, int argc, const return n_transformed > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; } +static int path_filter(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { + return path_filter(parser, streams, argc, argv, false /* is_is */); +} + +static int path_is(parser_t &parser, io_streams_t &streams, int argc, const wchar_t **argv) { + return path_filter(parser, streams, argc, argv, true /* is_is */); +} + // Keep sorted alphabetically static constexpr const struct path_subcommand { const wchar_t *name; @@ -685,6 +695,7 @@ static constexpr const struct path_subcommand { {L"dir", &path_dir}, {L"extension", &path_extension}, {L"filter", &path_filter}, + {L"is", &path_is}, {L"normalize", &path_normalize}, {L"real", &path_real}, {L"strip-extension", &path_strip_extension},