1 | # has-flag
|
2 |
|
3 | > Check if [`argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv) has a specific flag
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```
|
8 | $ npm install has-flag
|
9 | ```
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```js
|
14 | // foo.js
|
15 | import hasFlag from 'has-flag';
|
16 |
|
17 | hasFlag('unicorn');
|
18 | //=> true
|
19 |
|
20 | hasFlag('--unicorn');
|
21 | //=> true
|
22 |
|
23 | hasFlag('f');
|
24 | //=> true
|
25 |
|
26 | hasFlag('-f');
|
27 | //=> true
|
28 |
|
29 | hasFlag('foo=bar');
|
30 | //=> true
|
31 |
|
32 | hasFlag('foo');
|
33 | //=> false
|
34 |
|
35 | hasFlag('rainbow');
|
36 | //=> false
|
37 | ```
|
38 |
|
39 | ```
|
40 | $ node foo.js -f --unicorn --foo=bar -- --rainbow
|
41 | ```
|
42 |
|
43 | ## API
|
44 |
|
45 | ### hasFlag(flag, argv?)
|
46 |
|
47 | Returns a boolean for whether the flag exists.
|
48 |
|
49 | It correctly stops looking after an `--` argument terminator.
|
50 |
|
51 | #### flag
|
52 |
|
53 | Type: `string`
|
54 |
|
55 | CLI flag to look for. The `--` prefix is optional.
|
56 |
|
57 | #### argv
|
58 |
|
59 | Type: `string[]`\
|
60 | Default: `process.argv`
|
61 |
|
62 | CLI arguments.
|
63 |
|
64 | ---
|
65 |
|
66 | <div align="center">
|
67 | <b>
|
68 | <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>
|
69 | </b>
|
70 | <br>
|
71 | <sub>
|
72 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
73 | </sub>
|
74 | </div>
|