From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751991AbbJYIwW (ORCPT ); Sun, 25 Oct 2015 04:52:22 -0400 Received: from terminus.zytor.com ([198.137.202.10]:37101 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750952AbbJYIwT (ORCPT ); Sun, 25 Oct 2015 04:52:19 -0400 Date: Sun, 25 Oct 2015 01:51:37 -0700 From: tip-bot for Arnaldo Carvalho de Melo Message-ID: Cc: jolsa@redhat.com, tglx@linutronix.de, adrian.hunter@intel.com, bp@suse.de, dsahern@gmail.com, acme@redhat.com, mingo@kernel.org, a.p.zijlstra@chello.nl, brendan.d.gregg@gmail.com, eranian@google.com, wangnan0@huawei.com, namhyung@kernel.org, hpa@zytor.com, chandlerc@gmail.com, fweisbec@gmail.com, linux-kernel@vger.kernel.org Reply-To: chandlerc@gmail.com, fweisbec@gmail.com, linux-kernel@vger.kernel.org, brendan.d.gregg@gmail.com, a.p.zijlstra@chello.nl, namhyung@kernel.org, wangnan0@huawei.com, eranian@google.com, hpa@zytor.com, adrian.hunter@intel.com, mingo@kernel.org, acme@redhat.com, bp@suse.de, dsahern@gmail.com, jolsa@redhat.com, tglx@linutronix.de To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf tools: Show tool command line options ordered Git-Commit-ID: 869c55b0f473fecfe6c294c6fa965dedfe469e02 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 869c55b0f473fecfe6c294c6fa965dedfe469e02 Gitweb: http://git.kernel.org/tip/869c55b0f473fecfe6c294c6fa965dedfe469e02 Author: Arnaldo Carvalho de Melo AuthorDate: Fri, 23 Oct 2015 11:23:28 -0300 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 23 Oct 2015 21:50:49 -0300 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 Cc: Adrian Hunter Cc: Borislav Petkov Cc: Brendan Gregg Cc: Chandler Carruth Cc: David Ahern Cc: Frederic Weisbecker Cc: Jiri Olsa Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Stephane Eranian Cc: Wang Nan Link: http://lkml.kernel.org/n/tip-41qh68t35n4ehrpsuazp1dx8@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/parse-options.c | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c index 8aa7922..fb26532 100644 --- a/tools/perf/util/parse-options.c +++ b/tools/perf/util/parse-options.c @@ -2,6 +2,7 @@ #include "parse-options.h" #include "cache.h" #include "header.h" +#include #define OPT_SHORT 1 #define OPT_UNSET 2 @@ -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); } +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, const struct option *opts, int full) { + struct option *ordered; + if (!usagestr) return PARSE_OPT_HELP; @@ -661,11 +703,17 @@ int usage_with_options_internal(const char * const *usagestr, if (opts->type != OPTION_GROUP) fputc('\n', stderr); + ordered = options__order(opts); + if (ordered) + opts = ordered; + for ( ; opts->type != OPTION_END; opts++) print_option_help(opts, full); fputc('\n', stderr); + free(ordered); + return PARSE_OPT_HELP; }