option.js 977 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. 'use strict';
  10. // The actual option data.
  11. var data = {};
  12. // Get or set an option value.
  13. var option = module.exports = function(key, value) {
  14. var no = key.match(/^no-(.+)$/);
  15. if (arguments.length === 2) {
  16. return (data[key] = value);
  17. } else if (no) {
  18. return data[no[1]] === false;
  19. } else {
  20. return data[key];
  21. }
  22. };
  23. // Initialize option data.
  24. option.init = function(obj) {
  25. return (data = obj || {});
  26. };
  27. // List of options as flags.
  28. option.flags = function() {
  29. return Object.keys(data).filter(function(key) {
  30. // Don't display empty arrays.
  31. return !(Array.isArray(data[key]) && data[key].length === 0);
  32. }).map(function(key) {
  33. var val = data[key];
  34. return '--' + (val === false ? 'no-' : '') + key +
  35. (typeof val === 'boolean' ? '' : '=' + val);
  36. });
  37. };