argparse.h Source File

Reference Documentation

Platform
Intel® PAC
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.10
Napatech Software Suite: examples/common/argparse.h Source File
argparse.h
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 
21 struct argparse;
22 struct argparse_option;
23 
24 typedef int argparse_callback(struct argparse *This, const struct argparse_option *option, int terminate);
25 
29 };
30 
32  /* special */
34  /* options with no arguments */
38  /* options with arguments (optional or required) */
42 };
43 
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  */
80  const char short_name;
81  const char *long_name;
82  void *value;
83  const char *help;
85  intptr_t data;
86  int flags;
87  char *name_value;
88 };
89 
90 /*
91  * argpparse
92  */
93 struct 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
117 extern "C" {
118 #endif
119 
120 // builtin callbacks
121 int argparse_help_cb(struct argparse *This, const struct argparse_option *option, int terminate);
122 
123 int argparse_init(struct argparse *This, struct argparse_option *options,
124  const char *const *usage, int flags);
125 int argparse_parse(struct argparse *This, int argc, const char **argv);
126 void argparse_usage(struct argparse *This);
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif