1 /*
2  * Copyright (c) 2012-2019, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OSKAR_OPTION_PARSER_H_
30 #define OSKAR_OPTION_PARSER_H_
31 
32 /**
33  * @file oskar_option_parser.h
34  */
35 
36 #include <settings/oskar_settings_macros.h>
37 
38 namespace oskar {
39 
40 struct OptionParserPrivate;
41 
42 /**
43  * @brief
44  * Provides a command line parser for OSKAR applications.
45  *
46  * @details
47  * Provides a command line parser for OSKAR applications.
48  *
49  * Note on the use symbols in option syntax: (the following are advised
50  * in order to maintain consistency)
51  *
52  *  []   = optional (e.g. $ foo [settings file] )
53  *  <>   = required (e.g. $ foo <file name> )
54  *  ...  = repeating elements, "and so on"
55  *  |    = mutually exclusive
56  *
57  * TODO(BM) Better handling of unexpected options.
58  * It would be useful if a warning could be printed.
59  */
60 class OptionParser
61 {
62 public:
63     OSKAR_SETTINGS_EXPORT
64     OptionParser(const char* title, const char* ver,
65             const char* settings = "");
66 
67     OSKAR_SETTINGS_EXPORT
68     virtual ~OptionParser();
69 
70     OSKAR_SETTINGS_EXPORT
71     void add_example(const char* text);
72 
73     // Wrapper to define flags with no arguments
74     OSKAR_SETTINGS_EXPORT
75     void add_flag(const char* flag1, const char* help, bool required = false,
76             const char* flag2 = 0);
77 
78     // Wrapper to define flags with arguments with default values.
79     OSKAR_SETTINGS_EXPORT
80     void add_flag(const char* flag1, const char* help, int expected_args,
81             const char* defaults = "", bool required = false,
82             const char* flag2 = 0);
83 
84     OSKAR_SETTINGS_EXPORT
85     void add_optional(const char* name, const char* help = "");
86 
87     OSKAR_SETTINGS_EXPORT
88     void add_required(const char* name, const char* help = "");
89 
90     OSKAR_SETTINGS_EXPORT
91     void add_settings_options();
92 
93     OSKAR_SETTINGS_EXPORT
94     bool check_options(int argc, char** argv);
95 
96     OSKAR_SETTINGS_EXPORT
97     void error(const char* format, ...);
98 
99     OSKAR_SETTINGS_EXPORT
100     const char* get_arg(int i = 0) const;
101 
102     OSKAR_SETTINGS_EXPORT
103     double get_double(const char* name);
104 
105     OSKAR_SETTINGS_EXPORT
106     int get_int(const char* name);
107 
108     OSKAR_SETTINGS_EXPORT
109     const char* get_string(const char* name);
110 
111     OSKAR_SETTINGS_EXPORT
112     const char* const* get_input_files(int min_required, int* num_files);
113 
114     OSKAR_SETTINGS_EXPORT
115     int is_set(const char* option);
116 
117     OSKAR_SETTINGS_EXPORT
118     void print_usage();
119 
120     OSKAR_SETTINGS_EXPORT
121     int num_args() const;
122 
123     OSKAR_SETTINGS_EXPORT
124     void set_description(const char* description);
125 
126     OSKAR_SETTINGS_EXPORT
127     void set_settings(const char* text);
128 
129     OSKAR_SETTINGS_EXPORT
130     void set_title(const char* text);
131 
132     OSKAR_SETTINGS_EXPORT
133     void set_version(const char* version, bool show = true);
134 
135 private:
136     OptionParserPrivate* p;
137 };
138 
139 } /* namespace oskar */
140 
141 #endif /* OSKAR_OPTION_PARSER_H_ */
142