another __fish_sgrep replacement

This commit is contained in:
Kurtis Rader 2017-04-13 23:13:55 -07:00
parent c3584111d6
commit 0be8d0d385
10 changed files with 124 additions and 25 deletions

View File

View File

@ -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

View File

@ -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

View File

@ -33,8 +33,8 @@
#include <map>
#include <memory>
#include <random>
#include <string>
#include <set>
#include <string>
#include <utility>
#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);

View File

@ -0,0 +1 @@
__fish_contains_opt: Unknown option -x

View File

@ -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

View File

View File

@ -0,0 +1 @@
__fish_not_contains_opt: Unknown option -x

View File

@ -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

View File