Add path is shorthand for path filter -q

This replaces `test -e` and such.
This commit is contained in:
Fabian Homborg 2021-09-20 20:56:45 +02:00
parent b23548b2a6
commit efb3ae6d49
2 changed files with 34 additions and 1 deletions

View File

@ -12,6 +12,7 @@ Synopsis
path dir [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...] path dir [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...]
path extension [(-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 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 normalize [(-z | --null-in)] [(-Z | --null-out)] [(-q | --quiet)] [PATH...]
path real [(-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...] 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"! # "-x" is short for "--perm=exec" and "-w" short for "--perm=write"!
/home/me /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 "normalize" subcommand
----------------------- -----------------------

View File

@ -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!) // All strings are taken to be filenames, and if they match the type/perms/etc (and exist!)
// they are passed along. // 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; options_t opts;
opts.type_valid = true; opts.type_valid = true;
opts.perm_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 optind;
int retval = parse_opts(&opts, &optind, argc, argv, parser, streams); int retval = parse_opts(&opts, &optind, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval; 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; int n_transformed = 0;
arg_iterator_t aiter(argv, optind, streams, opts.null_in); 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; 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 // Keep sorted alphabetically
static constexpr const struct path_subcommand { static constexpr const struct path_subcommand {
const wchar_t *name; const wchar_t *name;
@ -685,6 +695,7 @@ static constexpr const struct path_subcommand {
{L"dir", &path_dir}, {L"dir", &path_dir},
{L"extension", &path_extension}, {L"extension", &path_extension},
{L"filter", &path_filter}, {L"filter", &path_filter},
{L"is", &path_is},
{L"normalize", &path_normalize}, {L"normalize", &path_normalize},
{L"real", &path_real}, {L"real", &path_real},
{L"strip-extension", &path_strip_extension}, {L"strip-extension", &path_strip_extension},