GDAL
gdalargumentparser.h
1 /******************************************************************************
2  * Project: GDAL Utilities
3  * Purpose: GDAL argument parser
4  * Author: Even Rouault <even.rouault at spatialys.com>
5  *
6  * ****************************************************************************
7  * Copyright (c) 2024, Even Rouault <even.rouault at spatialys.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  ****************************************************************************/
27 
28 #ifndef GDALARGUMENTPARSER_H
29 #define GDALARGUMENTPARSER_H
30 
31 #include "cpl_port.h"
32 #include "cpl_conv.h"
33 #include "cpl_string.h"
34 #include "gdal.h"
35 
36 // Rename argparse namespace to a GDAL private one
37 #define argparse gdal_argparse
38 
39 // Use our locale-unaware strtod()
40 #define ARGPARSE_CUSTOM_STRTOD CPLStrtodM
41 
42 #ifdef _MSC_VER
43 #pragma warning(push)
44 // unreachable code
45 #pragma warning(disable : 4702)
46 #endif
47 
48 #include "argparse/argparse.hpp"
49 
50 #ifdef _MSC_VER
51 #pragma warning(pop)
52 #endif
53 
54 using namespace argparse;
55 
56 // Place-holder macro using gettext() convention to indicate (future) translatable strings
57 #ifndef _
58 #define _(x) (x)
59 #endif
60 
67 class GDALArgumentParser : public ArgumentParser
68 {
69  public:
71  explicit GDALArgumentParser(const std::string &program_name,
72  bool bForBinary);
73 
75  std::string usage() const;
76 
78  void add_extra_usage_hint(const std::string &osExtraUsageHint);
79 
81  void display_error_and_usage(const std::exception &err);
82 
84  Argument &add_quiet_argument(bool *pVar);
85 
87  Argument &add_input_format_argument(CPLStringList *pvar);
88 
90  Argument &add_output_format_argument(std::string &var);
91 
93  Argument &add_creation_options_argument(CPLStringList &var);
94 
96  Argument &add_metadata_item_options_argument(CPLStringList &var);
97 
99  Argument &add_open_options_argument(CPLStringList &var);
100 
102  Argument &add_open_options_argument(CPLStringList *pvar);
103 
105  Argument &add_output_type_argument(GDALDataType &eDT);
106 
108  Argument &add_layer_creation_options_argument(CPLStringList &var);
109 
111  Argument &add_dataset_creation_options_argument(CPLStringList &var);
112 
114  void parse_args_without_binary_name(CSLConstList papszArgs);
115 
117  void parse_args(const CPLStringList &aosArgs);
118 
120  CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs);
121 
128  Argument &add_inverted_logic_flag(const std::string &name,
129  bool *store_into = nullptr,
130  const std::string &help = "");
131 
138  GDALArgumentParser *add_subparser(const std::string &description,
139  bool bForBinary);
140 
146  GDALArgumentParser *get_subparser(const std::string &name);
147 
154  bool is_used_globally(const std::string &name);
155 
156  private:
157  std::map<std::string, ArgumentParser::argument_it>::iterator
158  find_argument(const std::string &name);
159  std::vector<std::unique_ptr<GDALArgumentParser>> aoSubparsers;
160  std::string m_osExtraUsageHint{};
161 };
162 
163 #endif /* GDALARGUMENTPARSER_H */
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
Parse command-line arguments for GDAL utilities.
Definition: gdalargumentparser.h:68
Various convenience functions for CPL.
Core portability definitions for CPL.
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1183
Various convenience functions for working with strings and string lists.
Public (C callable) GDAL entry points.
GDALDataType
Definition: gdal.h:64