]> git.saurik.com Git - apple/security.git/blob - lib/SecArgParse.h
Security-59306.80.4.tar.gz
[apple/security.git] / lib / SecArgParse.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef SecArgParse_h
25 #define SecArgParse_h
26
27 #include <getopt.h>
28
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 /* This is a poor simulacrum of python's argparse library. To use, create an arguments struct, including an array of
34 * argument elements, and pass the struct (along with argv+argc) to options_parse(). This is one-shot argument parsing:
35 * you must set pointers for *argument and *flag in each option to receive the results of the argument parsing.
36 *
37 * Currently does not support:
38 * non-string arguments
39 * default values
40 * relationships between arguments
41 * detecting meaningless option configurations
42 *
43 * Example arguments:
44 * static struct argument options[] = {
45 * { .shortname='p', .longname="perfcounters", .flag=&perfCounters, .flagval=true, .description="Print performance counters"},
46 * { .longname="test", .flag=&test, .flagval=true, .description="test long option"},
47 * { .command="resync", .flag=&resync, .flagval=true, .description="Initiate a resync"},
48 * { .positional_name="positional", .positional_optional=false, .argument=&position, .description = "Positional argument" },
49 * { .shortname='a', .longname="asdf", .argname="number", .argument=&asdf, .description = "Long arg with argument" },
50 * };
51 *
52 * static struct arguments args = {
53 * .programname="testctl",
54 * .description="Control and report",
55 * .arguments = options,
56 * };
57 *
58 * Note: this library automatically adds a '-h' and a '--help' operation. Don't try to override this.
59 */
60
61 struct argument {
62 char shortname;
63 char* longname;
64 char* command;
65 char* positional_name;
66 bool positional_optional;
67 char* argname;
68
69 char** argument;
70 int* flag;
71 int flagval;
72 char* description;
73 };
74
75 struct arguments {
76 char* programname;
77 char* description;
78
79 struct argument* arguments;
80 };
81
82 bool options_parse(int argc, char * const *argv, struct arguments* args);
83 void print_usage(struct arguments* args);
84
85 #endif /* SecArgParse_h */