2022-03-16 14:20:26 +11:00
|
|
|
#include "./parser.hpp"
|
|
|
|
|
2022-03-18 13:26:10 +11:00
|
|
|
#include "./args.hpp"
|
2022-03-18 12:38:30 +11:00
|
|
|
|
2022-03-18 14:03:56 +11:00
|
|
|
#include <cruft/util/container.hpp>
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
using cruft::cmdopt2::parser;
|
|
|
|
|
|
|
|
|
2022-03-21 11:50:39 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
parser::parser (char const *_description)
|
|
|
|
: m_description (_description)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-03-18 16:23:17 +11:00
|
|
|
cruft::cmdopt2::positional_t&
|
|
|
|
parser::add (positional_t const &arg)&
|
2022-03-16 14:20:26 +11:00
|
|
|
{
|
|
|
|
return m_positional.emplace_back (std::move (arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-03-18 16:23:17 +11:00
|
|
|
cruft::cmdopt2::keyword_t&
|
|
|
|
parser::add (keyword_t const &arg)&
|
2022-03-16 14:20:26 +11:00
|
|
|
{
|
|
|
|
return m_keyword.emplace_back (std::move (arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
2022-03-18 16:04:45 +11:00
|
|
|
parser::parse_named (int argc, const char *const *argv, int *found) const
|
2022-03-16 14:20:26 +11:00
|
|
|
{
|
|
|
|
auto cursor = argv[0];
|
|
|
|
if (!cursor or !*cursor)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (*cursor++ != '-')
|
|
|
|
return 0;
|
|
|
|
if (!*cursor)
|
|
|
|
throw std::runtime_error ("missing argument name");
|
|
|
|
|
|
|
|
if (*cursor == '-') {
|
|
|
|
++cursor;
|
|
|
|
if (!*cursor)
|
|
|
|
throw std::runtime_error ("missing long argument name");
|
2022-03-18 16:04:45 +11:00
|
|
|
return parse_long (argc, argv, found);
|
2022-03-16 14:20:26 +11:00
|
|
|
} else {
|
2022-03-18 16:04:45 +11:00
|
|
|
return parse_short (argc, argv, found);
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
2022-03-18 16:04:45 +11:00
|
|
|
parser::parse_long (int argc, const char *const *argv, int *found) const
|
2022-03-16 14:20:26 +11:00
|
|
|
{
|
|
|
|
CHECK (argc >= 1);
|
|
|
|
CHECK (strlen (argv[0]) >= 2);
|
|
|
|
CHECK (argv[0][0] == '-');
|
|
|
|
CHECK (argv[0][1] == '-');
|
|
|
|
|
|
|
|
auto const eq = strchr (argv[0], '=');
|
|
|
|
std::string_view const key (argv[0] + 2, eq ?: strlen (argv[0]) + argv[0]);
|
|
|
|
|
|
|
|
auto const pos = std::find_if (
|
|
|
|
m_keyword.begin (),
|
|
|
|
m_keyword.end (),
|
|
|
|
[&] (auto const &arg)
|
|
|
|
{
|
|
|
|
return arg.long_ and *arg.long_ == key;
|
|
|
|
});
|
|
|
|
if (pos == m_keyword.end ())
|
|
|
|
throw std::runtime_error ("Unknown long argument");
|
|
|
|
|
2022-03-18 16:04:45 +11:00
|
|
|
found[std::distance (m_keyword.begin (), pos)] = true;
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
if (eq) {
|
|
|
|
(*pos->acceptor1) (eq + 1);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (pos->acceptor0) {
|
|
|
|
CHECK (!pos->acceptor1);
|
|
|
|
(*pos->acceptor0) ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK (pos->acceptor1);
|
|
|
|
if (argc < 2)
|
|
|
|
throw std::runtime_error ("Missing long arg value");
|
|
|
|
|
|
|
|
if (not argv[1] or not argv[1][0] or argv[1][0] == '-')
|
|
|
|
throw std::runtime_error ("Missing long arg value");
|
|
|
|
|
|
|
|
(*pos->acceptor1) (argv[1]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
2022-03-18 16:04:45 +11:00
|
|
|
parser::parse_short (int argc, const char *const *argv, int *found) const
|
2022-03-16 14:20:26 +11:00
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
CHECK (argc >= 1);
|
|
|
|
CHECK (strlen (argv[0]) >= 2);
|
|
|
|
CHECK (argv[0][0] == '-');
|
|
|
|
CHECK (argv[0][1] != '-');
|
|
|
|
|
|
|
|
auto const len = strlen (argv[0]);
|
|
|
|
if (len < 1)
|
|
|
|
throw std::runtime_error ("Missing short arguments");
|
|
|
|
|
|
|
|
// Handle single short args with a potential for an associated value
|
|
|
|
if (len == 2) {
|
|
|
|
auto const pos = std::find_if (
|
|
|
|
m_keyword.begin (),
|
|
|
|
m_keyword.end (),
|
|
|
|
[&] (auto const &arg)
|
|
|
|
{
|
|
|
|
return arg.short_ and *arg.short_ == argv[0][1];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (pos == m_keyword.end ())
|
|
|
|
throw std::runtime_error ("Unknown short argument");
|
|
|
|
|
2022-03-18 16:04:45 +11:00
|
|
|
found[std::distance (m_keyword.begin (), pos)] = true;
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
if (pos->acceptor0) {
|
|
|
|
CHECK (!pos->acceptor1);
|
|
|
|
(*pos->acceptor0) ();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:04:45 +11:00
|
|
|
if (pos->acceptor1) {
|
|
|
|
CHECK (!pos->acceptor0);
|
|
|
|
if (argc < 2)
|
|
|
|
throw std::runtime_error ("Missing short argument value");
|
|
|
|
(*pos->acceptor1) (argv[1]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle strings of short arguments, eg: "-vvv"
|
|
|
|
for (std::size_t i = 1; i < len; ++i) {
|
|
|
|
auto const pos = std::find_if (
|
|
|
|
m_keyword.begin (),
|
|
|
|
m_keyword.end (),
|
|
|
|
[&] (auto const &arg)
|
|
|
|
{
|
|
|
|
return arg.short_ and *arg.short_ == argv[0][1];
|
|
|
|
});
|
|
|
|
if (pos == m_keyword.end ())
|
|
|
|
throw std::runtime_error ("Unknown short argument");
|
2022-03-18 16:04:45 +11:00
|
|
|
found[std::distance (m_keyword.begin (), pos)] = true;
|
2022-03-16 14:20:26 +11:00
|
|
|
|
|
|
|
if (pos->acceptor1)
|
|
|
|
throw std::runtime_error ("Missing short argument value");
|
|
|
|
|
2022-03-18 16:04:45 +11:00
|
|
|
if (pos->acceptor0)
|
|
|
|
(*pos->acceptor0) ();
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
parser::parse (int const argc, const char *const *argv)
|
|
|
|
{
|
2022-03-18 16:04:45 +11:00
|
|
|
std::vector<int> found_positional (m_positional.size (), 0);
|
|
|
|
std::vector<int> found_keyword (m_keyword.size (), 0);
|
2022-03-16 14:20:26 +11:00
|
|
|
|
2022-03-18 14:03:56 +11:00
|
|
|
using namespace std::string_view_literals;
|
|
|
|
if (contains ("--help"sv, cruft::view (argv, argc))) {
|
|
|
|
usage (argc, argv, stdout);
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
int arg_cursor = 1;
|
|
|
|
int pos_cursor = 0;
|
|
|
|
|
|
|
|
while (arg_cursor != argc) {
|
|
|
|
auto const arg = argv[arg_cursor];
|
|
|
|
if (!arg or !*arg)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*arg == '-') {
|
2022-03-18 16:04:45 +11:00
|
|
|
arg_cursor += parse_named (argc - arg_cursor, argv + arg_cursor, found_keyword.data ());
|
2022-03-16 14:20:26 +11:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:04:45 +11:00
|
|
|
found_positional[pos_cursor] = true;
|
|
|
|
auto &pos_arg = m_positional[pos_cursor];
|
|
|
|
if (pos_arg.acceptor1)
|
2022-03-21 11:51:19 +11:00
|
|
|
(*pos_arg.acceptor1) (argv[arg_cursor]);
|
|
|
|
arg_cursor++;
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:51:40 +11:00
|
|
|
for (int i = 0, last = int (m_positional.size ()); i < last; ++i)
|
2022-03-18 16:04:45 +11:00
|
|
|
if (m_positional[i].required () and !found_positional[i])
|
|
|
|
throw std::runtime_error (
|
|
|
|
fmt::format ("Missing required position argument {}", m_positional[i].name)
|
|
|
|
);
|
|
|
|
|
2022-03-21 11:51:40 +11:00
|
|
|
for (int i = 0, last = int (m_keyword.size ()); i < last; ++i)
|
2022-03-18 16:04:45 +11:00
|
|
|
if (m_keyword[i].required () and !found_keyword[i])
|
|
|
|
throw std::runtime_error (
|
|
|
|
fmt::format ("Missing required named argument {}", m_keyword[i].name)
|
|
|
|
);
|
|
|
|
|
2022-03-16 14:20:26 +11:00
|
|
|
return arg_cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-03-21 11:49:51 +11:00
|
|
|
void
|
|
|
|
parser::usage (int argc, char const * const* argv, FILE *fp) const
|
|
|
|
{
|
2022-03-21 11:50:39 +11:00
|
|
|
fmt::print (fp, FMT_STRING("{}\nUsage:"), m_description);
|
2022-03-16 14:20:26 +11:00
|
|
|
if (argc > 0)
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, " {}", argv[0]);
|
2022-03-16 14:20:26 +11:00
|
|
|
|
|
|
|
static char constexpr OPTIONAL[2] { '[', ']' };
|
|
|
|
static char constexpr REQUIRED[2] { '<', '>' };
|
|
|
|
|
2022-03-21 11:49:51 +11:00
|
|
|
for (auto const &arg: m_keyword) {
|
2022-03-18 16:04:45 +11:00
|
|
|
auto const &delimiters = arg.required () ? REQUIRED : OPTIONAL;
|
2022-03-16 14:20:26 +11:00
|
|
|
|
2022-03-18 14:04:09 +11:00
|
|
|
if (arg.short_) {
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, FMT_STRING (" {}-{}"), delimiters[0], *arg.short_);
|
2022-03-18 14:04:09 +11:00
|
|
|
if (arg.acceptor1)
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, FMT_STRING (" <{}>"), arg.name);
|
|
|
|
fmt::print (fp, FMT_STRING ("{}"), delimiters[1]);
|
2022-03-18 14:04:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg.long_) {
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, FMT_STRING (" {}--{}"), delimiters[0], *arg.long_);
|
2022-03-18 14:04:09 +11:00
|
|
|
if (arg.acceptor1)
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, FMT_STRING (" <{}>"), arg.name);
|
|
|
|
fmt::print (fp, FMT_STRING ("{}"), delimiters[1]);
|
2022-03-18 14:04:09 +11:00
|
|
|
}
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:49:51 +11:00
|
|
|
for (auto const &arg: m_positional) {
|
2022-03-18 16:04:45 +11:00
|
|
|
auto const &delimiters = arg.required () ? REQUIRED : OPTIONAL;
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, FMT_STRING (" {}{}{}"), delimiters[0], arg.name, delimiters[1]);
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:49:51 +11:00
|
|
|
fmt::print (fp, "\n");
|
2022-03-16 14:20:26 +11:00
|
|
|
}
|