@@ -145,13 +145,21 @@ function optionList(command: Command): string {
145145 : `${ commandPath ( command ) } takes no options.`
146146}
147147
148+ /** What each guided command's own option parse left unknown, recorded by {@link addParseErrorGuidance}. */
149+ const UNKNOWN_ARGUMENTS = new WeakMap < Command , readonly string [ ] > ( )
150+
151+ function isDashLed ( arg : string ) : boolean {
152+ return arg . length > 1 && arg . startsWith ( '-' )
153+ }
154+
148155/**
149- * The first unknown option commander saw. Commander files it, and every
150- * operand after it, at the end of `args`; an operand never opens with a dash
151- * unless it follows `--`, and then no option is unknown.
156+ * The first unknown option commander saw. Its parse files that option and everything after it as
157+ * unknown, while an operand escaped with `--` (`files rename f_1 -- --name`) stays an operand:
158+ * `args` holds both, so only the parse's own unknown list tells them apart .
152159 */
153160function unknownOptionToken ( command : Command ) : string | undefined {
154- return command . args . find ( ( arg ) => arg . length > 1 && arg . startsWith ( '-' ) )
161+ const first = UNKNOWN_ARGUMENTS . get ( command ) ?. [ 0 ]
162+ return first !== undefined && isDashLed ( first ) ? first : undefined
155163}
156164
157165function missingMandatory ( command : Command ) : Option [ ] {
@@ -179,12 +187,12 @@ function unknownBesideMissing(command: Command): string | undefined {
179187 * was wanted (`files rename <id> new.pdf`, `knowledge search <kbId>`), when
180188 * that reading is the only one. An unknown option puts its own value among the
181189 * operands, and with two flags missing the value could belong to either, so
182- * both cases name nothing ( as does a dash-led operand escaped with `--`, which
183- * reads the same as an unknown option here ).
190+ * both cases name nothing, as does a dash-led operand escaped with `--`, which
191+ * may be the flag itself (`files rename <id> -- --name` ).
184192 */
185193function misplacedFlagValue ( command : Command ) : { option : Option ; value : string } | undefined {
186194 const missing = missingMandatory ( command )
187- if ( missing . length !== 1 || unknownOptionToken ( command ) !== undefined ) return undefined
195+ if ( missing . length !== 1 || command . args . some ( isDashLed ) ) return undefined
188196 if ( command . registeredArguments . some ( ( argument ) => argument . variadic ) ) return undefined
189197 const extra = command . args . slice ( command . registeredArguments . length )
190198 return extra . length === 1 ? { option : missing [ 0 ] , value : extra [ 0 ] } : undefined
@@ -370,6 +378,12 @@ function parseErrorGuidance(command: Command, code: string | undefined): string[
370378 * one would answer for all of them.
371379 */
372380function addParseErrorGuidance ( command : Command ) : void {
381+ const parseOptions = command . parseOptions . bind ( command )
382+ command . parseOptions = ( argv ) => {
383+ const parsed = parseOptions ( argv )
384+ UNKNOWN_ARGUMENTS . set ( command , parsed . unknown )
385+ return parsed
386+ }
373387 const error = command . error . bind ( command )
374388 command . error = ( message , options ) => {
375389 const guidance = parseErrorGuidance ( command , options ?. code )
0 commit comments