From 56870bde45a5d08ebe6e6752a13658b17e73f32b Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 9 Jul 2021 17:32:41 +0100 Subject: [PATCH] Added usage message, added usage-then-exit function for error and non-error cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for naka's current commit: Curse − 凶 --- src/main.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 0496362..42c7a3e 100644 --- a/src/main.c +++ b/src/main.c @@ -10,18 +10,46 @@ #include +// TODO: Have Makefile define these, or import them from Makefile defines. +#define PROG_NAME "naka" +#define PROG_DESCRIPTION "find a file within other file(s)" +#define PROG_AUTHOUR "Avril " +#define PROG_LICENSE "GPL3+" +#define PROG_VERSION VERSION(0,0,0,0) + +void prog_info(FILE* out) +{ + fprintf(out, PROG_NAME " v%s - " PROG_DESCRIPTION + "\n written by %s with <3\n license %s.\n", + v_ctoss(v_rawtoc(PROG_VERSION)), + PROG_AUTHOUR, + PROG_LICENSE); +} -int main(int argc, char** argv) +void usage(FILE* out, int argc, char** argv) { IGNORE(argc); - IGNORE(argv); - INFO("main start"); + prog_info(out); + fprintf(out, "\nUsage: %s \n", argv[0] ?: PROG_NAME); +} + +// err: 0 for normal exit and print to stdout. +// err: nonzero for abnormal exit and print to stderr. +noreturn void usage_then_exit(int err, int argc, char** argv) +{ + usage( err ? stderr : stdout, argc, argv); + exit(err); +} - //INFO("Hello world %lu %lu %lu!", i, bswap(i), bswap(bswap(i))); - //INFO("Version: 0x%x (0x%x)", vers, bswap(vers)); // Check which version endian was preferrable, we'll stick with the default system endian (little, for this one.) +int main(int argc, char** argv) +{ + INFO("main start"); + + usage_then_exit(0, argc, argv); INFO("main end"); + return 0; }