advgetopt 2.0.47
Parse complex command line arguments and configuration files in C++.
advgetopt.cpp
Go to the documentation of this file.
1// Copyright (c) 2006-2024 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
198{
200 Name("build-date")
204 , Help("print out the time and date when %p was built and exit.")
205 ),
207 Name("compiler-version")
211 , Help("print the version of the compiler used to compile the advgetopt library.")
212 ),
214 Name("configuration-filenames")
218 , Help("print out the list of configuration files checked out by this tool.")
219 ),
221 Name("copyright")
222 , ShortName('C')
225 , Help("print out the copyright of %p and exit.")
226 ),
228 Name("environment-variable-name")
232 , Help("print out the name of the environment variable supported by %p (if any.)")
233 ),
235 Name("has-sanitizer")
239 , Help("print whether the advgetopt was compiled with the sanitizer extension.")
240 ),
242 Name("help")
243 , ShortName('h')
247 , Help("print out this help screen and exit.")
248 ),
250 Name("license")
251 , ShortName('L')
254 , Help("print out the license of %p and exit.")
255 ),
257 Name("path-to-option-definitions")
261 , Help("print out the path to the option definitions.")
262 ),
264 Name("print-option")
269 , Help("print the value of the named option after parsing all the options from the command line, environment variables, and configuration files.")
270 ),
272 Name("show-option-sources")
276 , Help("parse all the options and then print out the source of each value and each override.")
277 ),
279 Name("version")
280 , ShortName('V')
283 , Help("print out the version of %p and exit.")
284 ),
286};
287
288
303{
305 Name("config-dir")
311 , Help("add one or more configuration directory paths to search for configuration files.")
312 ),
314};
315
316
317
318
328bool is_arg(char const * a)
329{
330 // "-" and "--" are not options; however "--" returns true
331 // because after a "--" we take the data as default arguments
332 return a[0] == '-' && a[1] != '\0';
333}
334
335
336
347void check_for_show_sources(int argc, char * argv[])
348{
349 static bool found = false;
350
351 if(!found && argv != nullptr)
352 {
353 for(int idx(1); idx < argc; ++idx)
354 {
355 if(strcmp(argv[idx], "--show-option-sources") == 0)
356 {
357 found = true;
359 }
360 }
361 }
362}
363
364
365
366} // no name namespace
367
368
369
547{
548 initialize_parser(opt_env);
549}
550
551
594 , int argc
595 , char * argv[])
596{
597 // TODO: I do not think that this check_for_show_sources() is required
598 // because we also do it in finish_parsing()
599 //
600 check_for_show_sources(argc, argv);
601
602 initialize_parser(opt_env);
603 finish_parsing(argc, argv);
604}
605
606
620{
621 f_variables = std::make_shared<variables>();
622 f_options_environment = opt_env;
623
627 {
628 parse_options_info(g_system_options, true);
631 {
632 // add the "--config-dir <path> ..." option
633 //
634 parse_options_info(g_if_configuration_filename_system_options, true);
635 }
636 }
638
640}
641
642
661void getopt::finish_parsing(int argc, char * argv[])
662{
663 if(argv == nullptr)
664 {
665 throw getopt_logic_error("argv pointer cannot be nullptr");
666 }
667
668 check_for_show_sources(argc, argv);
669
670 parse_program_name(argv);
671 if(f_options_by_name.empty())
672 {
673 throw getopt_logic_error("an empty list of options is not legal, you must defined at least one (i.e. --version, --help...)");
674 }
675
676 link_aliases();
677
678 parse_configuration_files(argc, argv);
679 f_parsed = false;
681 f_parsed = false;
683
685 {
686 flag_t const result(process_system_options(std::cout));
687 if((result & SYSTEM_OPTION_COMMANDS_MASK) != 0)
688 {
689 throw getopt_exit("system command processed.", 0);
690 }
691 }
692
693 if(cppthread::log.get_errors() != 0)
694 {
695 throw getopt_exit("errors were found on your command line, environment variable, or configuration file.", 1);
696 }
697}
698
699
713{
714 if(!f_parsed
716 {
717 throw getopt_initialization(
718 "function called too soon, parser is not done yet"
719 " (i.e. is_defined(), get_string(), get_long(),"
720 " get_double() cannot be called until the parser is done)");
721 }
722}
723
724
737
738
751bool getopt::has_flag(flag_t flag) const
752{
753 return (f_options_environment.f_environment_flags & flag) != 0;
754}
755
756
765{
767
770 {
771 // no name
772 //
773 return;
774 }
775
777 if(s == nullptr)
778 {
779 // no environment variable with that name
780 //
781 return;
782 }
783
785}
786
787
814{
815 // first test the global environment variable
816 //
817 if(!f_environment_variable.empty())
818 {
822 , true);
823 }
824
825 // second check each option environment variable
826 //
827 for(auto const & opt : f_options_by_name)
828 {
829 std::string const name(opt.second->get_environment_variable_name());
830
831 // get the value, only set the option if the variable is set
832 //
833 std::string value;
834 if(opt.second->get_environment_variable_value(
835 value
837 {
839 opt.second
840 , value
841 , std::string()
842 , string_list_t()
844 }
845 }
846
847 f_parsed = true;
848}
849
850
874 std::string const & str
875 , option_source_t source
876 , bool only_environment_variable)
877{
879 if(args.empty())
880 {
881 // nothing extra to do
882 //
883 return;
884 }
885
886 // TODO: expand the arguments that include unquoted '*', '?', '[...]'
887 // (note that we remove the quotes at the moment so we'd have
888 // to keep track of that specific problem...)
889
890 // the argv array has to be a null terminated bare string pointers
891 //
892 std::vector<char *> sub_argv;
893
894 sub_argv.resize(args.size() + 2);
895
896 // argv[0] is the program name
897 //
898 sub_argv[0] = const_cast<char *>(f_program_fullname.c_str());
899
900 // the other arguments are from the variable
901 //
902 for(size_t idx(0); idx < args.size(); ++idx)
903 {
904 sub_argv[idx + 1] = const_cast<char *>(args[idx].c_str());
905 }
906
907 // this is probably already a nullptr
908 //
909 sub_argv[args.size() + 1] = nullptr;
910
911 // now convert those parameters in values
912 //
914 static_cast<int>(sub_argv.size() - 1)
915 , sub_argv.data()
916 , source
917 , only_environment_variable);
918}
919
920
936string_list_t getopt::split_environment(std::string const & environment)
937{
938 // this is exactly like the command line only in an environment variable
939 // so parse the parameters just like the shell
940 //
941 string_list_t args;
942 std::string a;
943 char const * s(environment.c_str());
944 while(*s != '\0')
945 {
946 if(isspace(*s))
947 {
948 if(!a.empty())
949 {
950 args.push_back(a);
951 a.clear();
952 }
953 do
954 {
955 ++s;
956 }
957 while(isspace(*s));
958 }
959 else if(*s == '"'
960 || *s == '\'')
961 {
962 // support quotations and remove them from the argument
963 //
964 char const quote(*s++);
965 while(*s != '\0'
966 && *s != quote)
967 {
968 a += *s++;
969 }
970 if(*s != '\0')
971 {
972 ++s;
973 }
974 }
975 else
976 {
977 a += *s++;
978 }
979 }
980
981 if(!a.empty())
982 {
983 args.push_back(a);
984 }
985
986 return args;
987}
988
989
1038 int argc
1039 , char * argv[]
1040 , option_source_t source
1041 , bool only_environment_variable)
1042{
1043 for(int i(1); i < argc; ++i)
1044 {
1045 if(argv[i][0] == '-')
1046 {
1047 if(argv[i][1] == '-')
1048 {
1049 if(argv[i][2] == '\0')
1050 {
1051 // end of options, skip the '--' and then anything else
1052 // is taken as "filenames" (or whatever the tool expects)
1053 //
1054 if(f_default_option == nullptr)
1055 {
1056 cppthread::log << cppthread::log_level_t::error
1057 << "no default options defined; thus \"--\" is not accepted by this program."
1058 << cppthread::end;
1059 break;
1060 }
1061
1062 if(only_environment_variable)
1063 {
1065 {
1066 cppthread::log << cppthread::log_level_t::error
1067 << "option \"--\" is not supported in the environment variable."
1068 << cppthread::end;
1069 break;
1070 }
1071 }
1072 else
1073 {
1075 {
1076 cppthread::log << cppthread::log_level_t::error
1077 << "option \"--\" is not supported on the command line."
1078 << cppthread::end;
1079 break;
1080 }
1081 }
1082
1083 // in this case we do NOT test whether an argument uses
1084 // a dash (-) we take them all as default options
1085 //
1086 while(i + 1 < argc)
1087 {
1088 ++i;
1089 f_default_option->add_value(argv[i], string_list_t(), source);
1090 }
1091 }
1092 else
1093 {
1094 // a long option, check that it is defined in the
1095 // programmer defined options
1096 //
1097 std::string option_name(argv[i] + 2);
1098 std::string option_value;
1099 std::string::size_type pos(option_name.find('='));
1100 if(pos != std::string::npos)
1101 {
1102 if(pos == 0)
1103 {
1104 cppthread::log << cppthread::log_level_t::error
1105 << "name missing in \""
1106 << argv[i]
1107 << "\"."
1108 << cppthread::end;
1109 break;
1110 }
1111
1112 option_value = option_name.substr(pos + 1);
1113 option_name.resize(pos);
1114 }
1115 string_list_t option_keys;
1116 option_info::pointer_t opt(get_option(option_name));
1117 if(opt == nullptr)
1118 {
1119 pos = option_name.find_first_of("[:");
1120 if(pos != std::string::npos)
1121 {
1122 option_keys = parse_option_map(option_name.substr(pos));
1123 option_name.resize(pos);
1124 opt = get_option(option_name);
1125 }
1126 if(opt == nullptr)
1127 {
1128 cppthread::log << cppthread::log_level_t::error
1129 << "option \"--"
1130 << option_name
1131 << "\" is not supported."
1132 << cppthread::end;
1133 break;
1134 }
1135 if(!opt->has_flag(GETOPT_FLAG_ARRAY))
1136 {
1137 cppthread::log << cppthread::log_level_t::error
1138 << "option \"--"
1139 << option_name
1140 << "\" does not support the array or map syntax."
1141 << cppthread::end;
1142 break;
1143 }
1144 if(option_keys.empty())
1145 {
1146 cppthread::log << cppthread::log_level_t::error
1147 << "option \"--"
1148 << option_name
1149 << "\" has an invalid list of array keys."
1150 << cppthread::end;
1151 break;
1152 }
1153 }
1154 if(only_environment_variable)
1155 {
1156 if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1157 {
1158 cppthread::log << cppthread::log_level_t::error
1159 << "option \"--"
1160 << option_name
1161 << "\" is not supported in the environment variable."
1162 << cppthread::end;
1163 break;
1164 }
1165 }
1166 else
1167 {
1168 if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1169 {
1170 cppthread::log << cppthread::log_level_t::error
1171 << "option \"--"
1172 << option_name
1173 << "\" is not supported on the command line."
1174 << cppthread::end;
1175 break;
1176 }
1177 }
1178 if(pos != std::string::npos)
1179 {
1180 // the user specified a value after an equal sign
1181 //
1183 opt
1184 , option_value
1185 , std::string()
1186 , option_keys
1187 , source);
1188 }
1189 else
1190 {
1191 add_options(opt, i, argc, argv, option_keys, source);
1192 }
1193 }
1194 }
1195 else
1196 {
1197 if(argv[i][1] == '\0')
1198 {
1199 // stdin/stdout (a '-' by itself)
1200 //
1201 if(f_default_option == nullptr)
1202 {
1203 cppthread::log << cppthread::log_level_t::error
1204 << "no default options defined; thus \"-\" is not accepted by this program."
1205 << cppthread::end;
1206 break;
1207 }
1208 if(only_environment_variable)
1209 {
1211 {
1212 cppthread::log << cppthread::log_level_t::error
1213 << "option \"-\" is not supported in the environment variable."
1214 << cppthread::end;
1215 break;
1216 }
1217 }
1218 else
1219 {
1221 {
1222 cppthread::log << cppthread::log_level_t::error
1223 << "option \"-\" is not supported on the command line."
1224 << cppthread::end;
1225 break;
1226 }
1227 }
1228
1229 // this is similar to a default option by itself
1230 //
1231 f_default_option->add_value(argv[i], string_list_t(), source);
1232 }
1233 else
1234 {
1235 // short option(s)
1236 //
1237 // i gets incremented by add_options() so we have to
1238 // keep a copy in `k`
1239 //
1240 std::string const short_args_string(argv[i] + 1);
1241 for(libutf8::utf8_iterator short_args(short_args_string)
1242 ; short_args != short_args_string.end()
1243 ; ++short_args)
1244 {
1245 option_info::pointer_t opt(get_option(*short_args));
1246 if(opt == nullptr)
1247 {
1248 cppthread::log << cppthread::log_level_t::error
1249 << "option \"-"
1250 << short_name_to_string(*short_args)
1251 << "\" is not supported."
1252 << cppthread::end;
1253 break;
1254 }
1255 if(only_environment_variable)
1256 {
1257 if(!opt->has_flag(GETOPT_FLAG_ENVIRONMENT_VARIABLE))
1258 {
1259 cppthread::log << cppthread::log_level_t::error
1260 << "option \"-"
1261 << short_name_to_string(*short_args)
1262 << "\" is not supported in the environment variable."
1263 << cppthread::end;
1264 break;
1265 }
1266 }
1267 else
1268 {
1269 if(!opt->has_flag(GETOPT_FLAG_COMMAND_LINE))
1270 {
1271 cppthread::log << cppthread::log_level_t::error
1272 << "option \"-"
1273 << short_name_to_string(*short_args)
1274 << "\" is not supported on the command line."
1275 << cppthread::end;
1276 break;
1277 }
1278 }
1279 add_options(opt, i, argc, argv, string_list_t(), source);
1280 }
1281 }
1282 }
1283 }
1284 else
1285 {
1286 // direct entry (filename or whatever the tool expects as a default)
1287 //
1288 if(f_default_option == nullptr)
1289 {
1290 cppthread::log << cppthread::log_level_t::error
1291 << "no default options defined; we do not know what to do of \""
1292 << argv[i]
1293 << "\"; standalone parameters are not accepted by this program."
1294 << cppthread::end;
1295 break;
1296 }
1297 if(only_environment_variable)
1298 {
1300 {
1301 cppthread::log << cppthread::log_level_t::error
1302 << "default options are not supported in the environment variable."
1303 << cppthread::end;
1304 break;
1305 }
1306 }
1307 else
1308 {
1310 {
1311 cppthread::log << cppthread::log_level_t::error
1312 << "default options are not supported on the command line."
1313 << cppthread::end;
1314 break;
1315 }
1316 }
1317 f_default_option->add_value(argv[i], string_list_t(), source);
1318 }
1319 }
1320
1321 f_parsed = true;
1322}
1323
1324
1386string_list_t getopt::parse_option_map(std::string const & raw_key)
1387{
1388 string_list_t keys;
1389 if(raw_key.empty())
1390 {
1391 return keys;
1392 }
1393
1394 if(raw_key[0] == '[')
1395 {
1396 // the array syntax allows for keys to be written between square
1397 // brackets instead of separated by colon so [a][b][c] is equivalent
1398 // to a:b:c
1399 //
1400 std::string result;
1401 std::string::size_type pos(1);
1402 for(;;)
1403 {
1404 std::string::size_type start(pos);
1405 pos = raw_key.find(']', pos);
1406 if(pos == std::string::npos)
1407 {
1408 // this is an error...
1409 //
1410 return string_list_t();
1411 }
1412 std::string key(raw_key.substr(start, pos - start));
1413 if(!key.empty())
1414 {
1415 keys.push_back(key);
1416 }
1417 ++pos;
1418 if(pos >= raw_key.length())
1419 {
1420 break;
1421 }
1422 if(raw_key[pos] != '[')
1423 {
1424 // the next key must start with a '['
1425 //
1426 return string_list_t();
1427 }
1428 ++pos;
1429 }
1430 }
1431 else
1432 {
1433 split_string(raw_key, keys, {":"});
1434 }
1435
1436 // remove duplicates also sort the keys (which is not a perfect thing
1437 // to do, but it shouldn't hurt except that numbers will not be sorted
1438 // properly, i.e. "10" appears before "2").
1439 //
1440 std::sort(keys.begin(), keys.end());
1441 keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
1442
1443 return keys;
1444}
1445
1446
1460{
1461 if(opt != nullptr
1462 && opt->has_flag(GETOPT_FLAG_ALIAS))
1463 {
1464 opt = opt->get_alias_destination();
1465 if(opt == nullptr)
1466 {
1467 throw getopt_undefined("getopt::get_alias_destination(): alias is missing. Did you call link_aliases()?");
1468 }
1469 }
1470
1471 return opt;
1472}
1473
1474
1489
1490
1519option_info::pointer_t getopt::get_option(std::string const & name, bool exact_option) const
1520{
1522
1523 if(name.empty())
1524 {
1525 // see \note section in doxy above about this
1526 //
1527 throw getopt_invalid_parameter("get_option() `name` argument cannot be empty.");
1528 }
1529
1530 std::string const n(option_with_dashes(name));
1531
1532 // we need this special case when looking for the default option
1533 // because the name may not be "--" in the option table
1534 // (i.e. you could call your default option "filenames" for example.)
1535 //
1536 if(n.length() == 2
1537 && n[0] == '-'
1538 && n[1] == '-')
1539 {
1540 opt = f_default_option;
1541 }
1542 else
1543 {
1544 short_name_t short_name(string_to_short_name(n));
1545 if(short_name != NO_SHORT_NAME)
1546 {
1547 auto it(f_options_by_short_name.find(short_name));
1548 if(it != f_options_by_short_name.end())
1549 {
1550 opt = it->second;
1551 }
1552 }
1553 else
1554 {
1555 auto it(f_options_by_name.find(n));
1556 if(it != f_options_by_name.end())
1557 {
1558 opt = it->second;
1559 }
1560 }
1561 }
1562
1563 return exact_option
1564 ? opt
1565 : get_alias_destination(opt);
1566}
1567
1568
1585option_info::pointer_t getopt::get_option(short_name_t short_name, bool exact_option) const
1586{
1587 auto it(f_options_by_short_name.find(short_name));
1588 if(it == f_options_by_short_name.end())
1589 {
1590 return option_info::pointer_t();
1591 }
1592
1593 return exact_option
1594 ? it->second
1595 : get_alias_destination(it->second);
1596}
1597
1598
1621 , int & i
1622 , int argc
1623 , char ** argv
1624 , string_list_t const & option_keys
1625 , option_source_t source)
1626{
1627 if(opt->has_flag(GETOPT_FLAG_FLAG))
1628 {
1629 opt->add_value(opt->get_default(), option_keys, source);
1630 }
1631 else
1632 {
1633 if(i + 1 < argc && !is_arg(argv[i + 1]))
1634 {
1635 if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1636 {
1637 do
1638 {
1639 ++i;
1640 opt->add_value(argv[i], option_keys, source);
1641 }
1642 while(i + 1 < argc && !is_arg(argv[i + 1]));
1643 }
1644 else
1645 {
1646 ++i;
1647 opt->add_value(argv[i], option_keys, source);
1648 }
1649 }
1650 else
1651 {
1652 if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1653 {
1654 cppthread::log << cppthread::log_level_t::error
1655 << "option --"
1656 << opt->get_name()
1657 << " expects an argument."
1658 << cppthread::end;
1659 }
1660 else
1661 {
1662 // We need to set something because the value is being
1663 // set although no argument was specified (but that's
1664 // legal by this argument's definition)
1665 //
1666 opt->add_value(std::string(), option_keys, source);
1667 }
1668 }
1669 }
1670}
1671
1672
1694 , std::string const & value
1695 , std::string const & filename
1696 , string_list_t const & option_keys
1697 , option_source_t source)
1698{
1699 // is the value defined?
1700 //
1701 if(!value.empty())
1702 {
1703 if(!opt->has_flag(GETOPT_FLAG_FLAG))
1704 {
1705 // does the option support multiple entries?
1706 //
1707 if(opt->has_flag(GETOPT_FLAG_MULTIPLE))
1708 {
1709 opt->set_multiple_values(value, option_keys, source);
1710 }
1711 else
1712 {
1713 opt->set_value(0, value, option_keys, source);
1714 }
1715 return;
1716 }
1717
1718 // for flags, allow "true" or "false" that way we can reset those
1719 // flags and especially, it's possible to use them in configuration
1720 // files that way
1721 //
1722 if(is_false(value))
1723 {
1724 opt->reset();
1725 return;
1726 }
1727
1728 if(is_true(value))
1729 {
1730 // for this, pass an empty string to the next layer
1731 //
1732 opt->set_value(0, std::string(), option_keys, source);
1733 return;
1734 }
1735
1736#pragma GCC diagnostic push
1737#pragma GCC diagnostic ignored "-Wrestrict"
1738 cppthread::log << cppthread::log_level_t::error
1739 << "option "
1740 << (filename.empty()
1741 ? "--" + opt->get_name()
1742 : "\"" + option_with_underscores(opt->get_name()) + "\"")
1743 << " cannot be given value \""
1744 << value
1745 << "\""
1746 << (filename.empty()
1747 ? std::string()
1748 : " in configuration file \""
1749 + filename
1750 + "\"")
1751 << ". It only accepts \"true\" or \"false\"."
1752 << cppthread::end;
1753#pragma GCC diagnostic pop
1754 return;
1755 }
1756
1757 // does the option require a value when used?
1758 //
1759 if(opt->has_flag(GETOPT_FLAG_REQUIRED))
1760 {
1761#pragma GCC diagnostic push
1762#pragma GCC diagnostic ignored "-Wrestrict"
1763 cppthread::log << cppthread::log_level_t::error
1764 << "option "
1765 << (filename.empty()
1766 ? "--" + opt->get_name()
1767 : "\"" + option_with_underscores(opt->get_name()) + "\"")
1768 << " must be given a value"
1769 << (filename.empty()
1770 ? std::string()
1771 : " in configuration file \""
1772 + filename
1773 + "\"")
1774 << "."
1775 << cppthread::end;
1776#pragma GCC diagnostic pop
1777 return;
1778 }
1779
1780 // accept an empty value otherwise
1781 //
1782 opt->set_value(0, value, option_keys, source);
1783}
1784
1785
1801
1802
1803
1804} // namespace advgetopt
1805// vim: ts=4 sw=4 et
Definitions of the advanced getopt class.
options_environment f_options_environment
Definition advgetopt.h:224
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:225
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:226
void define_environment_variable_data()
Retrieve the environment variable string.
std::string f_program_fullname
Definition advgetopt.h:221
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:228
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:229
option_info::pointer_t f_default_option
Definition advgetopt.h:227
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.
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:730
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:709
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.