Skip to content

Commit 751f3b0

Browse files
authored
fix(man-page): account for optional flag arguments (#138)
1 parent 7f2c125 commit 751f3b0

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/generators/man-page/utils/converter.mjs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ export function flagValueToMandoc(flag) {
7878
}
7979

8080
// Split the flag into the name and value based on the separator ('=' or space).
81-
const value = flag.split(sep)[1];
81+
let value = flag.split(sep)[1];
82+
83+
// If the value ends with ']', the flag's argument is optional.
84+
if (value.endsWith(']')) {
85+
value = '[' + value;
86+
}
8287

8388
// If there is no value, return an empty string.
8489
if (!value) {
@@ -89,12 +94,12 @@ export function flagValueToMandoc(flag) {
8994
const prefix = sep === ' ' ? '' : ' Ns = Ns';
9095

9196
// Combine prefix and formatted value.
92-
return `${prefix} Ar ${value.replace(/\]$/, '')}`;
97+
return `${prefix} Ar ${value}`;
9398
}
9499

95100
const formatFlag = flag =>
96101
// 'Fl' denotes a flag, followed by an optional 'Ar' (argument).
97-
`Fl ${flag.split(/[= ]/)[0].slice(1)}${flagValueToMandoc(flag)}`;
102+
`Fl ${flag.split(/\[?[= ]/)[0].slice(1)}${flagValueToMandoc(flag)}`;
98103

99104
/**
100105
* Converts an API option metadata entry into the Mandoc format.

src/test/man-page.test.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ describe('Mandoc Conversion', () => {
9494
expected: ' Ns = Ns Ar value1,value2,value3',
9595
},
9696
{ input: '-x', expected: '' },
97+
{
98+
input: '--flag[=optional value]',
99+
expected: ' Ns = Ns Ar [optional value]',
100+
},
97101
];
98102
runTests(cases, flagValueToMandoc);
99103
});

0 commit comments

Comments
 (0)