aboutsummaryrefslogtreecommitdiffhomepage
path: root/builtin.cpp
diff options
context:
space:
mode:
authorGravatar maxfl <gmaxfl@gmail.com>2012-07-08 18:53:12 +0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-07-10 16:21:00 -0700
commit0c03b6ddc7555deddeb208d8b15300edffd9ef0f (patch)
tree5cd14cfafdd3d339521400e60da70f014aced451 /builtin.cpp
parent176a3913aa0dc2b0a03c16ffea9a22069266f4ab (diff)
Allow 'contains' builtin to return index
Add an option -i/--index to the contains builtin to print the index of first coincidence.
Diffstat (limited to 'builtin.cpp')
-rw-r--r--builtin.cpp11
1 files changed, 10 insertions, 1 deletions
diff --git a/builtin.cpp b/builtin.cpp
index 2f644fbe..36b8ec7a 100644
--- a/builtin.cpp
+++ b/builtin.cpp
@@ -2717,6 +2717,7 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
argc = builtin_count_args( argv );
int i;
wchar_t *needle;
+ int index=0;
woptind=0;
@@ -2728,6 +2729,10 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
}
,
{
+ L"index", no_argument, 0, 'i'
+ }
+ ,
+ {
0, 0, 0, 0
}
}
@@ -2739,7 +2744,7 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
int opt = wgetopt_long( argc,
argv,
- L"+h",
+ L"+hi",
long_options,
&opt_index );
if( opt == -1 )
@@ -2772,6 +2777,9 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
builtin_unknown_option( parser, argv[0], argv[woptind-1] );
return STATUS_BUILTIN_ERROR;
+ case 'i':
+ index=1;
+ break;
}
}
@@ -2790,6 +2798,7 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
if( !wcscmp( needle, argv[i]) )
{
+ if ( index ) append_format(stdout_buffer, L"%d\n", i-woptind );
return 0;
}
}