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