mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
Add -q/--query switch to the functions builtin. Works just like the same switch to set does. Useful for checking for the existance of functions in a script
darcs-hash:20060712174655-ac50b-14a98a15751bb5ddddf4ffb41521b616bef5e40c.gz
This commit is contained in:
parent
036cc4e0fa
commit
4a87c9895d
3 changed files with 40 additions and 20 deletions
41
builtin.c
41
builtin.c
|
@ -774,6 +774,7 @@ static int builtin_functions( wchar_t **argv )
|
|||
int list=0;
|
||||
int show_hidden=0;
|
||||
int res = 0;
|
||||
int query = 0;
|
||||
|
||||
woptind=0;
|
||||
|
||||
|
@ -800,6 +801,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
L"help", no_argument, 0, 'h'
|
||||
}
|
||||
,
|
||||
{
|
||||
L"query", no_argument, 0, 'q'
|
||||
}
|
||||
,
|
||||
{
|
||||
0, 0, 0, 0
|
||||
}
|
||||
|
@ -812,7 +817,7 @@ static int builtin_functions( wchar_t **argv )
|
|||
|
||||
int opt = wgetopt_long( argc,
|
||||
argv,
|
||||
L"ed:nah",
|
||||
L"ed:nahq",
|
||||
long_options,
|
||||
&opt_index );
|
||||
if( opt == -1 )
|
||||
|
@ -852,6 +857,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
builtin_print_help( argv[0], sb_out );
|
||||
return 0;
|
||||
|
||||
case 'q':
|
||||
query = 1;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
builtin_print_help( argv[0], sb_err );
|
||||
|
||||
|
@ -862,9 +871,9 @@ static int builtin_functions( wchar_t **argv )
|
|||
}
|
||||
|
||||
/*
|
||||
Erase, desc and list are mutually exclusive
|
||||
Erase, desc, query and list are mutually exclusive
|
||||
*/
|
||||
if( (erase + (desc!=0) + list) > 1 )
|
||||
if( (erase + (desc!=0) + list + query) > 1 )
|
||||
{
|
||||
sb_printf( sb_err,
|
||||
_( L"%ls: Invalid combination of options\n" ),
|
||||
|
@ -951,22 +960,25 @@ static int builtin_functions( wchar_t **argv )
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
switch( argc - woptind )
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
sb_append( sb_out, _( L"Current function definitions are:\n\n" ) );
|
||||
al_init( &names );
|
||||
function_get_names( &names, show_hidden );
|
||||
sort_list( &names );
|
||||
|
||||
for( i=0; i<al_get_count( &names ); i++ )
|
||||
if( !query )
|
||||
{
|
||||
functions_def( (wchar_t *)al_get( &names, i ) );
|
||||
sb_append( sb_out, _( L"Current function definitions are:\n\n" ) );
|
||||
al_init( &names );
|
||||
function_get_names( &names, show_hidden );
|
||||
sort_list( &names );
|
||||
|
||||
for( i=0; i<al_get_count( &names ); i++ )
|
||||
{
|
||||
functions_def( (wchar_t *)al_get( &names, i ) );
|
||||
}
|
||||
|
||||
al_destroy( &names );
|
||||
}
|
||||
|
||||
al_destroy( &names );
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -978,7 +990,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
res++;
|
||||
else
|
||||
{
|
||||
functions_def( argv[i] );
|
||||
if( !query )
|
||||
{
|
||||
functions_def( argv[i] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,11 +12,16 @@ This builtin command is used to print or erase functions.
|
|||
- <code>-e</code> or <code>--erase</code> causes the specified functions to be erased.
|
||||
- <code>-h</code> or <code>--help</code> display a help message and exit
|
||||
- <code>-n</code> or <code>--names</code> list only the names of all defined functions
|
||||
- <code>-q</code> or <code>--query</code> test if the specified functions exist. Does not output anything, but the builtins exit status is the number of functions specified that were not defined.
|
||||
|
||||
If \c functions is called with no arguments, the names and definition
|
||||
of all functions are printed, otherwise, the specified function
|
||||
definitions will be printed.
|
||||
The default behavior of \c functions when called with no arguments,
|
||||
is to print the names and definitions of all defined functions. If any
|
||||
non-switch parameters are given, only the definition of the specified
|
||||
functions are printed.
|
||||
|
||||
If a function is automatically loaded, using <code>functions -e</code>
|
||||
to erase it will not remove the function.
|
||||
Automatically loaded functions can not be removed using functions
|
||||
-e. Either remove the definition file or change the
|
||||
$fish_function_path variable to remove autoloaded functions.
|
||||
|
||||
The exit status of the functions builtin is the number functions
|
||||
specified in the argument list that do not exist.
|
||||
|
|
|
@ -3,4 +3,4 @@ complete -c functions -xa "(functions -na)" -d (N_ "Function")
|
|||
complete -c functions -s a -l all -d (N_ "Show hidden functions")
|
||||
complete -c functions -s h -l help -d (N_ "Display help and exit")
|
||||
complete -c functions -s d -l description -d (N_ "Set function description") -x
|
||||
|
||||
complete -c functions -s q -l query -d (N_ "Test if function is defined")
|
||||
|
|
Loading…
Reference in a new issue