diff options
author | Mike Frysinger <vapier@gentoo.org> | 2013-04-15 22:13:12 -0400 |
---|---|---|
committer | David Gibson <david@gibson.dropbear.id.au> | 2013-05-24 18:20:53 +1000 |
commit | be8d1c82cb0a9caeb7e2f804f9a9f845063d7d53 (patch) | |
tree | c850c488754c0d3144eaf2a4c7861e5eea50e6fb /util.c | |
parent | 4e76ec796c90d44d417f82d9db2d67cfe575f8ed (diff) | |
download | dtc-be8d1c82cb0a9caeb7e2f804f9a9f845063d7d53.zip dtc-be8d1c82cb0a9caeb7e2f804f9a9f845063d7d53.tar.gz dtc-be8d1c82cb0a9caeb7e2f804f9a9f845063d7d53.tar.bz2 |
fdtdump: make usage a bit more friendly
This starts a new usage framework and then cuts fdtdump over to it.
Now we can do `fdtdump -h` and get something useful back.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -392,3 +392,57 @@ void util_version(void) printf("Version: %s\n", DTC_VERSION); exit(0); } + +void util_long_usage(const char *errmsg, const char *synopsis, + const char *short_opts, struct option const long_opts[], + const char * const opts_help[]) +{ + FILE *fp = errmsg ? stderr : stdout; + const char a_arg[] = "<arg>"; + size_t a_arg_len = strlen(a_arg) + 1; + size_t i; + int optlen; + + fprintf(fp, + "Usage: %s\n" + "\n" + "Options: -[%s]\n", synopsis, short_opts); + + /* prescan the --long opt length to auto-align */ + optlen = 0; + for (i = 0; long_opts[i].name; ++i) { + /* +1 is for space between --opt and help text */ + int l = strlen(long_opts[i].name) + 1; + if (long_opts[i].has_arg == a_argument) + l += a_arg_len; + if (optlen < l) + optlen = l; + } + + for (i = 0; long_opts[i].name; ++i) { + /* helps when adding new applets or options */ + assert(opts_help[i] != NULL); + + /* first output the short flag if it has one */ + if (long_opts[i].val > '~') + fprintf(fp, " "); + else + fprintf(fp, " -%c, ", long_opts[i].val); + + /* then the long flag */ + if (long_opts[i].has_arg == no_argument) + fprintf(fp, "--%-*s", optlen, long_opts[i].name); + else + fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg, + (int)(optlen - strlen(long_opts[i].name) - a_arg_len), ""); + + /* finally the help text */ + fprintf(fp, "%s\n", opts_help[i]); + } + + if (errmsg) { + fprintf(fp, "\nError: %s\n", errmsg); + exit(EXIT_FAILURE); + } else + exit(EXIT_SUCCESS); +} |