Skip to content

Commit aa3d60e

Browse files
jpoimboePeter Zijlstra
authored andcommitted
libsubcmd: Fix OPTION_GROUP sorting
The OPTION_GROUP option type is a way of grouping certain options together in the printed usage text. It happens to be completely broken, thanks to the fact that the subcmd option sorting just sorts everything, without regard for grouping. Luckily, nobody uses this option anyway, though that will change shortly. Fix it by sorting each group individually. Signed-off-by: Josh Poimboeuf <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Link: https://lkml.kernel.org/r/e167ea3a11e2a9800eb062c1fd0f13e9cd05140c.1650300597.git.jpoimboe@redhat.com
1 parent 3398b12 commit aa3d60e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

tools/lib/subcmd/parse-options.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,9 @@ static int option__cmp(const void *va, const void *vb)
806806

807807
static struct option *options__order(const struct option *opts)
808808
{
809-
int nr_opts = 0, len;
809+
int nr_opts = 0, nr_group = 0, len;
810810
const struct option *o = opts;
811-
struct option *ordered;
811+
struct option *opt, *ordered, *group;
812812

813813
for (o = opts; o->type != OPTION_END; o++)
814814
++nr_opts;
@@ -819,7 +819,18 @@ static struct option *options__order(const struct option *opts)
819819
goto out;
820820
memcpy(ordered, opts, len);
821821

822-
qsort(ordered, nr_opts, sizeof(*o), option__cmp);
822+
/* sort each option group individually */
823+
for (opt = group = ordered; opt->type != OPTION_END; opt++) {
824+
if (opt->type == OPTION_GROUP) {
825+
qsort(group, nr_group, sizeof(*opt), option__cmp);
826+
group = opt + 1;
827+
nr_group = 0;
828+
continue;
829+
}
830+
nr_group++;
831+
}
832+
qsort(group, nr_group, sizeof(*opt), option__cmp);
833+
823834
out:
824835
return ordered;
825836
}

0 commit comments

Comments
 (0)