aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/parse-options.c
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2015-10-23 10:23:28 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-10-23 20:50:49 -0400
commit869c55b0f473fecfe6c294c6fa965dedfe469e02 (patch)
tree26c0a7dfbb52c39cb8bf48599840f7b30003a691 /tools/perf/util/parse-options.c
parent80fcd45ee05b4ef05e61d37a5ffb70a67095a9f6 (diff)
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: Ingo 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.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/parse-options.c')
-rw-r--r--tools/perf/util/parse-options.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index 8aa7922397a9..fb26532d67c3 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -2,6 +2,7 @@
2#include "parse-options.h" 2#include "parse-options.h"
3#include "cache.h" 3#include "cache.h"
4#include "header.h" 4#include "header.h"
5#include <linux/string.h>
5 6
6#define OPT_SHORT 1 7#define OPT_SHORT 1
7#define OPT_UNSET 2 8#define OPT_UNSET 2
@@ -642,9 +643,50 @@ static void print_option_help(const struct option *opts, int full)
642 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); 643 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
643} 644}
644 645
646static int option__cmp(const void *va, const void *vb)
647{
648 const struct option *a = va, *b = vb;
649 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
650
651 if (sa == 0)
652 sa = 'z' + 1;
653 if (sb == 0)
654 sb = 'z' + 1;
655
656 ret = sa - sb;
657
658 if (ret == 0) {
659 const char *la = a->long_name ?: "",
660 *lb = b->long_name ?: "";
661 ret = strcmp(la, lb);
662 }
663
664 return ret;
665}
666
667static struct option *options__order(const struct option *opts)
668{
669 int nr_opts = 0;
670 const struct option *o = opts;
671 struct option *ordered;
672
673 for (o = opts; o->type != OPTION_END; o++)
674 ++nr_opts;
675
676 ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
677 if (ordered == NULL)
678 goto out;
679
680 qsort(ordered, nr_opts, sizeof(*o), option__cmp);
681out:
682 return ordered;
683}
684
645int usage_with_options_internal(const char * const *usagestr, 685int usage_with_options_internal(const char * const *usagestr,
646 const struct option *opts, int full) 686 const struct option *opts, int full)
647{ 687{
688 struct option *ordered;
689
648 if (!usagestr) 690 if (!usagestr)
649 return PARSE_OPT_HELP; 691 return PARSE_OPT_HELP;
650 692
@@ -661,11 +703,17 @@ int usage_with_options_internal(const char * const *usagestr,
661 if (opts->type != OPTION_GROUP) 703 if (opts->type != OPTION_GROUP)
662 fputc('\n', stderr); 704 fputc('\n', stderr);
663 705
706 ordered = options__order(opts);
707 if (ordered)
708 opts = ordered;
709
664 for ( ; opts->type != OPTION_END; opts++) 710 for ( ; opts->type != OPTION_END; opts++)
665 print_option_help(opts, full); 711 print_option_help(opts, full);
666 712
667 fputc('\n', stderr); 713 fputc('\n', stderr);
668 714
715 free(ordered);
716
669 return PARSE_OPT_HELP; 717 return PARSE_OPT_HELP;
670} 718}
671 719