123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <stdio.h>
- #include "cargs.h"
- int main(int argc, char* argv[])
- {
- __cargs_decimal dbl = 0;
- __cargs_int i = 0;
- char* str = NULL;
- __cargs_bool b1 = __cargs_false;
- __cargs_bool b2 = __cargs_false;
- __cargs_bool needPrintHelp = __cargs_false;
- OptionData options[] = {
- decimalArg('d', "decimal", &dbl, "This is a decimal arg.\nHello world"),
- integerArg('i', "integer", &i, "This is an integer arg"),
- stringArg('s', "string", &str, "This is a string arg"),
- booleanArg('b', "boolean", &b1, "This is a first boolean arg"),
- booleanArg('k', NULL, &b2, "This is a second boolean arg"),
- integerArg(0, "integer-noref", NULL, "Integer arg without ref"),
- booleanArg(0, "help", &needPrintHelp, "Open help text"),
- integerArg('x', "index", NULL, "Index"),
- };
- ParseContext context = {
- .items = options,
- .itemsCount = 8,
- };
- ParseResult* result = parseArgs(&context, argc, argv);
- int errorsStatus = printErrors(&context, result, argc, argv);
- if (errorsStatus) {
- return 1;
- }
- if (needPrintHelp) {
- printHelp(&context);
- return 0;
- }
- printf("Decimal: %f\n", dbl);
- printf("Integer: %d\n", i);
- printf("String: %s\n", str);
- printf("Boolean 1: %d\n", b1);
- printf("Boolean 2: %d\n", b2);
- printf("Free values:\n");
- for (int i = 0; i < result->freeValuesCount; i++) {
- printf(" %s\n", result->freeValues[i]);
- }
- printf("Integer options (amount: %zu):\n", result->integerOptionsCount);
- for (size_t i = 0; i < result->integerOptionsCount; i++) {
- const char shortName = options[result->integerOptions[i]].shortOption;
- printf(" %c: %d\n", shortName == 0 ? '?' : shortName, result->integerValues[i]);
- }
- freeResult(result);
- return 0;
- }
|