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