poafloc

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

main.cpp (2986B)


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