-
Notifications
You must be signed in to change notification settings - Fork 10
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
Propose stop storing explicit undefined
in values for flags
#55
Comments
undefined
in values for flags`undefined
in values for flags
I think it's far better to have the explicit undefined, so that it works with reflection mechanisms ( |
The proposed/original "existence" pattern has two objects in the results. I have been calling them
Why is it better to have reflection claim that there is an entry in |
Right - I'm talking about the cases where I don't want all the options, i only want the flags, or, only the values. |
Exploring the parse results... // const parsed = parseArgs(['--foo=123', '--bar'], { withValue: ['foo'] });
const parsed = {
options: { foo: true, bar: true },
values: { foo: '123' },
positionals: []
};
console.log('Used with value 1:');
for (const option in parsed.values) {
console.log(`${option}: ${parsed.values[option]}`);
}
console.log('\nUsed with value 2:');
console.log(parsed.values);
console.log('\nUsed as flag 1:');
const flags = Object.keys(parsed.options).filter((option) => parsed.values[option] === undefined);
console.log(flags);
console.log('\nUsed as flag 2:');
for (const option in parsed.options) {
if (parsed.values[option] === undefined)
console.log(option);
}
|
I understand that if you use |
Ha, serves me right for quickly grabbing a pattern off the internet to do something "normal" with the results. 😊 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
I find it a cognitive load to deal with properties which may be implicitly or explicitly undefined. I have to carefully think about what question I am asking. When I iterate through the properties, the property values may be The defined |
That’s just an inherent part of the language. It’s not something API designers can fix - a property can be present or absent, and if present, |
When appropriate. I am proposing the change because I think the API is better without it. |
You've provided examples of how a present-undefined may not be needed. Can you provide any examples where having it causes problems? |
const { parseArgs } = require('@pkgjs/parseargs');
const withValue = ['warp-speed', 'destination'];
const result = parseArgs(undefined, { withValue });
// Custom behaviour, error for empty strings
for (let [key, value] of Object.entries(result.values)) {
if (value.length === 0) {
console.error(`option value can not be empty: --${key}`);
process.exit(2);
}
}
console.log('To infinity and beyond...')
|
Sure, but in the world we're talking about, -if (value.length === 0) {
+if (value?.length === 0) { or: -if (value.length === 0) {
+if (!value) { or a number of other alternatives. |
Sure, but bugs are problems. |
You said:
And I asked:
I am still not seeing the "far better"? |
Contrived bugs can happen in either case; I'm looking particularly at which use cases are made unacceptably difficult by one choice or the other. The language differentiates absence vs present+undefined, and many things care about this - object rest/spread/Object.assign; all of the reflection methods (keys/values/entries/getOwnPropertyDescriptors/ownKeys); the stage 1 pattern matching proposal; etc. |
@shadowspawn perhaps my opinion will change, but as I experiment with porting the yargs-parser DSL to |
I found it less painful when I realised I didn't need to use it, which made me question why have it. Have you used the storage of I'm considering a long comment with some research. I am still thinking it is wrong semantically, and may cause breaking changes for possible future additions (e.g. defaults). |
Oops, and a comment from @bcoe, thanks Benjamin too. Scrolled too quickly. 🙈 |
I have touched on this in a few different issues. Opening a separate issue to make this visible and separately identified as a suggestion (and discussion). Note: I used the proposed behaviour in all the examples in #38.
I do not think storing an explicit
undefined
invalues
when flags are parsed is adding any value (pun intended).Current behaviour:
Proposed behaviour:
The text was updated successfully, but these errors were encountered: