@@ -2363,6 +2363,104 @@ func TestCommand_OrderOfOperations(t *testing.T) {
23632363 })
23642364}
23652365
2366+ func TestCommand_HelpFlagTakesPrecedenceOverParseErrors (t * testing.T ) {
2367+ t .Run ("root command with --help and bad flag" , func (t * testing.T ) {
2368+ r := require .New (t )
2369+ var buf bytes.Buffer
2370+ var errBuf bytes.Buffer
2371+
2372+ cmd := & Command {
2373+ Writer : & buf ,
2374+ ErrWriter : & errBuf ,
2375+ Action : func (ctx context.Context , cmd * Command ) error {
2376+ return nil
2377+ },
2378+ }
2379+
2380+ err := cmd .Run (buildTestContext (t ), []string {"command" , "--help" , "--undefined" })
2381+ r .NoError (err )
2382+ r .Contains (buf .String (), "NAME:" )
2383+ r .Contains (buf .String (), "command" )
2384+ r .NotContains (errBuf .String (), "Incorrect Usage" )
2385+ })
2386+
2387+ t .Run ("root command with -h and bad flag" , func (t * testing.T ) {
2388+ r := require .New (t )
2389+ var buf bytes.Buffer
2390+ var errBuf bytes.Buffer
2391+
2392+ cmd := & Command {
2393+ Writer : & buf ,
2394+ ErrWriter : & errBuf ,
2395+ Action : func (ctx context.Context , cmd * Command ) error {
2396+ return nil
2397+ },
2398+ }
2399+
2400+ err := cmd .Run (buildTestContext (t ), []string {"command" , "-h" , "--undefined" })
2401+ r .NoError (err )
2402+ r .Contains (buf .String (), "NAME:" )
2403+ r .Contains (buf .String (), "command" )
2404+ r .NotContains (errBuf .String (), "Incorrect Usage" )
2405+ })
2406+
2407+ t .Run ("subcommand with --help and bad flag" , func (t * testing.T ) {
2408+ r := require .New (t )
2409+ var buf bytes.Buffer
2410+ var errBuf bytes.Buffer
2411+
2412+ cmd := & Command {
2413+ Writer : & buf ,
2414+ ErrWriter : & errBuf ,
2415+ Action : func (ctx context.Context , cmd * Command ) error {
2416+ return nil
2417+ },
2418+ Commands : []* Command {
2419+ {
2420+ Name : "foo" ,
2421+ Action : func (ctx context.Context , cmd * Command ) error {
2422+ return nil
2423+ },
2424+ },
2425+ },
2426+ }
2427+
2428+ err := cmd .Run (buildTestContext (t ), []string {"command" , "foo" , "--help" , "--undefined" })
2429+ r .NoError (err )
2430+ r .Contains (buf .String (), "NAME:" )
2431+ r .Contains (buf .String (), "foo" )
2432+ r .NotContains (errBuf .String (), "Incorrect Usage" )
2433+ })
2434+
2435+ t .Run ("subcommand with bad flag shows Incorrect Usage and help" , func (t * testing.T ) {
2436+ r := require .New (t )
2437+ var buf bytes.Buffer
2438+ var errBuf bytes.Buffer
2439+
2440+ cmd := & Command {
2441+ Writer : & buf ,
2442+ ErrWriter : & errBuf ,
2443+ Action : func (ctx context.Context , cmd * Command ) error {
2444+ return nil
2445+ },
2446+ Commands : []* Command {
2447+ {
2448+ Name : "foo" ,
2449+ Action : func (ctx context.Context , cmd * Command ) error {
2450+ return nil
2451+ },
2452+ },
2453+ },
2454+ }
2455+
2456+ err := cmd .Run (buildTestContext (t ), []string {"command" , "foo" , "--undefined" })
2457+ r .Error (err )
2458+ r .Contains (buf .String (), "NAME:" )
2459+ r .Contains (buf .String (), "foo" )
2460+ r .Contains (errBuf .String (), "Incorrect Usage" )
2461+ })
2462+ }
2463+
23662464func TestFlagActionOrder (t * testing.T ) {
23672465 tests := []struct {
23682466 Name string
0 commit comments