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