argparse.h

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Go to the documentation of this file.

1/** 2 * Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com> 3 * All rights reserved. 4 * 5 * Use of this source code is governed by a MIT-style license that can be found 6 * in the LICENSE file. 7 * 8 * Note: Some small changes has been made by Natatech. 9 */ 10 11#ifndef ARGPARSE_H 12#define ARGPARSE_H 13 14#include <assert.h> 15#include <math.h> 16#include <stdint.h> 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20 21struct argparse; 22struct argparse_option; 23 24typedef int argparse_callback(struct argparse *This, const struct argparse_option *option, int terminate); 25 26enum argparse_flag { 27 ARGPARSE_STOP_AT_NON_OPTION = 0x0001, 28 ARGPARSE_INTERNAL_OPTION = 0x8000, 29}; 30 31enum argparse_option_type { 32 /* special */ 33 ARGPARSE_OPT_END, 34 /* options with no arguments */ 35 ARGPARSE_OPT_BOOLEAN, 36 ARGPARSE_OPT_BIT, 37 ARGPARSE_OPT_BIT64, 38 /* options with arguments (optional or required) */ 39 ARGPARSE_OPT_INTEGER, 40 ARGPARSE_OPT_STRING, 41 ARGPARSE_OPT_UINT64, 42}; 43 44enum argparse_option_flags { 45 OPT_NONEG = 1, /* Negation disabled. */ 46}; 47 48/* 49 * Argparse option struct. 50 * 51 * `type`: 52 * holds the type of the option, you must have an ARGPARSE_OPT_END last in your 53 * array. 54 * 55 * `short_name`: 56 * the character to use as a short option name, '\0' if none. 57 * 58 * `long_name`: 59 * the long option name, without the leading dash, NULL if none. 60 * 61 * `value`: 62 * stores pointer to the value to be filled. 63 * 64 * `help`: 65 * the short help message associated to what the option does. 66 * Must never be NULL (except for ARGPARSE_OPT_END). 67 * 68 * `callback`: 69 * function is called when corresponding argument is parsed. 70 * 71 * `data`: 72 * associated data. Callbacks can use it like they want. 73 * 74 * `flags`: 75 * option flags. 76 * 77 */ 78struct argparse_option { 79 enum argparse_option_type type; 80 const char short_name; 81 const char *long_name; 82 void *value; 83 const char *help; 84 argparse_callback *callback; 85 intptr_t data; 86 int flags; 87 char *name_value; 88}; 89 90/* 91 * argpparse 92 */ 93struct argparse { 94 // user supplied 95 const struct argparse_option *options; 96 const char *const *usage; 97 int flags; 98 // internal context 99 int argc; 100 const char **argv; 101 const char **out; 102 int cpidx; 103 const char *optvalue; // current option value 104}; 105 106// builtin option macros 107#define OPT_END() { ARGPARSE_OPT_END, '0', "", NULL, "", NULL, 0, 0, NULL } 108#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ } 109#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ } 110#define OPT_BIT64(...) { ARGPARSE_OPT_BIT64, __VA_ARGS__ } 111#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ } 112#define OPT_UINT64(...) { ARGPARSE_OPT_UINT64, __VA_ARGS__ } 113#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ } 114#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, "show this help message and exit", argparse_help_cb, 0, 0, NULL) 115 116#ifdef __cplusplus 117extern "C" { 118#endif 119 120// builtin callbacks 121int argparse_help_cb(struct argparse *This, const struct argparse_option *option, int terminate); 122 123int argparse_init(struct argparse *This, struct argparse_option *options, 124 const char *const *usage, int flags); 125int argparse_parse(struct argparse *This, int argc, const char **argv); 126void argparse_usage(struct argparse *This); 127 128#ifdef __cplusplus 129} 130#endif 131 132#endif