advgetopt 2.0.50
Parse complex command line arguments and configuration files in C++.
flags.h
Go to the documentation of this file.
1// Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/advgetopt
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
31// C++
32//
33#include <cstdint>
34
35
36
37namespace advgetopt
38{
39
40
41
42typedef std::uint32_t flag_t;
43
44static constexpr flag_t GETOPT_FLAG_NONE = static_cast<flag_t>(0x00000000);
45
46static constexpr flag_t GETOPT_FLAG_COMMAND_LINE = static_cast<flag_t>(0x00000001); // acceptable on the command line
47static constexpr flag_t GETOPT_FLAG_ENVIRONMENT_VARIABLE = static_cast<flag_t>(0x00000002); // acceptable in environment variable
48static constexpr flag_t GETOPT_FLAG_CONFIGURATION_FILE = static_cast<flag_t>(0x00000004); // acceptable in configuration files
49static constexpr flag_t GETOPT_FLAG_DYNAMIC_CONFIGURATION = static_cast<flag_t>(0x00000008); // acceptable from the dynamic configuration system (see fluid-settings)
50
51static constexpr flag_t GETOPT_FLAG_ALIAS = static_cast<flag_t>(0x00000010); // alias, result in another option defined in "help" string
52static constexpr flag_t GETOPT_FLAG_FLAG = static_cast<flag_t>(0x00000020); // no parameter allowed (--help)
53static constexpr flag_t GETOPT_FLAG_REQUIRED = static_cast<flag_t>(0x00000040); // requires an additional parameter (--host 127.0.0.1)
54static constexpr flag_t GETOPT_FLAG_MULTIPLE = static_cast<flag_t>(0x00000080); // any number of parameters are allowed (--files a b c d ...)
55static constexpr flag_t GETOPT_FLAG_DEFAULT_OPTION = static_cast<flag_t>(0x00000100); // where entries go by default (a.k.a. after "--")
56static constexpr flag_t GETOPT_FLAG_HAS_DEFAULT = static_cast<flag_t>(0x00000200); // default value is defined
57static constexpr flag_t GETOPT_FLAG_PROCESS_VARIABLES = static_cast<flag_t>(0x00000400); // variables within this parameter are automatically processed
58static constexpr flag_t GETOPT_FLAG_ARRAY = static_cast<flag_t>(0x00000800); // allow for options to include an array or map specification (--opt[3] or --opt:back)
59
60static constexpr flag_t GETOPT_FLAG_SHOW_MOST = static_cast<flag_t>(0x00000000); // show in usage() when not in GROUP1 or GROUP2
61static constexpr flag_t GETOPT_FLAG_SHOW_USAGE_ON_ERROR = static_cast<flag_t>(0x00001000); // show in usage() when an error occurs
62static constexpr flag_t GETOPT_FLAG_SHOW_ALL = static_cast<flag_t>(0x00002000); // show in usage() when --long-help is used
63static constexpr flag_t GETOPT_FLAG_SHOW_GROUP1 = static_cast<flag_t>(0x00004000); // show in usage() when --<group1>-help is used (app dependent)
64static constexpr flag_t GETOPT_FLAG_SHOW_GROUP2 = static_cast<flag_t>(0x00008000); // show in usage() when --<group2>-help is used (app dependent)
65static constexpr flag_t GETOPT_FLAG_SHOW_SYSTEM = static_cast<flag_t>(0x00010000); // show in usage() when --system-help is used (options added by advgetopt, snaplogger, communicatord, etc.)
66
67static constexpr flag_t GETOPT_FLAG_GROUP_MASK = static_cast<flag_t>(0x00700000);
68static constexpr flag_t GETOPT_FLAG_GROUP_MINIMUM = static_cast<flag_t>(0);
69static constexpr flag_t GETOPT_FLAG_GROUP_MAXIMUM = static_cast<flag_t>(7);
70static constexpr flag_t GETOPT_FLAG_GROUP_SHIFT = static_cast<flag_t>(20);
71static constexpr flag_t GETOPT_FLAG_GROUP_NONE = static_cast<flag_t>(0x00000000); // not in a group
72static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS = static_cast<flag_t>(0x00100000); // in command group (group 1)
73static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS = static_cast<flag_t>(0x00200000); // in option group (group 2)
74static constexpr flag_t GETOPT_FLAG_GROUP_THREE = static_cast<flag_t>(0x00300000); // in group 3
75static constexpr flag_t GETOPT_FLAG_GROUP_FOUR = static_cast<flag_t>(0x00400000); // in group 4
76static constexpr flag_t GETOPT_FLAG_GROUP_FIVE = static_cast<flag_t>(0x00500000); // in group 5
77static constexpr flag_t GETOPT_FLAG_GROUP_SIX = static_cast<flag_t>(0x00600000); // in group 6
78static constexpr flag_t GETOPT_FLAG_GROUP_SEVEN = static_cast<flag_t>(0x00700000); // in group 7
79
80static constexpr flag_t GETOPT_FLAG_REMOVE_NAMESPACE = static_cast<flag_t>(0x10000000); // create a command line option without the namespace (useful along dynamic options)
81static constexpr flag_t GETOPT_FLAG_DYNAMIC = static_cast<flag_t>(0x20000000); // this value was found in a configuration file and dynamic parameters are allowed (i.e. no definition for this option was found)
82static constexpr flag_t GETOPT_FLAG_LOCK = static_cast<flag_t>(0x40000000); // this value is currently locked (can't be modified)
83
84static constexpr flag_t GETOPT_FLAG_END = static_cast<flag_t>(0x80000000); // mark the end of the list
85
86
87
88template<class none = void>
90{
91 return GETOPT_FLAG_NONE;
92}
93
94
95template<flag_t flag, flag_t ...args>
97{
98 static_assert((flag & GETOPT_FLAG_GROUP_MASK) == 0
100 , "more than one GETOPT_FLAG_GROUP_... is not allowed within one set of flags.");
101
102 return flag | option_flags_merge<args...>();
103}
104
105
106template<flag_t flag, flag_t ...args>
108{
110
111 static_assert(static_cast<int>((result & GETOPT_FLAG_FLAG) != 0)
113 + static_cast<int>((result & GETOPT_FLAG_END) != 0)
114 <= 1
115 , "flag GETOPT_FLAG_FLAG is not compatible with any of GETOPT_FLAG_REQUIRED | GETOPT_FLAG_MULTIPLE | GETOPT_FLAG_DEFAULT_OPTION or none of these flags were specified.");
116
118 ^ ((result & GETOPT_FLAG_END) != 0)
119 , "flags must include at least one of GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE or be set to GETOPT_FLAG_END");
120
121 return result;
122}
123
124
125constexpr flag_t end_flags()
126{
128}
129
130
131template<flag_t ...args>
132constexpr flag_t any_flags()
133{
135
136 static_assert((result & GETOPT_FLAG_END) == 0
137 , "an option_flag() cannot include GETOPT_FLAG_END");
138
139 return result;
140}
141
142
143template<flag_t ...args>
145{
147
148 //static_assert((result & (GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE)) == 0
149 // , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
150
151 return result;
152}
153
154
155template<flag_t ...args>
165
166
167template<flag_t ...args>
178
179
180template<flag_t ...args>
182{
184
186 , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
187
188 return result;
189}
190
191
192template<flag_t ...args>
194{
196
198 , "an option_flag() cannot include GETOPT_FLAG_ENVIRONMENT_VARIABLE | GETOPT_FLAG_CONFIGURATION_FILE");
199
200 return result;
201}
202
203
204template<flag_t ...args>
205constexpr flag_t var_flags()
206{
208
210 , "a config_flag() cannot include GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_CONFIGURATION_FILE");
211
212 return result;
213}
214
215
216template<flag_t ...args>
218{
220
222 , "a config_flag() cannot include GETOPT_FLAG_COMMAND_LINE | GETOPT_FLAG_ENVIRONMENT_VARIABLE");
223
224 return result;
225}
226
227
228
229
230
231
232} // namespace advgetopt
233// vim: ts=4 sw=4 et
The advgetopt environment to parse command line options.
Definition version.h:37
constexpr flag_t config_flags()
Definition flags.h:217
static constexpr flag_t GETOPT_FLAG_GROUP_MINIMUM
Definition flags.h:68
static constexpr flag_t GETOPT_FLAG_GROUP_SEVEN
Definition flags.h:78
static constexpr flag_t GETOPT_FLAG_NONE
Definition flags.h:44
static constexpr flag_t GETOPT_FLAG_LOCK
Definition flags.h:82
static constexpr flag_t GETOPT_FLAG_GROUP_MASK
Definition flags.h:67
static constexpr flag_t GETOPT_FLAG_ARRAY
Definition flags.h:58
static constexpr flag_t GETOPT_FLAG_REMOVE_NAMESPACE
Definition flags.h:80
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
Definition flags.h:73
static constexpr flag_t GETOPT_FLAG_SHOW_USAGE_ON_ERROR
Definition flags.h:61
static constexpr flag_t GETOPT_FLAG_SHOW_GROUP1
Definition flags.h:63
constexpr flag_t end_flags()
Definition flags.h:125
static constexpr flag_t GETOPT_FLAG_END
Definition flags.h:84
static constexpr flag_t GETOPT_FLAG_SHOW_SYSTEM
Definition flags.h:65
constexpr flag_t option_flags_merge()
Definition flags.h:89
static constexpr flag_t GETOPT_FLAG_GROUP_MAXIMUM
Definition flags.h:69
static constexpr flag_t GETOPT_FLAG_DYNAMIC_CONFIGURATION
Definition flags.h:49
static constexpr flag_t GETOPT_FLAG_PROCESS_VARIABLES
Definition flags.h:57
constexpr flag_t command_flags()
Definition flags.h:193
static constexpr flag_t GETOPT_FLAG_COMMAND_LINE
Definition flags.h:46
constexpr flag_t standalone_command_flags()
Definition flags.h:181
static constexpr flag_t GETOPT_FLAG_GROUP_FOUR
Definition flags.h:75
static constexpr flag_t GETOPT_FLAG_GROUP_SIX
Definition flags.h:77
static constexpr flag_t GETOPT_FLAG_SHOW_GROUP2
Definition flags.h:64
std::uint32_t flag_t
Definition flags.h:42
static constexpr flag_t GETOPT_FLAG_CONFIGURATION_FILE
Definition flags.h:48
constexpr flag_t option_flags()
Definition flags.h:144
static constexpr flag_t GETOPT_FLAG_GROUP_SHIFT
Definition flags.h:70
static constexpr flag_t GETOPT_FLAG_FLAG
Definition flags.h:52
static constexpr flag_t GETOPT_FLAG_DYNAMIC
Definition flags.h:81
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
Definition flags.h:72
constexpr flag_t var_flags()
Definition flags.h:205
static constexpr flag_t GETOPT_FLAG_GROUP_FIVE
Definition flags.h:76
static constexpr flag_t GETOPT_FLAG_GROUP_NONE
Definition flags.h:71
constexpr flag_t all_flags()
Definition flags.h:156
static constexpr flag_t GETOPT_FLAG_DEFAULT_OPTION
Definition flags.h:55
static constexpr flag_t GETOPT_FLAG_HAS_DEFAULT
Definition flags.h:56
constexpr flag_t any_flags()
Definition flags.h:132
static constexpr flag_t GETOPT_FLAG_MULTIPLE
Definition flags.h:54
static constexpr flag_t GETOPT_FLAG_SHOW_MOST
Definition flags.h:60
constexpr flag_t combine_option_flags()
Definition flags.h:107
static constexpr flag_t GETOPT_FLAG_GROUP_THREE
Definition flags.h:74
static constexpr flag_t GETOPT_FLAG_REQUIRED
Definition flags.h:53
constexpr flag_t standalone_all_flags()
Definition flags.h:168
static constexpr flag_t GETOPT_FLAG_ENVIRONMENT_VARIABLE
Definition flags.h:47
static constexpr flag_t GETOPT_FLAG_SHOW_ALL
Definition flags.h:62
static constexpr flag_t GETOPT_FLAG_ALIAS
Definition flags.h:51

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.