diff --git a/__fish_not_contain_opt.err b/__fish_not_contain_opt.err new file mode 100644 index 000000000..e69de29bb diff --git a/share/functions/__fish_contains_opt.fish b/share/functions/__fish_contains_opt.fish index 8e35aa365..ffd139ad6 100644 --- a/share/functions/__fish_contains_opt.fish +++ b/share/functions/__fish_contains_opt.fish @@ -1,12 +1,10 @@ - function __fish_contains_opt -d "Checks if a specific option has been given in the current commandline" set -l next_short - set -l short_opt set -l long_opt for i in $argv - if test $next_short + if test -n "$next_short" set next_short set short_opt $short_opt $i else @@ -14,32 +12,30 @@ function __fish_contains_opt -d "Checks if a specific option has been given in t case -s set next_short 1 case '-*' - echo __fish_contains_opt: Unknown option $i + echo __fish_contains_opt: Unknown option $i >&2 return 1 - - case '**' + case '*' set long_opt $long_opt $i end end end for i in $short_opt - - if test -z $i + if test -z "$i" continue end - if commandline -cpo | __fish_sgrep -- "^-"$i"\|^-[^-]*"$i >/dev/null + if string match -qr -- "^-$i|^-[^-]*$i" (commandline -cpo) return 0 end - if commandline -ct | __fish_sgrep -- "^-"$i"\|^-[^-]*"$i >/dev/null + if string match -qr -- "^-$i|^-[^-]*$i" (commandline -ct) return 0 end end for i in $long_opt - if test -z $i + if test -z "$i" continue end @@ -50,4 +46,3 @@ function __fish_contains_opt -d "Checks if a specific option has been given in t return 1 end - diff --git a/share/functions/__fish_not_contain_opt.fish b/share/functions/__fish_not_contain_opt.fish index 933e5eec7..f05f1749d 100644 --- a/share/functions/__fish_not_contain_opt.fish +++ b/share/functions/__fish_not_contain_opt.fish @@ -1,11 +1,10 @@ function __fish_not_contain_opt -d "Checks that a specific option is not in the current command line" set -l next_short - set -l short_opt set -l long_opt for i in $argv - if test $next_short + if test -n "$next_short" set next_short set short_opt $short_opt $i else @@ -13,32 +12,31 @@ function __fish_not_contain_opt -d "Checks that a specific option is not in the case -s set next_short 1 case '-*' - echo __fish_contains_opt: Unknown option $i + echo __fish_not_contains_opt: Unknown option $i >&2 return 1 - case '**' + case '*' set long_opt $long_opt $i end end end for i in $short_opt - - if test -z $i + if test -z "$i" continue end - if commandline -cpo | __fish_sgrep -- "^-"$i"\|^-[^-]*"$i >/dev/null + if string match -qr -- "^-$i|^-[^-]*$i" (commandline -cpo) return 1 end - if commandline -ct | __fish_sgrep -- "^-"$i"\|^-[^-]*"$i >/dev/null + if string match -qr -- "^-$i|^-[^-]*$i" (commandline -ct) return 1 end end for i in $long_opt - if test -z $i + if test -z "$i" continue end diff --git a/src/builtin.cpp b/src/builtin.cpp index 0e8c2ae64..a0a5b3d5a 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -33,8 +33,8 @@ #include #include #include -#include #include +#include #include #include "builtin.h" @@ -457,7 +457,7 @@ static void builtin_bind_list_modes(io_streams_t &streams) { for (const input_mapping_name_t &binding : lst) { modes.insert(binding.mode); } - for (const auto& mode : modes) { + for (const auto &mode : modes) { streams.out.append_format(L"%ls\n", mode.c_str()); } } @@ -3045,7 +3045,7 @@ static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) { int pid = fish_wcstoi(argv[i]); if (errno || pid < 0) { streams.err.append_format(_(L"%ls: '%ls' is not a valid job specifier\n"), L"bg", - argv[i]); + argv[i]); res = STATUS_BUILTIN_ERROR; } pids.push_back(pid); @@ -3056,7 +3056,7 @@ static int builtin_bg(parser_t &parser, io_streams_t &streams, wchar_t **argv) { // Background all existing jobs that match the pids. // Non-existent jobs aren't an error, but information about them is useful. for (auto p : pids) { - if (job_t* j = job_get_from_pid(p)) { + if (job_t *j = job_get_from_pid(p)) { res |= send_to_bg(parser, streams, j); } else { streams.err.append_format(_(L"%ls: Could not find job '%d'\n"), argv[0], p); diff --git a/tests/__fish_contains_opt.err b/tests/__fish_contains_opt.err new file mode 100644 index 000000000..2c92086df --- /dev/null +++ b/tests/__fish_contains_opt.err @@ -0,0 +1 @@ +__fish_contains_opt: Unknown option -x diff --git a/tests/__fish_contains_opt.in b/tests/__fish_contains_opt.in new file mode 100644 index 000000000..38598ecee --- /dev/null +++ b/tests/__fish_contains_opt.in @@ -0,0 +1,52 @@ +function commandline + if test $argv[1] = '-ct' + echo --long4\n-4 + else if test $argv[1] = '-cpo' + echo cmd\n-z\n-bc\n--long1\narg1\n-d\narg2\n--long2 + end +end + +__fish_contains_opt -s z +or echo fails to find -z + +__fish_contains_opt -s c +or echo fails to find -c + +__fish_contains_opt -s x +and echo should not have found -x + +__fish_contains_opt -s x -s z +or echo fails to find -z + +__fish_contains_opt -s x -s c +or echo fails to find -c + +__fish_contains_opt -s x long1 +or echo fails to find --long1 + +__fish_contains_opt long2 +or echo fails to find --long2 + +__fish_contains_opt long1 long2 +or echo fails to find --long1 or --long2 + +__fish_contains_opt long3 +and echo should not have found --long3 + +__fish_contains_opt -s 4 long4 +or echo fails to find -4 + +__fish_contains_opt long4 +and echo should not have found --long4 + +__fish_contains_opt arg1 +and echo should not have found --arg1 + +__fish_contains_opt -s a +and echo should not have found -a + +# This should result in message written to stderr and an error status. +__fish_contains_opt -x w +and '"__fish_contains_opt -x w" should not have succeeded' + +true diff --git a/tests/__fish_contains_opt.out b/tests/__fish_contains_opt.out new file mode 100644 index 000000000..e69de29bb diff --git a/tests/__fish_not_contain_opt.err b/tests/__fish_not_contain_opt.err new file mode 100644 index 000000000..0a8a8a204 --- /dev/null +++ b/tests/__fish_not_contain_opt.err @@ -0,0 +1 @@ +__fish_not_contains_opt: Unknown option -x diff --git a/tests/__fish_not_contain_opt.in b/tests/__fish_not_contain_opt.in new file mode 100644 index 000000000..fd7ce4b5b --- /dev/null +++ b/tests/__fish_not_contain_opt.in @@ -0,0 +1,52 @@ +function commandline + if test $argv[1] = '-ct' + echo --long4\n-4 + else if test $argv[1] = '-cpo' + echo cmd\n-z\n-bc\n--long1\narg1\n-d\narg2\n--long2 + end +end + +__fish_not_contain_opt -s z +and echo should not have found -z + +__fish_not_contain_opt -s c +and echo should not have found -c + +__fish_not_contain_opt -s x +or echo unexpectedly found -x + +__fish_not_contain_opt -s x -s z +and echo should not have found -x/-z + +__fish_not_contain_opt -s x -s c +and echo should not have found -x/-c + +__fish_not_contain_opt -s x long1 +and echo should not have found --long1 + +__fish_not_contain_opt long2 +and echo found --long2 + +__fish_not_contain_opt long1 long2 +and echo found --long1 or --long2 + +__fish_not_contain_opt long3 +or echo unexpectedly found --long3 + +__fish_not_contain_opt -s 4 long4 +and echo unexpectedly found -4 + +__fish_not_contain_opt long4 +or echo should not have found --long4 + +__fish_not_contain_opt arg1 +or echo should not have found --arg1 + +__fish_not_contain_opt -s a +or echo should not have found -a + +# This should result in message written to stderr and an error status. +__fish_not_contain_opt -x w +and '"__fish_not_contain_opt -x w" should not have succeeded' + +true diff --git a/tests/__fish_not_contain_opt.out b/tests/__fish_not_contain_opt.out new file mode 100644 index 000000000..e69de29bb