You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
986 B

4 years ago
  1. # has-flag [![Build Status](https://travis-ci.org/sindresorhus/has-flag.svg?branch=master)](https://travis-ci.org/sindresorhus/has-flag)
  2. > Check if [`argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv) has a specific flag
  3. Correctly stops looking after an `--` argument terminator.
  4. ## Install
  5. ```
  6. $ npm install has-flag
  7. ```
  8. ## Usage
  9. ```js
  10. // foo.js
  11. const hasFlag = require('has-flag');
  12. hasFlag('unicorn');
  13. //=> true
  14. hasFlag('--unicorn');
  15. //=> true
  16. hasFlag('f');
  17. //=> true
  18. hasFlag('-f');
  19. //=> true
  20. hasFlag('foo=bar');
  21. //=> true
  22. hasFlag('foo');
  23. //=> false
  24. hasFlag('rainbow');
  25. //=> false
  26. ```
  27. ```
  28. $ node foo.js -f --unicorn --foo=bar -- --rainbow
  29. ```
  30. ## API
  31. ### hasFlag(flag, [argv])
  32. Returns a boolean for whether the flag exists.
  33. #### flag
  34. Type: `string`
  35. CLI flag to look for. The `--` prefix is optional.
  36. #### argv
  37. Type: `string[]`<br>
  38. Default: `process.argv`
  39. CLI arguments.
  40. ## License
  41. MIT © [Sindre Sorhus](https://sindresorhus.com)