Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shadowspawn opened this issue Mar 18, 2022 · 11 comments
Closed

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

shadowspawn opened this issue Mar 18, 2022 · 11 comments
Labels
bring-your-own-code Experiments with how authors might build on parseArgs

Comments

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 18, 2022

(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' }
}
@ljharb

This comment was marked as off-topic.

@shadowspawn

This comment was marked as off-topic.

@ljharb

This comment was marked as off-topic.

@shadowspawn

This comment was marked as off-topic.

@shadowspawn shadowspawn added the bring-your-own-code Experiments with how authors might build on parseArgs label Mar 27, 2022
@iansu

This comment was marked as resolved.

@bakkot

This comment was marked as resolved.

@shadowspawn

This comment was marked as resolved.

@iansu
Copy link
Collaborator

iansu commented May 29, 2022

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.

@lirantal
Copy link

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.

@shadowspawn
Copy link
Collaborator Author

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 });

@shadowspawn
Copy link
Collaborator Author

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
Labels
bring-your-own-code Experiments with how authors might build on parseArgs
Projects
None yet
Development

No branches or pull requests

5 participants