@@ -334,6 +334,64 @@ describe('elastic CLI -- stack command tree', () => {
334334 } )
335335} )
336336
337+ describe ( 'elastic CLI -- command and option error ordering' , ( ) => {
338+ it ( 'reports an unknown subcommand before parsing its trailing options' , async ( ) => {
339+ const dir = await mkdtemp ( join ( tmpdir ( ) , 'elastic-cli-unknown-command-' ) )
340+ await writeFile ( join ( dir , '.elasticrc.yml' ) , [
341+ 'current_context: local' ,
342+ 'contexts:' ,
343+ ' local:' ,
344+ ' elasticsearch:' ,
345+ ' url: http://localhost:9200' ,
346+ '' ,
347+ ] . join ( '\n' ) )
348+
349+ try {
350+ const { code, stderr } = await runCli (
351+ [ 'stack' , 'es' , 'serch' , '--index' , 'my-index' ] ,
352+ { cwd : dir , env : { HOME : dir , USERPROFILE : dir , XDG_CONFIG_HOME : dir } }
353+ )
354+
355+ assert . equal ( code , 1 )
356+ assert . match ( stderr , / u n k n o w n c o m m a n d : s e r c h / )
357+ assert . doesNotMatch ( stderr , / u n k n o w n o p t i o n / )
358+ } finally {
359+ await rm ( dir , { recursive : true } )
360+ }
361+ } )
362+
363+ it ( 'still reports an unknown option for a valid command' , async ( ) => {
364+ const dir = await mkdtemp ( join ( tmpdir ( ) , 'elastic-cli-unknown-option-' ) )
365+ await writeFile ( join ( dir , '.elasticrc.yml' ) , [
366+ 'current_context: local' ,
367+ 'contexts:' ,
368+ ' local:' ,
369+ ' elasticsearch:' ,
370+ ' url: http://localhost:9200' ,
371+ '' ,
372+ ] . join ( '\n' ) )
373+
374+ try {
375+ const { code, stderr } = await runCli (
376+ [ 'stack' , 'es' , 'search' , '--not-a-real-option' ] ,
377+ { cwd : dir , env : { HOME : dir , USERPROFILE : dir , XDG_CONFIG_HOME : dir } }
378+ )
379+
380+ assert . equal ( code , 1 )
381+ assert . match ( stderr , / u n k n o w n o p t i o n ' - - n o t - a - r e a l - o p t i o n ' / )
382+ } finally {
383+ await rm ( dir , { recursive : true } )
384+ }
385+ } )
386+
387+ it ( 'reports an unknown option when no subcommand is provided' , async ( ) => {
388+ const { code, stderr } = await runCli ( [ 'sanitize' , '--not-a-real-option' ] )
389+
390+ assert . equal ( code , 1 )
391+ assert . match ( stderr , / u n k n o w n o p t i o n ' - - n o t - a - r e a l - o p t i o n ' / )
392+ } )
393+ } )
394+
337395describe ( 'elastic CLI -- --help --json' , ( ) => {
338396 it ( '`elastic --help --json` emits structured JSON help' , async ( ) => {
339397 const dir = await mkdtemp ( join ( tmpdir ( ) , 'elastic-cli-help-json-' ) )
0 commit comments