Skip to content

Commit e6a219f

Browse files
test: update return structure and improve tests descriptions
1 parent 213c061 commit e6a219f

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

doc/api/util.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,11 +2055,11 @@ changes:
20552055
* Returns: {Object} The parsed command line arguments:
20562056
* `values` {Object} A mapping of parsed option names with their {string}
20572057
or {boolean} values.
2058+
* `help` {string | undefined} Formatted help text for all options provided. Only included if general `help` text
2059+
is available.
20582060
* `positionals` {string\[]} Positional arguments.
20592061
* `tokens` {Object\[] | undefined} See [parseArgs tokens](#parseargs-tokens)
20602062
section. Only returned if `config` includes `tokens: true`.
2061-
* `printUsage` {string | undefined} Formatted help text for all options provided. Only included if general `help` text
2062-
is available.
20632063

20642064
Provides a higher level API for command-line argument parsing than interacting
20652065
with `process.argv` directly. Takes a specification for the expected arguments
@@ -2109,7 +2109,7 @@ console.log(values, positionals);
21092109

21102110
`parseArgs` supports automatic formatted help text generation for command-line options. To use this feature, provide
21112111
general help text using the `help` config property, and also
2112-
add a `help` property to each option can be optionally included.
2112+
a `help` property to each option can be optionally included.
21132113

21142114
```mjs
21152115
import { parseArgs } from 'node:util';

lib/internal/util/parse_args/parse_args.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,8 @@ function formatHelpTextForPrint(longOption, optionConfig) {
328328
helpTextForPrint += `--${longOption}`;
329329
if (type === 'string') {
330330
helpTextForPrint += ' <arg>';
331-
} else if (type === 'boolean') {
332-
helpTextForPrint += '';
333331
}
332+
334333
if (help) {
335334
if (helpTextForPrint.length > layoutSpacing) {
336335
helpTextForPrint += '\n' + ''.padEnd(layoutSpacing) + help;
@@ -462,7 +461,7 @@ const parseArgs = (config = kEmptyObject) => {
462461
});
463462

464463
if (help && printUsage.length > 0) {
465-
result.printUsage = printUsage;
464+
result.values.help = printUsage;
466465
}
467466

468467
return result;

test/parallel/test-parse-args.mjs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,58 +1082,66 @@ test('help value for option must be a string', () => {
10821082
);
10831083
});
10841084

1085+
test('when option has help text values but help arg value is not provided, then no help value appear', () => {
1086+
const args = ['-f', 'bar'];
1087+
const options = { foo: { type: 'string', short: 'f', help: 'help text'} };
1088+
const expected = { values: { __proto__: null, foo: 'bar' }, positionals: []};
1089+
const result = parseArgs({ args, options, allowPositionals: true });
1090+
assert.deepStrictEqual(result, expected);
1091+
})
1092+
10851093
test('when option has short and long flags, then both appear in usage', () => {
10861094
const args = ['-f', 'bar'];
10871095
const options = { foo: { type: 'string', short: 'f', help: 'help text' } };
10881096
const help = 'Description for some awesome stuff:';
10891097
const printUsage = help + '\n-f, --foo <arg> help text';
1090-
const expected = { values: { __proto__: null, foo: 'bar' }, positionals: [], printUsage };
1098+
const expected = { values: { __proto__: null, foo: 'bar', help: printUsage }, positionals: [] };
10911099
const result = parseArgs({ args, options, allowPositionals: true, help });
10921100
assert.deepStrictEqual(result, expected);
10931101
});
10941102

1095-
test('when help arg with help value for short group option is added, then add help text', () => {
1103+
test('when options has short group flags, then both appear in usage', () => {
10961104
const args = ['-fm', 'bar'];
10971105
const options = { foo: { type: 'boolean', short: 'f', help: 'help text' },
10981106
moo: { type: 'string', short: 'm', help: 'help text' } };
10991107
const help = 'Description for some awesome stuff:';
11001108
const printUsage = help + '\n-f, --foo help text\n-m, --moo <arg> help text';
1101-
const expected = { values: { __proto__: null, foo: true, moo: 'bar' }, positionals: [], printUsage };
1109+
const expected = { values: { help: printUsage, __proto__: null, foo: true, moo: 'bar' }, positionals: [] };
11021110
const result = parseArgs({ args, options, allowPositionals: true, help });
11031111
assert.deepStrictEqual(result, expected);
11041112
});
11051113

1106-
test('when help arg with help value for short option and value is added, then add help text', () => {
1114+
test('when options has short flag with value, then both appear in usage', () => {
11071115
const args = ['-fFILE'];
11081116
const options = { foo: { type: 'string', short: 'f', help: 'help text' } };
11091117
const help = 'Description for some awesome stuff:';
11101118
const printUsage = help + '\n-f, --foo <arg> help text';
1111-
const expected = { values: { __proto__: null, foo: 'FILE' }, positionals: [], printUsage };
1119+
const expected = { values: { help: printUsage, __proto__: null, foo: 'FILE' }, positionals: [] };
11121120
const result = parseArgs({ args, options, allowPositionals: true, help });
11131121
assert.deepStrictEqual(result, expected);
11141122
});
11151123

1116-
test('when help arg with help value for lone long option is added, then add help text', () => {
1124+
test('when options has long flag, then it appear in usage', () => {
11171125
const args = ['--foo', 'bar'];
11181126
const options = { foo: { type: 'string', help: 'help text' } };
11191127
const help = 'Description for some awesome stuff:';
11201128
const printUsage = help + '\n--foo <arg> help text';
1121-
const expected = { values: { __proto__: null, foo: 'bar' }, positionals: [], printUsage };
1129+
const expected = { values: { help: printUsage, __proto__: null, foo: 'bar' }, positionals: [] };
11221130
const result = parseArgs({ args, options, allowPositionals: true, help });
11231131
assert.deepStrictEqual(result, expected);
11241132
});
11251133

1126-
test('when help arg with help value for lone long option and value is added, then add help text', () => {
1134+
test('when options has long flag with value, then both appear in usage', () => {
11271135
const args = ['--foo=bar'];
11281136
const options = { foo: { type: 'string', help: 'help text' } };
11291137
const help = 'Description for some awesome stuff:';
11301138
const printUsage = help + '\n--foo <arg> help text';
1131-
const expected = { values: { __proto__: null, foo: 'bar' }, positionals: [], printUsage };
1139+
const expected = { values: { help: printUsage, __proto__: null, foo: 'bar' }, positionals: [] };
11321140
const result = parseArgs({ args, options, allowPositionals: true, help });
11331141
assert.deepStrictEqual(result, expected);
11341142
});
11351143

1136-
test('when help arg with help values and without explicit help texts, then add help text', () => {
1144+
test('when options has help values with and without explicit texts, then all appear in usage', () => {
11371145
const args = [
11381146
'-h', '-a', 'val1',
11391147
];
@@ -1174,10 +1182,10 @@ test('when help arg with help values and without explicit help texts, then add h
11741182
'-L, --looooooooooooooongHelpText <arg>\n' +
11751183
' Very long option help text for demonstration purposes';
11761184

1177-
assert.strictEqual(result.printUsage, printUsage);
1185+
assert.strictEqual(result.values.help, printUsage);
11781186
});
11791187

1180-
test('when help arg but no help text is available, then add help text', () => {
1188+
test('when general help text and options with no help values, then all appear in usage', () => {
11811189
const args = ['-a', 'val1', '--help'];
11821190
const help = 'Description for some awesome stuff:';
11831191
const options = { alpha: { type: 'string', short: 'a' }, help: { type: 'boolean' } };
@@ -1188,5 +1196,5 @@ test('when help arg but no help text is available, then add help text', () => {
11881196

11891197
const result = parseArgs({ args, options, help });
11901198

1191-
assert.strictEqual(result.printUsage, printUsage);
1199+
assert.strictEqual(result.values.help, printUsage);
11921200
});

0 commit comments

Comments
 (0)