urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.89k stars 1.69k forks source link

Flags are alphabetically sorted when using categories #1914

Open NilsJPWerner opened 1 month ago

NilsJPWerner commented 1 month ago

My urfave/cli version is

github.com/urfave/cli/v2 v2.27.1

Checklist

Dependency Management

Describe the bug

The docs state that flags are not sorted by default. However when using categories the flags within each category is sorted alphabetically. Furthermore the categories are also sorted alphabetically.

To reproduce

var TestCmd = &cli.Command{
    Name: "test",
    Flags: []cli.Flag{
        &cli.StringFlag{
            Name:     "B_flag_2",
            Category: "B",
        },
        &cli.StringFlag{
            Name:     "B_flag_1",
            Category: "B",
        },
        &cli.StringFlag{
            Name:     "A_flag_2",
            Category: "A",
        },
        &cli.StringFlag{
            Name:     "A_flag_1",
            Category: "A",
        },
    },
    Action: func(c *cli.Context) error {
        return nil
    },
}

Observed behavior

NAME:
   main test

USAGE:
   main test [command options] [arguments...]

OPTIONS:
   A

   --A_flag_1 value  
   --A_flag_2 value  

   B

   --B_flag_1 value  
   --B_flag_2 value  

Expected behavior

NAME:
   main test

USAGE:
   main test [command options] [arguments...]

OPTIONS:
   B

   --B_flag_2 value  
   --B_flag_1 value  

   A

   --A_flag_2 value
   --A_flag_1 value    
dearchap commented 1 month ago

@NilsJPWerner Yes that is the current behaviour. I agree it would be better to make it configurable(i.e sort or not) but at this point we arent adding any features to v2. Yes this is a bug but its not "security critical" so most likely we wont fix. We can do this for v3 . Would you like to make a PR for v3 ?

NilsJPWerner commented 1 month ago

Is v3 substantially different?

dearchap commented 1 month ago

Not really