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
35// Rename argparse namespace to a GDAL private one
36#define argparse gdal_argparse
37
38// Use our locale-unaware strtod()
39#define ARGPARSE_CUSTOM_STRTOD CPLStrtodM
40
41#include "argparse/argparse.hpp"
42
43using namespace argparse;
44
45// Place-holder macro using gettext() convention to indicate (future) translatable strings
46#ifndef _
47#define _(x) (x)
48#endif
49
56 class GDALArgumentParser : public ArgumentParser
57{
58 public:
60 explicit GDALArgumentParser(const std::string &program_name,
61 bool bForBinary);
62
64 void display_error_and_usage(const std::exception &err);
65
67 void add_quiet_argument(bool *pVar);
68
70 void add_input_format_argument(CPLStringList *pvar);
71
73 void add_output_format_argument(std::string &var);
74
76 void add_creation_options_argument(CPLStringList &var);
77
79 void add_metadata_item_options_argument(CPLStringList &var);
80
82 void add_open_options_argument(CPLStringList &var);
83
85 void add_open_options_argument(CPLStringList *pvar);
86
88 void add_output_type_argument(GDALDataType &eDT);
89
91 void parse_args_without_binary_name(CSLConstList papszArgs);
92
94 void parse_args(const CPLStringList &aosArgs);
95
97 CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs);
98
105 Argument &add_inverted_logic_flag(const std::string &name,
106 bool *store_into = nullptr,
107 const std::string &help = "");
108
109 private:
110 std::map<std::string, ArgumentParser::argument_it>::iterator
111 find_argument(const std::string &name);
112};
113
114#endif /* GDALARGUMENTPARSER_H */
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition: cpl_string.h:449
GDALArgumentParser
Parse command-line arguments for GDAL utilities.
Definition: gdalargumentparser.h:57
GDALArgumentParser::add_inverted_logic_flag
Argument & add_inverted_logic_flag(const std::string &name, bool *store_into=nullptr, const std::string &help="")
Add an inverted logic (default true, false when set) flag.
Definition: gdalargumentparser.cpp:342
GDALArgumentParser::add_creation_options_argument
void add_creation_options_argument(CPLStringList &var)
Add "-co KEY=VALUE" argument for creation options, and store its value into var.
Definition: gdalargumentparser.cpp:165
GDALArgumentParser::display_error_and_usage
void display_error_and_usage(const std::exception &err)
Format an exception as an error message and display the program usage.
Definition: gdalargumentparser.cpp:98
GDALArgumentParser::parse_args_without_binary_name
void parse_args_without_binary_name(CSLConstList papszArgs)
Parse command line arguments, without the initial program name.
Definition: gdalargumentparser.cpp:238
GDALArgumentParser::add_quiet_argument
void add_quiet_argument(bool *pVar)
Add -q/–quiet argument, and store its value in *pVar (if pVar not null)
Definition: gdalargumentparser.cpp:110
GDALArgumentParser::GDALArgumentParser
GDALArgumentParser(const std::string &program_name, bool bForBinary)
Constructor.
Definition: gdalargumentparser.cpp:40
GDALArgumentParser::get_non_positional_arguments
CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs)
Return the non positional arguments.
Definition: gdalargumentparser.cpp:272
GDALArgumentParser::parse_args
void parse_args(const CPLStringList &aosArgs)
Parse command line arguments, with the initial program name.
Definition: gdalargumentparser.cpp:362
GDALArgumentParser::add_input_format_argument
void add_input_format_argument(CPLStringList *pvar)
Add "-if format_name" argument for input format, and store its value into *pvar.
Definition: gdalargumentparser.cpp:126
GDALArgumentParser::add_output_format_argument
void add_output_format_argument(std::string &var)
Add "-of format_name" argument for output format, and store its value into var.
Definition: gdalargumentparser.cpp:152
GDALArgumentParser::add_output_type_argument
void add_output_type_argument(GDALDataType &eDT)
Add "-ot data_type" argument for output type, and store its value into eDT.
Definition: gdalargumentparser.cpp:217
GDALArgumentParser::add_open_options_argument
void add_open_options_argument(CPLStringList &var)
Add "-oo KEY=VALUE" argument for open options, and store its value into var.
Definition: gdalargumentparser.cpp:191
GDALArgumentParser::add_metadata_item_options_argument
void add_metadata_item_options_argument(CPLStringList &var)
Add "-mo KEY=VALUE" argument for metadata item options, and store its value into var.
Definition: gdalargumentparser.cpp:178
cpl_conv.h
Various convenience functions for CPL.
cpl_port.h
Core portability definitions for CPL.
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1183
cpl_string.h
Various convenience functions for working with strings and string lists.
GDALDataType
GDALDataType
Definition: gdal.h:64