-
Notifications
You must be signed in to change notification settings - Fork 10
Closed
Labels
bring-your-own-codeExperiments with how authors might build on parseArgsExperiments with how authors might build on parseArgs
Description
(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' }
}
lirantal
Metadata
Metadata
Assignees
Labels
bring-your-own-codeExperiments with how authors might build on parseArgsExperiments with how authors might build on parseArgs