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 * SPDX-License-Identifier: MIT
10 ****************************************************************************/
11
12#ifndef GDALARGUMENTPARSER_H
13#define GDALARGUMENTPARSER_H
14
15#include "cpl_port.h"
16#include "cpl_conv.h"
17#include "cpl_string.h"
18#include "gdal.h"
19
20// Rename argparse namespace to a GDAL private one
21#define argparse gdal_argparse
22
23// Use our locale-unaware strtod()
24#define ARGPARSE_CUSTOM_STRTOD CPLStrtodM
25
26#ifdef _MSC_VER
27#pragma warning(push)
28// unreachable code
29#pragma warning(disable : 4702)
30#endif
31
32#ifdef __GNUC__
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Weffc++"
35#endif
36
37#include "argparse/argparse.hpp"
38
39#ifdef __GNUC__
40#pragma GCC diagnostic pop
41#endif
42
43#ifdef _MSC_VER
44#pragma warning(pop)
45#endif
46
47 using namespace argparse;
48
49// Place-holder macro using gettext() convention to indicate (future) translatable strings
50#ifndef _
51#define _(x) (x)
52#endif
53
60 class GDALArgumentParser : public ArgumentParser
61{
62 public:
64 explicit GDALArgumentParser(const std::string &program_name,
65 bool bForBinary);
66
68 std::string usage() const;
69
71 void add_extra_usage_hint(const std::string &osExtraUsageHint);
72
74 void display_error_and_usage(const std::exception &err);
75
77 Argument &add_quiet_argument(bool *pVar);
78
80 Argument &add_input_format_argument(CPLStringList *pvar);
81
83 Argument &add_output_format_argument(std::string &var);
84
86 Argument &add_creation_options_argument(CPLStringList &var);
87
89 Argument &add_metadata_item_options_argument(CPLStringList &var);
90
92 Argument &add_open_options_argument(CPLStringList &var);
93
95 Argument &add_open_options_argument(CPLStringList *pvar);
96
98 Argument &add_output_type_argument(GDALDataType &eDT);
99
101 Argument &add_layer_creation_options_argument(CPLStringList &var);
102
104 Argument &add_dataset_creation_options_argument(CPLStringList &var);
105
107 void parse_args_without_binary_name(CSLConstList papszArgs);
108
110 void parse_args(const CPLStringList &aosArgs);
111
113 CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs);
114
121 Argument &add_inverted_logic_flag(const std::string &name,
122 bool *store_into = nullptr,
123 const std::string &help = "");
124
131 GDALArgumentParser *add_subparser(const std::string &description,
132 bool bForBinary);
133
139 GDALArgumentParser *get_subparser(const std::string &name);
140
147 bool is_used_globally(const std::string &name);
148
149 private:
150 std::map<std::string, ArgumentParser::argument_it>::iterator
151 find_argument(const std::string &name);
152 std::vector<std::unique_ptr<GDALArgumentParser>> aoSubparsers{};
153 std::string m_osExtraUsageHint{};
154};
155
156#endif /* GDALARGUMENTPARSER_H */
CPLStringList
String list class designed around our use of C "char**" string lists.
Definition cpl_string.h:436
GDALArgumentParser
Parse command-line arguments for GDAL utilities.
Definition gdalargumentparser.h:61
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:378
GDALArgumentParser::add_open_options_argument
Argument & add_open_options_argument(CPLStringList &var)
Add "-oo KEY=VALUE" argument for open options, and store its value into var.
Definition gdalargumentparser.cpp:205
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:82
GDALArgumentParser::get_subparser
GDALArgumentParser * get_subparser(const std::string &name)
Get a subparser by name (case insensitive)
Definition gdalargumentparser.cpp:404
GDALArgumentParser::add_creation_options_argument
Argument & add_creation_options_argument(CPLStringList &var)
Add "-co KEY=VALUE" argument for creation options, and store its value into var.
Definition gdalargumentparser.cpp:178
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:274
GDALArgumentParser::add_input_format_argument
Argument & add_input_format_argument(CPLStringList *pvar)
Add "-if format_name" argument for input format, and store its value into *pvar.
Definition gdalargumentparser.cpp:138
GDALArgumentParser::add_output_format_argument
Argument & add_output_format_argument(std::string &var)
Add "-of format_name" argument for output format, and store its value into var.
Definition gdalargumentparser.cpp:164
GDALArgumentParser::get_non_positional_arguments
CPLStringList get_non_positional_arguments(const CPLStringList &aosArgs)
Return the non positional arguments.
Definition gdalargumentparser.cpp:308
GDALArgumentParser::usage
std::string usage() const
Return usage message.
Definition gdalargumentparser.cpp:94
GDALArgumentParser::add_layer_creation_options_argument
Argument & add_layer_creation_options_argument(CPLStringList &var)
Add "-lco NAME=VALUE" argument for layer creation options, and store its value into var.
Definition gdalargumentparser.cpp:251
GDALArgumentParser::add_extra_usage_hint
void add_extra_usage_hint(const std::string &osExtraUsageHint)
Adds an extra usage hint.
Definition gdalargumentparser.cpp:110
GDALArgumentParser::parse_args
void parse_args(const CPLStringList &aosArgs)
Parse command line arguments, with the initial program name.
Definition gdalargumentparser.cpp:450
GDALArgumentParser::add_dataset_creation_options_argument
Argument & add_dataset_creation_options_argument(CPLStringList &var)
Add "-dsco NAME=VALUE" argument for dataset creation options, and store its value into var.
Definition gdalargumentparser.cpp:261
GDALArgumentParser::add_output_type_argument
Argument & add_output_type_argument(GDALDataType &eDT)
Add "-ot data_type" argument for output type, and store its value into eDT.
Definition gdalargumentparser.cpp:233
GDALArgumentParser::is_used_globally
bool is_used_globally(const std::string &name)
Return true if the argument is used in the command line (also checking subparsers,...
Definition gdalargumentparser.cpp:413
GDALArgumentParser::add_metadata_item_options_argument
Argument & 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:192
GDALArgumentParser::add_quiet_argument
Argument & add_quiet_argument(bool *pVar)
Add -q/–quiet argument, and store its value in *pVar (if pVar not null)
Definition gdalargumentparser.cpp:120
GDALArgumentParser::add_subparser
GDALArgumentParser * add_subparser(const std::string &description, bool bForBinary)
Create and add a subparser to the argument parser, keeping ownership.
Definition gdalargumentparser.cpp:395
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:1179
cpl_string.h
Various convenience functions for working with strings and string lists.
gdal.h
Public (C callable) GDAL entry points.
GDALDataType
GDALDataType
Definition gdal.h:48