diff options
author | Nicholas Johnson <nick@nicksphere.ch> | 2022-01-25 00:00:00 +0000 |
---|---|---|
committer | Nicholas Johnson <nick@nicksphere.ch> | 2022-01-25 00:00:00 +0000 |
commit | 15c7695a46acc7bd27f75da51e6b993c249f5936bfbf5b339a0ed2e281bcf2d1 (patch) | |
tree | 8f7f44f505d53a9ba0f09ebf8cad24b24d1b58cae47402efe7e007909da5f1ff | |
parent | 568624bc668045822726981a0d4518ce131eab8ff9abfb79cc257235f254d31b (diff) |
Use getopt.h instead of rolling my own arg parser
-rw-r--r-- | gemini2html.1 | 2 | ||||
-rw-r--r-- | src/main.c | 43 |
2 files changed, 32 insertions, 13 deletions
diff --git a/gemini2html.1 b/gemini2html.1 index 7436994..b3964f2 100644 --- a/gemini2html.1 +++ b/gemini2html.1 @@ -1,4 +1,4 @@ -.TH GEMINI2HTML "1" "January 2022" "gemini2html v1.0.0" +.TH GEMINI2HTML "1" "January 2022" "gemini2html v1.1.0" .SH NAME gemini2html \- convert gemini text to HTML .SH SYNOPSIS @@ -19,34 +19,53 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include "gemini2html.h" +#define VERSION "1.1.0" + /* displays the help menu */ -void display_help_text(char* prog) { +void display_help_text() { char* help_text = "Convert Gemini files to HTML.\n" "\n" "OPTIONS:\n" - " -h, --help display this help and exit\n" + " -h, --help display this help text then exit\n" + " -v, --version display program version number then exit\n" "\n"; - printf("Usage: %s [OPTION]... < input.gmi > output.html\n%s", prog, help_text); + printf("Usage: gemini2html [OPTION]... < input.gmi > output.html\n%s", help_text); +} + +/* displays version information */ +void display_version_text() { + printf("gemini2html v%s\n", VERSION); } /* CLI for gemini2html */ int main(int argc, char* argv[]) { - if (argc > 1) { - if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { - display_help_text(argv[0]); - return 0; - } else { - printf("unrecognized argument: %s\n", argv[1]); - return -1; + int c; + + while (1) { + static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + }; + + int option_index = 0; + + c = getopt_long(argc, argv, "hv", long_options, &option_index); + + if (c == -1) break; + + switch(c) { + case 'h': display_help_text(); exit (EXIT_SUCCESS); + case 'v': display_version_text(); exit (EXIT_SUCCESS); + default: return -1; } } if (gmi_to_html(stdin, stdout) == -1) return -1; - - return 0; + else return 0; }
\ No newline at end of file |