advgetopt 2.0.49
Parse complex command line arguments and configuration files in C++.
advgetopt.cpp
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
84// self
85//
86#include "advgetopt/advgetopt.h"
87
88#include "advgetopt/exception.h"
89
90
91// snapdev
92//
93#include <snapdev/join_strings.h>
94#include <snapdev/not_reached.h>
95
96
97// cppthread
98//
99#include <cppthread/log.h>
100
101
102// libutf8
103//
104#include <libutf8/iterator.h>
105
106
107// C
108//
109#include <string.h>
110
111
112// last include
113//
114#include <snapdev/poison.h>
115
116
117
118
119
125namespace advgetopt
126{
127
128
129namespace
130{
131
132
204{
206 Name("build-date")
210 , Help("print out the time and date when %p was built and exit.")
211 ),
213 Name("compiler-version")
217 , Help("print the version of the compiler used to compile the advgetopt library.")
218 ),
220 Name("configuration-filenames")
224 , Help("print out the list of configuration files checked out by this tool.")
225 ),
227 Name("copyright")
228 , ShortName('C')
231 , Help("print out the copyright of %p and exit.")
232 ),
234 Name("environment-variable-name")
238 , Help("print out the name of the environment variable supported by %p (if any.)")
239 ),
241 Name("filenames-of-option-definitions")
245 , Help("print out the full filename of the main option definitions and if any, the filenames of additional option definitions (often used by plugins).")
246 ),
248 Name("has-sanitizer")
252 , Help("print whether the advgetopt was compiled with the sanitizer extension.")
253 ),
255 Name("help")
256 , ShortName('h')
260 , Help("print out this help screen and exit.")
261 ),
263 Name("license")
264 , ShortName('L')
267 , Help("print out the license of %p and exit.")
268 ),
270 Name("path-to-option-definitions")
274 , Help("print out the path to the option definitions.")
275 ),
277 Name("print-option")
282 , Help("print the value of the named option after parsing all the options from the command line, environment variables, and configuration files.")
283 ),
285 Name("show-option-sources")
289 , Help("parse all the options and then print out the source of each value and each override.")
290 ),
292 Name("version")
293 , ShortName('V')
296 , Help("print out the version of %p and exit.")
297 ),
299};
300
301
316{
318 Name("config-dir")
324 , Help("add one or more configuration directory paths to search for configuration files.")
325 ),
327};
328
329
330
331
341bool is_arg(char const * a)
342{
343 // "-" and "--" are not options; however "--" returns true
344 // because after a "--" we take the data as default arguments
345 return a[0] == '-' && a[1] != '\0';
346}
347
348
349
360void check_for_show_sources(int argc, char * argv[])
361{
362 static bool found = false;
363
364 if(!found && argv != nullptr)
365 {
366 for(int idx(1); idx < argc; ++idx)
367 {
368 if(strcmp(argv[idx], "--show-option-sources") == 0)
369 {
370 found = true;
372 }
373 }
374 }
375}
376
377
378
379} // no name namespace
380
381
382
560{
561 initialize_parser(opt_env);
562}
563
564
607 , int argc
608 , char * argv[])
609{
610 // TODO: I do not think that this check_for_show_sources() is required
611 // because we also do it in finish_parsing()
612 //
613 check_for_show_sources(argc, argv);
614
615 initialize_parser(opt_env);
616 finish_parsing(argc, argv);
617}
618
619
633{
634 f_variables = std::make_shared<variables>();
635 f_options_environment = opt_env;
636
640 {
641 parse_options_info(g_system_options, true);
644 {
645 // add the "--config-dir <path> ..." option
646 //
647 parse_options_info(g_if_configuration_filename_system_options, true);
648 }
649 }
651
653}
654
655
674void getopt::finish_parsing(int argc, char * argv[])
675{
676 if(argv == nullptr)
677 {
678 throw getopt_logic_error("argv pointer cannot be nullptr");
679 }
680
681 check_for_show_sources(argc, argv);
682
683 parse_program_name(argv);
684 if(f_options_by_name.empty())
685 {
686 throw getopt_logic_error("an empty list of options is not legal, you must defined at least one (i.e. --version, --help...)");
687 }
688
689 link_aliases();
690
691 parse_configuration_files(argc, argv);
692 f_parsed = false;
694 f_parsed = false;
696
698 {
699 flag_t const result(process_system_options(std::cout));
700 if((result & SYSTEM_OPTION_COMMANDS_MASK) != 0)
701 {
702 throw getopt_exit("system command processed.", 0);
703 }
704 }
705
706 if(cppthread::log.get_errors() != 0)
707 {
708 throw getopt_exit("errors were found on your command line, environment variable, or configuration file.", 1);
709 }
710}
711
712
726{
727 if(!f_parsed
729 {
730 throw getopt_initialization(
731 "function called too soon, parser is not done yet"
732 " (i.e. is_defined(), get_string(), get_long(),"
733 " get_double() cannot be called until the parser is done)");
734 }
735}
736
737
750
751
764bool getopt::has_flag(flag_t flag) const
765{
766 return (f_options_environment.f_environment_flags & flag) != 0;
767}
768
769
778{
780
783 {
784 // no name
785 //
786 return;
787 }
788
790 if(s == nullptr)
791 {
792 // no environment variable with that name
793 //
794 return;
795 }
796
798}
799
800
827{
828 // first test the global environment variable
829 //
830 if(!f_environment_variable.empty())
831 {
835 , true);
836 }
837
838 // second check each option environment variable
839 //
840 for(auto const & opt : f_options_by_name)
841 {
842 std::string const name(opt.second->get_environment_variable_name());
843
844 // get the value, only set the option if the variable is set
845 //
846 std::string value;
847 if(opt.second->get_environment_variable_value(
848 value
850 {
852 opt.second
853 , value
854 , std::string()
855 , string_list_t()
857 }
858 }
859
860 f_parsed = true;
861}
862
863
887 std::string const & str
888 , option_source_t source
889 , bool only_environment_variable)
890{
892 if(args.empty())
893 {
894 // nothing extra to do
895 //
896 return;
897 }
898
899 // TODO: expand the arguments that include unquoted '*', '?', '[...]'
900 // (note that we remove the quotes at the moment so we'd have
901 // to keep track of that specific problem...)
902
903 // the argv array has to be a null terminated bare string pointers
904 //
905 std::vector<char *> sub_argv;
906
907 sub_argv.resize(args.size() + 2);
908
909 // argv[0] is the program name
910 //
911 sub_argv[0] = const_cast<char *>(f_program_fullname.c_str());
912
913 // the other arguments are from the variable
914 //
915 for(size_t idx(0); idx < args.size(); ++idx)
916 {
917 sub_argv[idx + 1] = const_cast<char *>(args[idx].c_str());
918 }
919
920 // this is probably already a nullptr
921 //
922 sub_argv[args.size() + 1] = nullptr;
923
924 // now convert those parameters in values
925 //
927 static_cast<int>(sub_argv.size() - 1)
928 , sub_argv.data()
929 , source
930 , only_environment_variable);
931}
932
933
949string_list_t getopt::split_environment(std::string const & environment)
950{
951 // this is exactly like the command line only in an environment variable
952 // so parse the parameters just like the shell
953 //
954 string_list_t args;
955 std::string a;
956 char const * s(environment.c_str());
957 while(*s != '\0')
958 {
959 if(isspace(*s))
960 {
961 if(!a.empty())
962 {
963 args.push_back(a);
964 a.clear();
965 }
966 do
967 {
968 ++s;
969 }
970 while(isspace(*s));
971 }
972 else if(*s == '"'
973 || *s == '\'')
974 {
975 // support quotations and remove them from the argument
976 //
977 char const quote(*s++);
978 while(*s != '\0'
979 && *s != quote)
980 {
981 a += *s++;
982 }
983 if(*s != '\0')
984 {
985 ++s;
986 }
987 }
988 else
989 {
990 a += *s++;
991 }
992 }
993
994 if(!a.empty())
995 {
996 args.push_back(a);
997 }
998
999 return args;
1000}
1001
1002
1051 int argc
1052 , char * argv[]
1053 , option_source_t source
1054 , bool only_environment_variable)
1055{
1056 for(int i(1); i < argc; ++i)
1057 {
1058 if(argv[i][0] == '-')
1059 {
1060 if(argv[i][1] == '-')
1061 {
1062 if(argv[i][2] == '\0')
1063 {
1064 // end of options, skip the '--' and then anything else
1065 // is taken as "filenames" (or whatever the tool expects)
1066 //
1067 if(f_default_option == nullptr)
1068 {
1069 cppthread::log << cppthread::log_level_t::error
1070 << "no default options defined; thus \"--\" is not accepted by this program."
1071 << cppthread::end;
1072 break;
1073 }
1074
1075 if(only_environment_variable)
1076 {
1078 {
1079 cppthread::log << cppthread::log_level_t::error
1080 << "option \"--\" is not supported in the environment variable."
1081 << cppthread::end;
1082 break;
1083 }
1084 }
1085 else
1086 {
1088 {
1089 cppthread::log << cppthread::log_level_t::error
1090 << "option \"--\" is not supported on the command line."
1091 << cppthread::end;
1092 break;
1093 }
1094 }
1095
1096 // in this case we do NOT test whether an argument uses
1097 // a dash (-) we take them all as default options
1098 //
1099 while(i + 1 < argc)
1100 {
1101 ++i;
1102 f_default_option->add_value(argv[i], string_list_t(), source);
1103 }
1104 }
1105 else
1106 {
1107 // a long option, check that it is defined in the
1108 // programmer defined options
1109 //
1110 std::string option_name(argv[i] + 2);
1111 std::string option_value;
1112 std::string::size_type pos(option_name.find('='));
1113 if(pos != std::string::npos)
1114 {
1115 if(pos == 0)
1116 {
1117 cppthread::log << cppthread::log_level_t::error
1118 << "name missing in \""
1119 << argv[i]
1120 << "\"."
1121 << cppthread::end;
1122 break;
1123 }
1124
1125 option_value = option_name.substr(pos + 1);
1126 option_name.resize(pos);
1127 }
1128 string_list_t option_keys;
1129 option_info::pointer_t opt(get_option(option_name));
1130 if(opt == nullptr)
1131 {
1132 pos = option_name.find_first_of("[:");
1133 if(pos != std::string::npos)
1134 {
1135 option_keys = parse_option_map(option_name.substr(pos));
1136 option_name.resize(pos);
1137 opt = get_option(option_name);
1138 }
1139 if(opt == nullptr)
1140 {
1141 cppthread::log << cppthread::log_level_t::error
1142 << "option \"--"
1143 << option_name
1144 << "\" is not supported."
1145 << cppthread::end;
1146 break;
1147 }
1148 if(!opt->has_flag(GETOPT_FLAG_ARRAY))
1149 {
1150 cppthread::log << cppthread::log_level_t::error
1151 << "option \"--"
1152 << option_name
1153 << "\" does not support the array or map syntax."
1154 << cppthread::end;
1155 break;
1156 }
1157 if(option_keys.empty())
1158 {
1159 cppthread::log << cppthread::log_level_t::error
1160 << "option \"--"
1161 << option_name
1162 << "\" has an invalid list of array keys."
1163 << cppthread::end;
1164 break;
1165 }
1166 }
1167 if(only_environment_variable)
1168 {
1169 if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1170 {
1171 cppthread::log << cppthread::log_level_t::error
1172 << "option \"--"
1173 << option_name
1174 << "\" is not supported in the environment variable."
1175 << cppthread::end;
1176 break;
1177 }
1178 }
1179 else
1180 {
1181 if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1182 {
1183 cppthread::log << cppthread::log_level_t::error
1184 << "option \"--"
1185 << option_name
1186 << "\" is not supported on the command line."
1187 << cppthread::end;
1188 break;
1189 }
1190 }
1191 if(pos != std::string::npos)
1192 {
1193 // the user specified a value after an equal sign
1194 //
1196 opt
1197 , option_value
1198 , std::string()
1199 , option_keys
1200 , source);
1201 }
1202 else
1203 {
1204 add_options(opt, i, argc, argv, option_keys, source);
1205 }
1206 }
1207 }
1208 else
1209 {
1210 if(argv[i][1] == '\0')
1211 {
1212 // stdin/stdout (a '-' by itself)
1213 //
1214 if(f_default_option == nullptr)
1215 {
1216 cppthread::log << cppthread::log_level_t::error
1217 << "no default options defined; thus \"-\" is not accepted by this program."
1218 << cppthread::end;
1219 break;
1220 }
1221 if(only_environment_variable)
1222 {
1224 {
1225 cppthread::log << cppthread::log_level_t::error
1226 << "option \"-\" is not supported in the environment variable."
1227 << cppthread::end;
1228 break;
1229 }
1230 }
1231 else
1232 {
1234 {
1235 cppthread::log << cppthread::log_level_t::error
1236 << "option \"-\" is not supported on the command line."
1237 << cppthread::end;
1238 break;
1239 }
1240 }
1241
1242 // this is similar to a default option by itself
1243 //
1244 f_default_option->add_value(argv[i], string_list_t(), source);
1245 }
1246 else
1247 {
1248 // short option(s)
1249 //
1250 // i gets incremented by add_options() so we have to
1251 // keep a copy in `k`
1252 //
1253 std::string const short_args_string(argv[i] + 1);
1254 for(libutf8::utf8_iterator short_args(short_args_string)
1255 ; short_args != short_args_string.end()
1256 ; ++short_args)
1257 {
1258 option_info::pointer_t opt(get_option(*short_args));
1259 if(opt == nullptr)
1260 {
1261 cppthread::log << cppthread::log_level_t::error
1262 << "option \"-"
1263 << short_name_to_string(*short_args)
1264 << "\" is not supported."
1265 << cppthread::end;
1266 break;
1267 }
1268 if(only_environment_variable)
1269 {
1270 if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1271 {
1272 cppthread::log << cppthread::log_level_t::error
1273 << "option \"-"
1274 << short_name_to_string(*short_args)
1275 << "\" is not supported in the environment variable."
1276 << cppthread::end;
1277 break;
1278 }
1279 }
1280 else
1281 {
1282 if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1283 {
1284 cppthread::log << cppthread::log_level_t::error
1285 << "option \"-"
1286 << short_name_to_string(*short_args)
1287 << "\" is not supported on the command line."
1288 << cppthread::end;
1289 break;
1290 }
1291 }
1292 add_options(opt, i, argc, argv, string_list_t(), source);
1293 }
1294 }
1295 }
1296 }
1297 else
1298 {
1299 // direct entry (filename or whatever the tool expects as a default)
1300 //
1301 if(f_default_option == nullptr)
1302 {
1303 cppthread::log << cppthread::log_level_t::error
1304 << "no default options defined; we do not know what to do of \""
1305 << argv[i]
1306 << "\"; standalone parameters are not accepted by this program."
1307 << cppthread::end;
1308 break;
1309 }
1310 if(only_environment_variable)
1311 {
1313 {
1314 cppthread::log << cppthread::log_level_t::error
1315 << "default options are not supported in the environment variable."
1316 << cppthread::end;
1317 break;
1318 }
1319 }
1320 else
1321 {
1323 {
1324 cppthread::log << cppthread::log_level_t::error
1325 << "default options are not supported on the command line."
1326 << cppthread::end;
1327 break;
1328 }
1329 }
1330 f_default_option->add_value(argv[i], string_list_t(), source);
1331 }
1332 }
1333
1334 f_parsed = true;
1335}
1336
1337
1399string_list_t getopt::parse_option_map(std::string const & raw_key)
1400{
1401 string_list_t keys;
1402 if(raw_key.empty())
1403 {
1404 return keys;
1405 }
1406
1407 if(raw_key[0] == '[')
1408 {
1409 // the array syntax allows for keys to be written between square
1410 // brackets instead of separated by colon so [a][b][c] is equivalent
1411 // to a:b:c
1412 //
1413 std::string result;
1414 std::string::size_type pos(1);
1415 for(;;)
1416 {
1417 std::string::size_type start(pos);
1418 pos = raw_key.find(']', pos);
1419 if(pos == std::string::npos)
1420 {
1421 // this is an error...
1422 //
1423 return string_list_t();
1424 }
1425 std::string key(raw_key.substr(start, pos - start));
1426 if(!key.empty())
1427 {
1428 keys.push_back(key);
1429 }
1430 ++pos;
1431 if(pos >= raw_key.length())
1432 {
1433 break;
1434 }
1435 if(raw_key[pos] != '[')
1436 {
1437 // the next key must start with a '['
1438 //
1439 return string_list_t();
1440 }
1441 ++pos;
1442 }
1443 }
1444 else
1445 {
1446 split_string(raw_key, keys, {":"});
1447 }
1448
1449 // remove duplicates also sort the keys (which is not a perfect thing
1450 // to do, but it shouldn't hurt except that numbers will not be sorted
1451 // properly, i.e. "10" appears before "2").
1452 //
1453 std::sort(keys.begin(), keys.end());
1454 keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
1455
1456 return keys;
1457}
1458
1459
1473{
1474 if(opt != nullptr
1475 && opt->has_flag(GETOPT_FLAG_ALIAS))
1476 {
1477 opt = opt->get_alias_destination();
1478 if(opt == nullptr)
1479 {
1480 throw getopt_undefined("getopt::get_alias_destination(): alias is missing. Did you call link_aliases()?");
1481 }
1482 }
1483
1484 return opt;
1485}
1486
1487
1502
1503
1532option_info::pointer_t getopt::get_option(std::string const & name, bool exact_option) const
1533{
1535
1536 if(name.empty())
1537 {
1538 // see \note section in doxy above about this
1539 //
1540 throw getopt_invalid_parameter("get_option() `name` argument cannot be empty.");
1541 }
1542
1543 std::string const n(option_with_dashes(name));
1544
1545 // we need this special case when looking for the default option
1546 // because the name may not be "--" in the option table
1547 // (i.e. you could call your default option "filenames" for example.)
1548 //
1549 if(n.length() == 2
1550 && n[0] == '-'
1551 && n[1] == '-')
1552 {
1553 opt = f_default_option;
1554 }
1555 else
1556 {
1557 short_name_t short_name(string_to_short_name(n));
1558 if(short_name != NO_SHORT_NAME)
1559 {
1560 auto it(f_options_by_short_name.find(short_name));
1561 if(it != f_options_by_short_name.end())
1562 {
1563 opt = it->second;
1564 }
1565 }
1566 else
1567 {
1568 auto it(f_options_by_name.find(n));
1569 if(it != f_options_by_name.end())
1570 {
1571 opt = it->second;
1572 }
1573 }
1574 }
1575
1576 return exact_option
1577 ? opt
1578 : get_alias_destination(opt);
1579}
1580
1581
1598option_info::pointer_t getopt::get_option(short_name_t short_name, bool exact_option) const
1599{
1600 auto it(f_options_by_short_name.find(short_name));
1601 if(it == f_options_by_short_name.end())
1602 {
1603 return option_info::pointer_t();
1604 }
1605
1606 return exact_option
1607 ? it->second
1608 : get_alias_destination(it->second);
1609}
1610
1611
1634 , int & i
1635 , int argc
1636 , char ** argv
1637 , string_list_t const & option_keys
1638 , option_source_t source)
1639{
1640 if(opt->has_flag(GETOPT_FLAG_FLAG))
1641 {
1642 opt->add_value(opt->get_default(), option_keys, source);
1643 }
1644 else
1645 {
1646 if(i + 1 < argc && !is_arg(argv[i + 1]))
1647 {
1648 if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1649 {
1650 do
1651 {
1652 ++i;
1653 opt->add_value(argv[i], option_keys, source);
1654 }
1655 while(i + 1 < argc && !is_arg(argv[i + 1]));
1656 }
1657 else
1658 {
1659 ++i;
1660 opt->add_value(argv[i], option_keys, source);
1661 }
1662 }
1663 else
1664 {
1665 if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1666 {
1667 cppthread::log << cppthread::log_level_t::error
1668 << "option --"
1669 << opt->get_name()
1670 << " expects an argument."
1671 << cppthread::end;
1672 }
1673 else
1674 {
1675 // We need to set something because the value is being
1676 // set although no argument was specified (but that's
1677 // legal by this argument's definition)
1678 //
1679 opt->add_value(std::string(), option_keys, source);
1680 }
1681 }
1682 }
1683}
1684
1685
1707 , std::string const & value
1708 , std::string const & filename
1709 , string_list_t const & option_keys
1710 , option_source_t source)
1711{
1712 // is the value defined?
1713 //
1714 if(!value.empty())
1715 {
1716 if(!opt->has_flag(GETOPT_FLAG_FLAG))
1717 {
1718 // does the option support multiple entries?
1719 //
1720 if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1721 {
1722 opt->set_multiple_values(value, option_keys, source);
1723 }
1724 else
1725 {
1726 opt->set_value(0, value, option_keys, source);
1727 }
1728 return;
1729 }
1730
1731 // for flags, allow "true" or "false" that way we can reset those
1732 // flags and especially, it's possible to use them in configuration
1733 // files that way
1734 //
1735 if(is_false(value))
1736 {
1737 opt->reset();
1738 return;
1739 }
1740
1741 if(is_true(value))
1742 {
1743 // for this, pass an empty string to the next layer
1744 //
1745 opt->set_value(0, std::string(), option_keys, source);
1746 return;
1747 }
1748
1749#pragma GCC diagnostic push
1750#pragma GCC diagnostic ignored "-Wrestrict"
1751 cppthread::log << cppthread::log_level_t::error
1752 << "option "
1753 << (filename.empty()
1754 ? "--" + opt->get_name()
1755 : "\"" + option_with_underscores(opt->get_name()) + "\"")
1756 << " cannot be given value \""
1757 << value
1758 << "\""
1759 << (filename.empty()
1760 ? std::string()
1761 : " in configuration file \""
1762 + filename
1763 + "\"")
1764 << ". It only accepts \"true\" or \"false\"."
1765 << cppthread::end;
1766#pragma GCC diagnostic pop
1767 return;
1768 }
1769
1770 // does the option require a value when used?
1771 //
1772 if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1773 {
1774#pragma GCC diagnostic push
1775#pragma GCC diagnostic ignored "-Wrestrict"
1776 cppthread::log << cppthread::log_level_t::error
1777 << "option "
1778 << (filename.empty()
1779 ? "--" + opt->get_name()
1780 : "\"" + option_with_underscores(opt->get_name()) + "\"")
1781 << " must be given a value"
1782 << (filename.empty()
1783 ? std::string()
1784 : " in configuration file \""
1785 + filename
1786 + "\"")
1787 << "."
1788 << cppthread::end;
1789#pragma GCC diagnostic pop
1790 return;
1791 }
1792
1793 // accept an empty value otherwise
1794 //
1795 opt->set_value(0, value, option_keys, source);
1796}
1797
1798
1814
1815
1816
1817} // namespace advgetopt
1818// vim: ts=4 sw=4 et
Definitions of the advanced getopt class.
options_environment f_options_environment
Definition advgetopt.h:225
option_info::pointer_t get_alias_destination(option_info::pointer_t opt) const
Return the alias if there is one.
void initialize_parser(options_environment const &opt_env)
Initialize the parser.
void add_option_from_string(option_info::pointer_t opt, std::string const &value, std::string const &filename, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Add an option with a value defined in a string.
void parse_configuration_files(int argc=0, char *argv[]=nullptr)
This function checks for arguments in configuration files.
void add_options(option_info::pointer_t opt, int &i, int argc, char **argv, string_list_t const &option_keys, option_source_t source=option_source_t::SOURCE_DIRECT)
Read parameters of the current option.
option_info::map_by_name_t f_options_by_name
Definition advgetopt.h:226
void parse_string(std::string const &str, option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse a string similar to a command line argument.
option_info::pointer_t get_option(std::string const &name, bool exact_option=false) const
Retrieve an option by name.
void parse_environment_variable()
Check for an environment variable.
option_info::map_by_short_name_t f_options_by_short_name
Definition advgetopt.h:227
void define_environment_variable_data()
Retrieve the environment variable string.
std::string f_program_fullname
Definition advgetopt.h:222
variables::pointer_t get_variables() const
Retrieve a pointer to the variables defined in the getopt object.
void parse_arguments(int argc, char *argv[], option_source_t source=option_source_t::SOURCE_DIRECT, bool only_environment_variable=false)
Parse an array of arguments.
void parse_options_info(option const *opts, bool ignore_duplicates=false)
Parse the options to option_info objects.
void parse_options_from_file()
Check for a file with option definitions.
void parse_program_name(char *argv[])
Transform the argv[0] parameter in the program name.
getopt(options_environment const &opts)
Initialize the getopt object.
std::string f_environment_variable
Definition advgetopt.h:229
void is_parsed() const
Verify that the parser is done.
options_environment const & get_options_environment() const
Return a reference to the options environment.
flag_t process_system_options(std::basic_ostream< char > &out)
Process the system options.
bool has_flag(flag_t flag) const
Check whether an environment flag is set or not.
void link_aliases()
Link options marked as a GETOPT_FLAG_ALIAS.
static string_list_t split_environment(std::string const &environment)
Transform a string in an array of arguments.
option_info::map_by_name_t const & get_options() const
Retrieve the complete list of options.
void finish_parsing(int argc, char *argv[])
Actually parse everything.
void parse_options_from_group_names()
Transform group names in –<name>-help commands.
variables::pointer_t f_variables
Definition advgetopt.h:230
option_info::pointer_t f_default_option
Definition advgetopt.h:228
static string_list_t parse_option_map(std::string const &raw_key)
Parse a map following an option name.
std::map< std::string, pointer_t > map_by_name_t
Definition option_info.h:80
std::shared_ptr< option_info > pointer_t
Definition option_info.h:78
static void set_trace_sources(bool trace)
Whether the sources should be traced.
std::shared_ptr< variables > pointer_t
Definition variables.h:61
Definitions of the advanced getopt exceptions.
option const g_if_configuration_filename_system_options[]
Optional list of options.
option const g_system_options[]
Definitions of the system options.
void check_for_show_sources(int argc, char *argv[])
Check for a "--show-option-sources" flag.
bool is_arg(char const *a)
Check whether this parameter is an argument.
The advgetopt environment to parse command line options.
Definition version.h:37
void split_string(std::string const &str, string_list_t &result, string_list_t const &separators)
Split a string in sub-strings separated by separators.
Definition utils.cpp:347
static constexpr flag_t GETOPT_FLAG_ARRAY
Definition flags.h:57
static constexpr flag_t GETOPT_FLAG_GROUP_OPTIONS
Definition flags.h:72
bool is_false(std::string s)
Check whether a value represents "false".
Definition utils.cpp:737
static constexpr flag_t GETOPT_FLAG_SHOW_USAGE_ON_ERROR
Definition flags.h:60
constexpr option define_option(ARGS ...args)
Definition options.h:259
static constexpr flag_t GETOPT_FLAG_SHOW_SYSTEM
Definition flags.h:64
short_name_t string_to_short_name(std::string const &name)
Transform a string to a short name.
constexpr flag_t command_flags()
Definition flags.h:191
static constexpr flag_t GETOPT_FLAG_COMMAND_LINE
Definition flags.h:45
constexpr flag_t standalone_command_flags()
Definition flags.h:179
constexpr option end_options()
Definition options.h:294
constexpr short_name_t NO_SHORT_NAME
Definition option_info.h:51
std::uint32_t flag_t
Definition flags.h:41
char32_t short_name_t
Definition option_info.h:49
bool is_true(std::string s)
Check whether a value represents "true".
Definition utils.cpp:716
std::string option_with_underscores(std::string const &s)
Converts an option back to using underscores.
Definition utils.cpp:288
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_PROCESS_SYSTEM_PARAMETERS
Definition options.h:435
static constexpr flag_t GETOPT_FLAG_FLAG
Definition flags.h:51
static constexpr flag_t GETOPT_FLAG_GROUP_COMMANDS
Definition flags.h:71
std::string short_name_to_string(short_name_t short_name)
Convert a short name to a UTF-8 string.
std::string quote(std::string const &s, char open, char close)
The converse of unquote.
Definition utils.cpp:201
constexpr flag_t SYSTEM_OPTION_COMMANDS_MASK
Definition advgetopt.h:71
constexpr flag_t any_flags()
Definition flags.h:130
static constexpr flag_t GETOPT_FLAG_MULTIPLE
Definition flags.h:53
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS
Definition options.h:434
std::string option_with_dashes(std::string const &s)
Convert the _ found in a string to - instead.
Definition utils.cpp:259
static constexpr flag_t GETOPT_FLAG_REQUIRED
Definition flags.h:52
constexpr flag_t GETOPT_ENVIRONMENT_FLAG_AUTO_DONE
Definition options.h:437
static constexpr flag_t GETOPT_FLAG_ENVIRONMENT_VARIABLE
Definition flags.h:46
std::vector< std::string > string_list_t
Definition utils.h:41
static constexpr flag_t GETOPT_FLAG_ALIAS
Definition flags.h:50
Structure representing an option.
Definition options.h:70
char const * f_environment_variable_intro
Definition options.h:447
char const * f_configuration_filename
Definition options.h:450
char const * f_environment_variable_name
Definition options.h:446

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.