perf tools: Show tool command line options ordered

When asking for a listing of the options, be it using -h or when an
unknown option is passed, order it by one-letter options, then the ones
having just long names.
Suggested-by: default avatarIngo Molnar <mingo@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Chandler Carruth <chandlerc@gmail.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-41qh68t35n4ehrpsuazp1dx8@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 80fcd45e
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "parse-options.h" #include "parse-options.h"
#include "cache.h" #include "cache.h"
#include "header.h" #include "header.h"
#include <linux/string.h>
#define OPT_SHORT 1 #define OPT_SHORT 1
#define OPT_UNSET 2 #define OPT_UNSET 2
...@@ -642,9 +643,50 @@ static void print_option_help(const struct option *opts, int full) ...@@ -642,9 +643,50 @@ static void print_option_help(const struct option *opts, int full)
fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
} }
static int option__cmp(const void *va, const void *vb)
{
const struct option *a = va, *b = vb;
int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
if (sa == 0)
sa = 'z' + 1;
if (sb == 0)
sb = 'z' + 1;
ret = sa - sb;
if (ret == 0) {
const char *la = a->long_name ?: "",
*lb = b->long_name ?: "";
ret = strcmp(la, lb);
}
return ret;
}
static struct option *options__order(const struct option *opts)
{
int nr_opts = 0;
const struct option *o = opts;
struct option *ordered;
for (o = opts; o->type != OPTION_END; o++)
++nr_opts;
ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
if (ordered == NULL)
goto out;
qsort(ordered, nr_opts, sizeof(*o), option__cmp);
out:
return ordered;
}
int usage_with_options_internal(const char * const *usagestr, int usage_with_options_internal(const char * const *usagestr,
const struct option *opts, int full) const struct option *opts, int full)
{ {
struct option *ordered;
if (!usagestr) if (!usagestr)
return PARSE_OPT_HELP; return PARSE_OPT_HELP;
...@@ -661,11 +703,17 @@ int usage_with_options_internal(const char * const *usagestr, ...@@ -661,11 +703,17 @@ int usage_with_options_internal(const char * const *usagestr,
if (opts->type != OPTION_GROUP) if (opts->type != OPTION_GROUP)
fputc('\n', stderr); fputc('\n', stderr);
ordered = options__order(opts);
if (ordered)
opts = ordered;
for ( ; opts->type != OPTION_END; opts++) for ( ; opts->type != OPTION_END; opts++)
print_option_help(opts, full); print_option_help(opts, full);
fputc('\n', stderr); fputc('\n', stderr);
free(ordered);
return PARSE_OPT_HELP; return PARSE_OPT_HELP;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment