poafloc

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

example_cpp.cpp (3107B)


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