Skip to content

How might I add support for defaults? (And env?) #82

Closed

Description

@shadowspawn

(This is a bring-your-own-feature experiment, not a prototype of parseArgs implementation.)

Default values is our current canonical example of something that might get added in the future. A complication for some uses of defaults is telling whether the option value came from the user or the defaults. Another feature is support for options coming from environment variables.

Can we add all three of those in user land now?

const { parseArgs } = require('@pkgjs/parseargs');

const options = {
  str: { type: 'string', short: 's' },
  env: { type: 'string', env: 'ENV', short: 'e' },
  def: { type: 'string', default: 'DEFAULT',short: 'd' }
};

const result = parseArgs({ options });

result.valueOrigins = {};

Object.keys(result.values).forEach(optionName => {
  result.valueOrigins[optionName] = 'cli';
});
Object.entries(options)
  .filter(([optionName, detail]) => result.values[optionName] === undefined)
  .forEach(([optionName, detail]) => {
    if (detail.env  && process.env[detail.env] !== undefined) {
        result.values[optionName] = process.env[detail.env];
        result.valueOrigins[optionName] = 'env';
    } else if (detail.default !== undefined) {
        result.values[optionName] = detail.default;
        result.valueOrigins[optionName] = 'default';
    }
  }); 

console.log(result);
% ENV=EEE node index.js --str SSS  ppp 
{
  values: { str: 'SSS', env: 'EEE', def: 'DEFAULT' },
  positionals: [ 'ppp' ],
  valueOrigins: { str: 'cli', env: 'env', def: 'default' }
}

Activity

  1. ljharb commented on Mar 18, 2022

    @ljharb
  2. shadowspawn commented on Mar 18, 2022

    @shadowspawn
    Author
  3. ljharb commented on Mar 18, 2022

    @ljharb
  4. shadowspawn commented on Mar 18, 2022

    @shadowspawn
    Author
  5. iansu commented on May 28, 2022

    @iansu
  6. bakkot commented on May 28, 2022

    @bakkot
  7. shadowspawn commented on May 29, 2022

    @shadowspawn
    Author
  8. iansu commented on May 29, 2022

    @iansu
    Collaborator

    I guess I was missing something 🤦

    I did get this to work. Thanks for the help. It would still be nice to add support for defaults as well as required options in the future.

  9. lirantal commented on Aug 12, 2022

    @lirantal

    I'm not particularly fond of coupling environment variables into parsed arguments but I do like the idea of default values, as well as indicating whether the default value has been assigned or not.

  10. shadowspawn commented on Sep 4, 2022

    @shadowspawn
    CollaboratorAuthor

    Rolling your own support for just defaults is a one-liner. Per #142 (comment)

    let options = { foo: { type: 'string', default: 'bar' }, ...etc };
    let { values } = parseArgs({ options });
    
    Object.entries(options).forEach(([k, v]) => { values[k] ??= options[k].default });
    
  11. shadowspawn commented on Oct 15, 2022

    @shadowspawn
    CollaboratorAuthor

    Node.js v18.11.0 includes support for defaults. Closing this stale example of bring-your-own-code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bring-your-own-codeExperiments with how authors might build on parseArgs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions