Skip to content

Commit 02cd018

Browse files
authored
feat!: Require type to be specified for each supplied option (#95)
1 parent 1dafb6e commit 02cd018

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ process.mainArgs = process.argv.slice(process._exec ? 1 : 2)
8181
* `args` {string[]} (Optional) Array of argument strings; defaults
8282
to [`process.mainArgs`](process_argv)
8383
* `options` {Object} (Optional) An object describing the known options to look for in `args`; `options` keys are the long names of the known options, and the values are objects with the following properties:
84-
* `type` {'string'|'boolean'} (Optional) Type of known option; defaults to `'boolean'`;
84+
* `type` {'string'|'boolean'} (Required) Type of known option
8585
* `multiple` {boolean} (Optional) If true, when appearing one or more times in `args`, results are collected in an `Array`
8686
* `short` {string} (Optional) A single character alias for an option; When appearing one or more times in `args`; Respects the `multiple` configuration
8787
* `strict` {Boolean} (Optional) A `Boolean` on wheather or not to throw an error when unknown args are encountered
@@ -147,6 +147,7 @@ const args = ['-f', 'b'];
147147
const options = {
148148
foo: {
149149
short: 'f',
150+
type: 'boolean'
150151
},
151152
};
152153
const { flags, values, positionals } = parseArgs({ args, options });

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ const parseArgs = ({
111111
({ 0: longOption, 1: optionConfig }) => {
112112
validateObject(optionConfig, `options.${longOption}`);
113113

114-
if (ObjectHasOwn(optionConfig, 'type')) {
115-
validateUnion(optionConfig.type, `options.${longOption}.type`, ['string', 'boolean']);
116-
}
114+
validateUnion(optionConfig.type, `options.${longOption}.type`, ['string', 'boolean']);
117115

118116
if (ObjectHasOwn(optionConfig, 'short')) {
119117
const shortOption = optionConfig.short;

test/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('when short option `type: "string"` used with value then stored as value',
3939

4040
test('when short option listed in short used as flag then long option stored as flag', (t) => {
4141
const passedArgs = ['-f'];
42-
const passedOptions = { foo: { short: 'f' } };
42+
const passedOptions = { foo: { short: 'f', type: 'boolean' } };
4343
const expected = { flags: { foo: true }, values: { foo: undefined }, positionals: [] };
4444
const args = parseArgs({ args: passedArgs, options: passedOptions });
4545

@@ -114,7 +114,7 @@ test('handles short-option groups in conjunction with long-options', (t) => {
114114

115115
test('handles short-option groups with "short" alias configured', (t) => {
116116
const passedArgs = ['-rf'];
117-
const passedOptions = { remove: { short: 'r' } };
117+
const passedOptions = { remove: { short: 'r', type: 'boolean' } };
118118
const expected = { flags: { remove: true, f: true }, values: { remove: undefined, f: undefined }, positionals: [] };
119119
const args = parseArgs({ args: passedArgs, options: passedOptions });
120120
t.deepEqual(args, expected);
@@ -375,6 +375,16 @@ test('invalid argument passed for options', (t) => {
375375
t.end();
376376
});
377377

378+
test('then type property missing for option then throw', function(t) {
379+
const knownOptions = { foo: { } };
380+
381+
t.throws(function() { parseArgs({ options: knownOptions }); }, {
382+
code: 'ERR_INVALID_ARG_TYPE'
383+
});
384+
385+
t.end();
386+
});
387+
378388
test('boolean passed to "type" option', (t) => {
379389
const passedArgs = ['--so=wat'];
380390
const passedOptions = { foo: { type: true } };
@@ -399,7 +409,7 @@ test('invalid union value passed to "type" option', (t) => {
399409

400410
test('invalid short option length', (t) => {
401411
const passedArgs = [];
402-
const passedOptions = { foo: { short: 'fo' } };
412+
const passedOptions = { foo: { short: 'fo', type: 'boolean' } };
403413

404414
t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
405415
code: 'ERR_INVALID_SHORT_OPTION'

test/short-option-combined-with-value.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test('when combine string short with value like negative number then parsed as v
6262

6363
test('when combine string short with value which matches configured flag then parsed as value', (t) => {
6464
const passedArgs = ['-af'];
65-
const passedOptions = { alpha: { short: 'a', type: 'string' }, file: { short: 'f' } };
65+
const passedOptions = { alpha: { short: 'a', type: 'string' }, file: { short: 'f', type: 'boolean' } };
6666
const expected = { flags: { alpha: true }, values: { alpha: 'f' }, positionals: [] };
6767

6868
const result = parseArgs({ args: passedArgs, options: passedOptions });

test/short-option-groups.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('when pass zero-config group of booleans then parsed as booleans', (t) => {
1717

1818
test('when pass low-config group of booleans then parsed as booleans', (t) => {
1919
const passedArgs = ['-rf', 'p'];
20-
const passedOptions = { r: {}, f: {} };
20+
const passedOptions = { r: { type: 'boolean' }, f: { type: 'boolean' } };
2121
const expected = { flags: { r: true, f: true }, values: { r: undefined, f: undefined }, positionals: ['p'] };
2222

2323
const result = parseArgs({ args: passedArgs, options: passedOptions });

0 commit comments

Comments
 (0)