| 1 |
# has-flag [](https://travis-ci.org/sindresorhus/has-flag) |
| 2 |
|
| 3 |
> Check if [`argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv) has a specific flag |
| 4 |
|
| 5 |
Correctly stops looking after an `--` argument terminator. |
| 6 |
|
| 7 |
--- |
| 8 |
|
| 9 |
<div align="center"> |
| 10 |
<b> |
| 11 |
<a href="https://tidelift.com/subscription/pkg/npm-has-flag?utm_source=npm-has-flag&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> |
| 12 |
</b> |
| 13 |
<br> |
| 14 |
<sub> |
| 15 |
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. |
| 16 |
</sub> |
| 17 |
</div> |
| 18 |
|
| 19 |
--- |
| 20 |
|
| 21 |
|
| 22 |
## Install |
| 23 |
|
| 24 |
``` |
| 25 |
$ npm install has-flag |
| 26 |
``` |
| 27 |
|
| 28 |
|
| 29 |
## Usage |
| 30 |
|
| 31 |
```js |
| 32 |
// foo.js |
| 33 |
const hasFlag = require('has-flag'); |
| 34 |
|
| 35 |
hasFlag('unicorn'); |
| 36 |
//=> true |
| 37 |
|
| 38 |
hasFlag('--unicorn'); |
| 39 |
//=> true |
| 40 |
|
| 41 |
hasFlag('f'); |
| 42 |
//=> true |
| 43 |
|
| 44 |
hasFlag('-f'); |
| 45 |
//=> true |
| 46 |
|
| 47 |
hasFlag('foo=bar'); |
| 48 |
//=> true |
| 49 |
|
| 50 |
hasFlag('foo'); |
| 51 |
//=> false |
| 52 |
|
| 53 |
hasFlag('rainbow'); |
| 54 |
//=> false |
| 55 |
``` |
| 56 |
|
| 57 |
``` |
| 58 |
$ node foo.js -f --unicorn --foo=bar -- --rainbow |
| 59 |
``` |
| 60 |
|
| 61 |
|
| 62 |
## API |
| 63 |
|
| 64 |
### hasFlag(flag, [argv]) |
| 65 |
|
| 66 |
Returns a boolean for whether the flag exists. |
| 67 |
|
| 68 |
#### flag |
| 69 |
|
| 70 |
Type: `string` |
| 71 |
|
| 72 |
CLI flag to look for. The `--` prefix is optional. |
| 73 |
|
| 74 |
#### argv |
| 75 |
|
| 76 |
Type: `string[]`<br> |
| 77 |
Default: `process.argv` |
| 78 |
|
| 79 |
CLI arguments. |
| 80 |
|
| 81 |
|
| 82 |
## Security |
| 83 |
|
| 84 |
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. |
| 85 |
|
| 86 |
|
| 87 |
## License |
| 88 |
|
| 89 |
MIT © [Sindre Sorhus](https://sindresorhus.com) |