poafloc

poafloc - Parser Of Arguments For Lines Of Commands
git clone git://git.dimitrijedobrota.com/poafloc.git
Log | Files | Refs | README | LICENSE

main.c (3071B)


      1 #include "poafloc.h"
      2 
      3 #include <stdio.h>
      4 
      5 void error(const char *message) { fprintf(stderr, "%s\n", message); }
      6 
      7 typedef struct {
      8     const char *output_file;
      9     const char *input_file;
     10 
     11     int debug;
     12     int hex;
     13     int relocatable;
     14 } arguments_t;
     15 
     16 int parse_opt(int key, const char *arg, poafloc_parser_t *parser) {
     17     arguments_t *arguments = (arguments_t *)poafloc_parser_input(parser);
     18 
     19     switch (key) {
     20     case 777: arguments->debug = 1; break;
     21     case 'h':
     22         if (arguments->relocatable) error("cannot mix -hex and -relocatable");
     23         arguments->hex = 1;
     24         break;
     25     case 'r':
     26         if (arguments->hex) error("cannot mix -hex and -relocatable");
     27         arguments->relocatable = 1;
     28         break;
     29     case 'o': arguments->output_file = arg ? arg : "stdout"; break;
     30     case 'i': arguments->input_file = arg; break;
     31     // case Parser::Key::ARG: arguments->args.push_back(arg); break;
     32     case POAFLOC_KEY_ERROR: fprintf(stderr, "handled error\n");
     33     case POAFLOC_KEY_INIT:
     34         arguments->input_file = "stdin";
     35         arguments->output_file = "stdout";
     36     }
     37 
     38     return 0;
     39 }
     40 
     41 // clang-format off
     42 static const poafloc_option_t options[] = {
     43     {           0,  'R',      0,                        0,        "random 0-group option"},
     44     {           0,    0,      0,                        0,                 "Program mode", 1},
     45     {"relocatable", 'r',      0,                        0, "Output in relocatable format"},
     46     {        "hex", 'h',      0,                        0,         "Output in hex format"},
     47     {"hexadecimal",   0,      0,  POAFLOC_OPTION_ALIAS | POAFLOC_OPTION_HIDDEN},
     48     {            0,   0,      0,                        0,               "For developers", 4},
     49     {      "debug", 777,      0,                        0,        "Enable debugging mode"},
     50     {            0,   0,      0,                        0,                 "Input/output", 3},
     51     {     "output", 'o', "file", POAFLOC_OPTION_ARG_OPTIONAL,  "Output file, default stdout"},
     52     {            0, 'i', "file",                        0,                  "Input  file"},
     53     {            0,   0,      0,                        0,        "Informational Options", -1},
     54     {0},
     55 };
     56 
     57 static const poafloc_arg_t argp = {
     58 	options, parse_opt, "doc string\nother usage",
     59 	"First half of the message\vsecond half of the message"
     60 };
     61 // clang-format on
     62 
     63 int main(int argc, char *argv[]) {
     64     arguments_t arguments = {0};
     65 
     66     if (poafloc_parse(&argp, argc, argv, 0, &arguments)) {
     67         error("There was an error while parsing arguments");
     68         return 1;
     69     }
     70 
     71     printf("Command line options:\n");
     72     printf("\t       input: %s\n", arguments.input_file);
     73     printf("\t      output: %s\n", arguments.output_file);
     74     printf("\t         hex: %d\n", arguments.hex);
     75     printf("\t       debug: %d\n", arguments.debug);
     76     printf("\t relocatable: %d\n", arguments.relocatable);
     77 
     78     // std::cout << "\t args: ";
     79     // for (const auto &arg : arguments.args)
     80     //     std::cout << arg << " ";
     81     // std::cout << std::endl;
     82 
     83     return 0;
     84 }