1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // parseargs.cfa -- PUBLIC |
---|
8 | // API for arguments parsing (argc, argv) |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Wed Oct 12 15:28:01 2022 |
---|
12 | // Last Modified By : Peter A. Buhr |
---|
13 | // Last Modified On : Mon Jul 8 18:18:14 2024 |
---|
14 | // Update Count : 2 |
---|
15 | // |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <array.hfa> |
---|
19 | |
---|
20 | struct cfa_option { |
---|
21 | int val; // reserved |
---|
22 | char short_name; |
---|
23 | const char * long_name; |
---|
24 | const char * help; |
---|
25 | void * variable; |
---|
26 | bool (*parse)(const char *, void * ); |
---|
27 | }; |
---|
28 | |
---|
29 | extern cfa_option last_option; |
---|
30 | |
---|
31 | static inline void ?{}( cfa_option & this ) {} |
---|
32 | |
---|
33 | forall(T & | { bool parse( const char *, T & ); }) |
---|
34 | static inline void ?{}( cfa_option & this, char short_name, const char * long_name, const char * help, T & variable ) { |
---|
35 | this.val = 0; |
---|
36 | this.short_name = short_name; |
---|
37 | this.long_name = long_name; |
---|
38 | this.help = help; |
---|
39 | this.variable = (void*)&variable; |
---|
40 | this.parse = (bool (*)(const char *, void * ))parse; |
---|
41 | } |
---|
42 | |
---|
43 | forall(T &) |
---|
44 | static inline void ?{}( cfa_option & this, char short_name, const char * long_name, const char * help, T & variable, bool (*parse)(const char *, T & )) { |
---|
45 | this.val = 0; |
---|
46 | this.short_name = short_name; |
---|
47 | this.long_name = long_name; |
---|
48 | this.help = help; |
---|
49 | this.variable = (void*)&variable; |
---|
50 | this.parse = (bool (*)(const char *, void * ))parse; |
---|
51 | } |
---|
52 | |
---|
53 | void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ); |
---|
54 | void parse_args( int argc, char * argv[], cfa_option options[], size_t opt_count, const char * usage, char ** & left ); |
---|
55 | |
---|
56 | forall( [N] ) { |
---|
57 | void parse_args( const array( cfa_option, N ) & options, const char * usage, char ** & left ); |
---|
58 | void parse_args( int argc, char * argv[], const array( cfa_option, N ) & options, const char * usage, char ** & left ); |
---|
59 | } |
---|
60 | |
---|
61 | void print_args_usage(cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)); |
---|
62 | void print_args_usage(int argc, char * argv[], cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)); |
---|
63 | |
---|
64 | forall( [N] ) { |
---|
65 | void print_args_usage( const array(cfa_option, N ) & options, const char * usage, bool error) __attribute__ ((noreturn)); |
---|
66 | void print_args_usage(int argc, char * argv[], const array( cfa_option, N ) & options, const char * usage, bool error) __attribute__ ((noreturn)); |
---|
67 | } |
---|
68 | |
---|
69 | bool parse_yesno (const char *, bool & ); |
---|
70 | bool parse_truefalse(const char *, bool & ); |
---|
71 | bool parse_settrue (const char *, bool & ); |
---|
72 | bool parse_setfalse (const char *, bool & ); |
---|
73 | |
---|
74 | bool parse(const char *, const char * & ); |
---|
75 | bool parse(const char *, int & ); |
---|
76 | bool parse(const char *, unsigned & ); |
---|
77 | bool parse(const char *, unsigned long & ); |
---|
78 | bool parse(const char *, unsigned long long & ); |
---|
79 | bool parse(const char *, double & ); |
---|