-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add default
option parameter
#142
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4313c8a
feat: add defaultValue option parameter
Eomm 8a7a813
fix: removed default values from tokens
Eomm d94289a
chore: moved utility function
Eomm df855e0
feat: manage defaultValue array
Eomm d5759a0
docs: fix option type
Eomm 15d988a
fix: multiple option value instead of presence
Eomm 50f2b25
docs: add example
Eomm ea92d4e
fix: restore multiple boolean check when needed
Eomm f9e72da
fix: feedback
Eomm dd078a7
chore: rename defaultValue option to default
Eomm 7f68c46
fix: test description
Eomm b2f6a44
chore: update after merge into node.js core
Eomm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
// This example shows how to understand if a default value is used or not. | ||
|
||
// 1. const { parseArgs } = require('node:util'); // from node | ||
// 2. const { parseArgs } = require('@pkgjs/parseargs'); // from package | ||
const { parseArgs } = require('..'); // in repo | ||
|
||
const options = { | ||
file: { short: 'f', type: 'string', default: 'FOO' }, | ||
}; | ||
|
||
const { values, tokens } = parseArgs({ options, tokens: true }); | ||
|
||
const isFileDefault = !tokens.some((token) => token.kind === 'option' && | ||
token.name === 'file' | ||
); | ||
|
||
console.log(values); | ||
console.log(`Is the file option [${values.file}] the default value? ${isFileDefault}`); | ||
|
||
// Try the following: | ||
// node is-default-value.js | ||
// node is-default-value.js -f FILE | ||
// node is-default-value.js --file FILE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* global assert */ | ||
/* eslint max-len: 0 */ | ||
'use strict'; | ||
|
||
const { test } = require('./utils'); | ||
const { parseArgs } = require('../index.js'); | ||
|
||
test('default must be a boolean when option type is boolean', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'boolean', default: 'not a boolean' } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default must be Boolean/ | ||
); | ||
}); | ||
|
||
test('default must accept undefined value', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'boolean', default: undefined } }; | ||
const result = parseArgs({ args, options }); | ||
const expected = { | ||
values: { | ||
__proto__: null, | ||
}, | ||
positionals: [] | ||
}; | ||
assert.deepStrictEqual(result, expected); | ||
}); | ||
|
||
test('default must be a boolean array when option type is boolean and multiple', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default must be Array/ | ||
); | ||
}); | ||
|
||
test('default must be a boolean array when option type is string and multiple is true', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default\[2\] must be Boolean/ | ||
); | ||
}); | ||
|
||
test('default must be a string when option type is string', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'string', default: true } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default must be String/ | ||
); | ||
}); | ||
|
||
test('default must be an array when option type is string and multiple is true', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default must be Array/ | ||
); | ||
}); | ||
|
||
test('default must be a string array when option type is string and multiple is true', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, /options\.alpha\.default\[1\] must be String/ | ||
); | ||
}); | ||
|
||
test('default accepted input when multiple is true', () => { | ||
const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr']; | ||
const options = { | ||
inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, | ||
emptyStringArr: { type: 'string', multiple: true, default: [] }, | ||
fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] }, | ||
inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, | ||
emptyBoolArr: { type: 'boolean', multiple: true, default: [] }, | ||
fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] }, | ||
}; | ||
const expected = { values: { __proto__: null, | ||
inputStringArr: ['c', 'd'], | ||
inputBoolArr: [true, true], | ||
emptyStringArr: [], | ||
fullStringArr: ['a', 'b'], | ||
emptyBoolArr: [], | ||
fullBoolArr: [false, true, false] }, | ||
positionals: [] }; | ||
const result = parseArgs({ args, options }); | ||
assert.deepStrictEqual(result, expected); | ||
}); | ||
|
||
test('when default is set, the option must be added as result', () => { | ||
const args = []; | ||
const options = { | ||
a: { type: 'string', default: 'HELLO' }, | ||
b: { type: 'boolean', default: false }, | ||
c: { type: 'boolean', default: true } | ||
}; | ||
const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] }; | ||
|
||
const result = parseArgs({ args, options }); | ||
assert.deepStrictEqual(result, expected); | ||
}); | ||
|
||
test('when default is set, the args value takes precedence', () => { | ||
const args = ['--a', 'WORLD', '--b', '-c']; | ||
const options = { | ||
a: { type: 'string', default: 'HELLO' }, | ||
b: { type: 'boolean', default: false }, | ||
c: { type: 'boolean', default: true } | ||
}; | ||
const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] }; | ||
|
||
const result = parseArgs({ args, options }); | ||
assert.deepStrictEqual(result, expected); | ||
}); | ||
|
||
test('tokens should not include the default options', () => { | ||
const args = []; | ||
const options = { | ||
a: { type: 'string', default: 'HELLO' }, | ||
b: { type: 'boolean', default: false }, | ||
c: { type: 'boolean', default: true } | ||
}; | ||
|
||
const expectedTokens = []; | ||
|
||
const { tokens } = parseArgs({ args, options, tokens: true }); | ||
assert.deepStrictEqual(tokens, expectedTokens); | ||
}); | ||
|
||
test('tokens:true should not include the default options after the args input', () => { | ||
const args = ['--z', 'zero', 'positional-item']; | ||
const options = { | ||
z: { type: 'string' }, | ||
a: { type: 'string', default: 'HELLO' }, | ||
b: { type: 'boolean', default: false }, | ||
c: { type: 'boolean', default: true } | ||
}; | ||
|
||
const expectedTokens = [ | ||
{ kind: 'option', name: 'z', rawName: '--z', index: 0, value: 'zero', inlineValue: false }, | ||
{ kind: 'positional', index: 2, value: 'positional-item' }, | ||
]; | ||
|
||
const { tokens } = parseArgs({ args, options, tokens: true, allowPositionals: true }); | ||
assert.deepStrictEqual(tokens, expectedTokens); | ||
}); | ||
|
||
test('proto as default value must be ignored', () => { | ||
const args = []; | ||
const options = Object.create(null); | ||
|
||
// eslint-disable-next-line no-proto | ||
options.__proto__ = { type: 'string', default: 'HELLO' }; | ||
|
||
const result = parseArgs({ args, options, allowPositionals: true }); | ||
const expected = { values: { __proto__: null }, positionals: [] }; | ||
assert.deepStrictEqual(result, expected); | ||
}); | ||
|
||
|
||
test('multiple as false should expect a String', () => { | ||
const args = []; | ||
const options = { alpha: { type: 'string', multiple: false, default: ['array'] } }; | ||
assert.throws(() => { | ||
parseArgs({ args, options }); | ||
}, / must be String got array/); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.