is-default-value.js 765 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. // This example shows how to understand if a default value is used or not.
  3. // 1. const { parseArgs } = require('node:util'); // from node
  4. // 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package
  5. const { parseArgs } = require('..'); // in repo
  6. const options = {
  7. file: { short: 'f', type: 'string', default: 'FOO' },
  8. };
  9. const { values, tokens } = parseArgs({ options, tokens: true });
  10. const isFileDefault = !tokens.some((token) => token.kind === 'option' &&
  11. token.name === 'file'
  12. );
  13. console.log(values);
  14. console.log(`Is the file option [${values.file}] the default value? ${isFileDefault}`);
  15. // Try the following:
  16. // node is-default-value.js
  17. // node is-default-value.js -f FILE
  18. // node is-default-value.js --file FILE