Allow 'contains' builtin to return index

Add an option -i/--index to the contains builtin to print the index
of first coincidence.
This commit is contained in:
maxfl 2012-07-08 18:53:12 +08:00 committed by ridiculousfish
parent 176a3913aa
commit 0c03b6ddc7
2 changed files with 11 additions and 1 deletions

View file

@ -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;
@ -2727,6 +2728,10 @@ static int builtin_contains( parser_t &parser, wchar_t ** argv )
L"help", no_argument, 0, 'h'
}
,
{
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;
}
}

View file

@ -5,6 +5,7 @@
\subsection contains-description Description
- \c -i or \c --index print the the word index
- \c -h or \c --help display this message
Test if the set VALUES contains the string KEY. Return status is 0 if