2019-03-31 09:05:09 +00:00
|
|
|
.. _cmd-contains:
|
|
|
|
|
2018-12-17 01:39:33 +00:00
|
|
|
contains - test if a word is present in a list
|
2019-01-03 04:10:47 +00:00
|
|
|
==============================================
|
2018-12-17 01:39:33 +00:00
|
|
|
|
2018-12-18 01:58:24 +00:00
|
|
|
Synopsis
|
|
|
|
--------
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:23:02 +00:00
|
|
|
``contains`` [**options**] *KEY* [*VALUES* ...]
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2018-12-19 02:44:30 +00:00
|
|
|
Description
|
2019-01-03 04:10:47 +00:00
|
|
|
-----------
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
``contains`` tests whether the set *VALUES* contains the string *KEY*.
|
|
|
|
If so, ``contains`` exits with code 0; if not, it exits with code 1.
|
2018-12-16 21:08:41 +00:00
|
|
|
|
|
|
|
The following options are available:
|
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
- **-i** or **--index** print the word index
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
Note that ``contains`` interprets all arguments starting with a **-** as an option to ``contains``, until an **--** argument is reached.
|
|
|
|
|
|
|
|
See the examples below.
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2018-12-19 02:44:30 +00:00
|
|
|
Example
|
2019-01-03 04:10:47 +00:00
|
|
|
-------
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
If *animals* is a list of animals, the following will test if *animals* contains "cat":
|
2018-12-19 03:14:04 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
if contains cat $animals
|
|
|
|
echo Your animal list is evil!
|
|
|
|
end
|
|
|
|
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
This code will add some directories to :envvar:`PATH` if they aren't yet included:
|
2018-12-19 03:14:04 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
for i in ~/bin /usr/local/bin
|
|
|
|
if not contains $i $PATH
|
|
|
|
set PATH $PATH $i
|
|
|
|
end
|
2018-12-16 21:08:41 +00:00
|
|
|
end
|
2018-12-19 03:14:04 +00:00
|
|
|
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
While this will check if function ``hasargs`` is being ran with the **-q** option:
|
2018-12-19 03:14:04 +00:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
function hasargs
|
|
|
|
if contains -- -q $argv
|
|
|
|
echo '$argv contains a -q option'
|
|
|
|
end
|
2018-12-16 21:08:41 +00:00
|
|
|
end
|
2018-12-19 03:14:04 +00:00
|
|
|
|
2018-12-16 21:08:41 +00:00
|
|
|
|
2021-12-17 23:16:47 +00:00
|
|
|
The **--** here stops ``contains`` from treating **-q** to an option to itself.
|
|
|
|
Instead it treats it as a normal string to check.
|