libcruft-util/cruft/util/cmdopt.cpp

535 lines
12 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2013-2016 Danny Robson <danny@nerdcruft.net>
*/
#include "cmdopt.hpp"
#include "cast.hpp"
#include "debug/assert.hpp"
#include <cstring>
#include <iostream>
#include <iomanip>
using namespace cruft::cmdopt;
using namespace cruft::cmdopt::option;
///////////////////////////////////////////////////////////////////////////////
base::base ()
: m_required ([] () { return false; })
{ ; }
//-----------------------------------------------------------------------------
base::~base ()
{ ; }
//-----------------------------------------------------------------------------
void
base::execute (void)
{
throw invalid_null ();
}
//-----------------------------------------------------------------------------
void
base::execute (const char *restrict value)
{
throw invalid_value (value);
}
//-----------------------------------------------------------------------------
void
base::start (void)
2015-06-30 22:04:54 +10:00
{
m_seen = false;
}
//-----------------------------------------------------------------------------
void
base::finish (void)
2015-06-30 22:04:54 +10:00
{
if (!fulfilled ())
throw invalid_required ();
}
//-----------------------------------------------------------------------------
void
base::required (std::function<bool(void)> _required)
{
m_required = std::move (_required);
}
2015-06-30 22:04:54 +10:00
//-----------------------------------------------------------------------------
bool
base::required (void) const
{
return m_required ();
2015-06-30 22:04:54 +10:00
}
//-----------------------------------------------------------------------------
void
2015-06-30 22:04:54 +10:00
base::required (bool _required)
{
m_required = [_required] (void) { return _required; };
2015-06-30 22:04:54 +10:00
}
//-----------------------------------------------------------------------------
bool
base::seen (void) const
{
return m_seen;
}
//-----------------------------------------------------------------------------
bool
base::seen (bool _seen)
{
return m_seen = _seen;
}
//-----------------------------------------------------------------------------
bool
base::fulfilled (void) const
{
if (!m_required ())
return true;
return seen ();
}
///////////////////////////////////////////////////////////////////////////////
2015-06-30 22:06:10 +10:00
void
null::execute (void)
{
seen (true);
}
//-----------------------------------------------------------------------------
void
null::execute (const char *restrict)
{
seen (true);
}
//-----------------------------------------------------------------------------
const std::string&
null::example (void) const
{
static const std::string EXAMPLE;
return EXAMPLE;
}
2015-06-30 22:07:02 +10:00
///////////////////////////////////////////////////////////////////////////////
present::present (bool &_data):
2015-06-30 22:07:02 +10:00
m_data (_data)
{ ; }
//-----------------------------------------------------------------------------
void
present::execute (void)
{
seen (true);
}
//-----------------------------------------------------------------------------
const std::string&
present::example (void) const
{
static const std::string EXAMPLE;
return EXAMPLE;
}
2015-06-30 22:07:02 +10:00
//-----------------------------------------------------------------------------
void
present::finish (void)
{
m_data = seen ();
base::finish ();
}
2015-06-30 22:06:10 +10:00
//-----------------------------------------------------------------------------
namespace cruft::cmdopt::option {
template class value<uint16_t>;
template class value<uint32_t>;
template class value<uint64_t>;
2017-01-05 15:06:49 +11:00
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
count<T>::count (T &_data):
value<T> (_data)
2015-06-15 17:47:18 +10:00
{ ; }
//-------------------------------------------------------------------------
template <typename T>
void
count<T>::execute (void)
{
2015-06-15 17:47:18 +10:00
++this->data ();
2015-06-30 22:04:54 +10:00
this->seen (true);
}
//-----------------------------------------------------------------------------
namespace cruft::cmdopt::option {
template class count<unsigned>;
2017-01-05 15:06:49 +11:00
}
///////////////////////////////////////////////////////////////////////////////
static size_t
suffix_to_multiplier (char c)
{
switch (c) {
case 'e':
case 'E':
return cruft::pow (1024UL, 6u);
case 'p':
case 'P':
return cruft::pow (1024UL, 5u);
case 't':
case 'T':
return cruft::pow (1024UL, 4u);
case 'g':
case 'G':
return cruft::pow (1024UL, 3u);
case 'm':
case 'M':
return cruft::pow (1024UL, 2u);
case 'k':
case 'K':
return cruft::pow (1024UL, 1u);
default:
const char str[2] = { c, '\0' };
throw std::invalid_argument (str);
}
}
//-----------------------------------------------------------------------------
void
bytes::execute (const char *restrict str)
{
const char *tail;
const char *last = str + strlen (str);
unsigned long val = std::strtoul (const_cast<char *> (str), const_cast<char**> (&tail), 10);
CHECK_LE (tail, last);
if (tail == str) {
throw invalid_value (str);
} else if (tail == last) {
data (val);
} else if (tail + 1 == last) {
try {
data (val * suffix_to_multiplier (*tail));
} catch (const std::invalid_argument&)
{ throw invalid_value (str); }
} else
throw invalid_value (str);
}
///////////////////////////////////////////////////////////////////////////////
int
parser::scan (int argc, char const *const *argv)
{
CHECK_GT (argc, 0);
CHECK (argv);
2015-06-30 22:04:54 +10:00
for (auto &j: m_options)
j.handler->start ();
2015-06-30 22:04:54 +10:00
// start iterating after our program's name
int i = 1;
while (i < argc) {
auto arg = argv[i];
auto len = strlen (arg);
// bail if there's no potential for an option
if (len < 2 || arg[0] != '-')
break;
// stop processing named options on '--'
if (len == 2 && arg[1] == '-') {
++i;
break;
}
// parse longopt
auto inc = arg[1] == '-'
? parse_long (i, argc, argv)
: parse_short (i, argc, argv);
2015-07-21 03:17:04 +10:00
CHECK_GT (inc, 0);
i += inc;
}
// process the positional arguments
for (size_t cursor = 0; i < argc && cursor < m_positional.size (); ++i, ++cursor)
m_positional[cursor].get ().execute (argv[i]);
// ensure we've processed all the arguments
if (i != argc)
throw unhandled_argument (i);
// allow arguments to check if they've been successfully handled
2015-06-30 22:04:54 +10:00
for (auto &j: m_options)
j.handler->finish ();
2015-06-30 22:04:54 +10:00
return i;
}
//-----------------------------------------------------------------------------
int
parser::parse_long (int pos, int argc, const char *const *argv)
{
CHECK_LT (pos, argc);
CHECK_GE (pos, 0);
CHECK_GE (argc, 0);
CHECK (argv);
CHECK_EQ (argv[pos][0], '-');
CHECK_EQ (argv[pos][1], '-');
// break first atom into components and extract the key
const char *start = argv[pos] + 2;
const char *eq = strchr (start, '=');
const char *last = start + strlen (start);
std::string key { start, eq ? eq : last };
if (key == "help")
print_help (argc, argv);
auto &handler = m_long.at (key).get ();
// maybe grab a value from the next atom and dispatch
if (!eq) {
// check the next atom for the value
if (pos + 1 < argc)
if (argv[pos + 1][0] != '-') {
handler.execute (argv[pos+1]);
return 2;
}
handler.execute ();
} else {
handler.execute (eq+1);
}
return 1;
}
//-----------------------------------------------------------------------------
int
parser::parse_short (int pos, int argc, const char *const *argv)
{
CHECK_LT (pos, argc);
CHECK_GE (pos, 0);
CHECK_GE (argc, 0);
CHECK (argv);
CHECK_EQ (argv[pos][0], '-');
CHECK_NEQ (argv[pos][1], '-');
// we have a run of no-value keys
auto len = strlen (argv[pos]);
if (len > 2 || pos + 1 == argc || argv[pos+1][0] == '-') {
for (size_t i = 1; i < len; ++i) {
auto letter = argv[pos][i];
if (letter == 'h')
print_help (argc, argv);
m_short.at(letter).get ().execute ();
}
return 1;
}
// we have a value following
auto letter = argv[pos][1];
m_short.at (letter).get ().execute (argv[pos + 1]);
return 2;
}
///////////////////////////////////////////////////////////////////////////////
void
parser::print_help (const int argc,
const char *const *argv) const
{
(void)argc;
CHECK_EQ (m_short.size (), m_options.size ());
CHECK_EQ (m_long.size (), m_options.size ());
if (m_options.empty ())
exit (0);
// find the longest long form argument so we can set field alignment
auto largestwidth = std::max_element (
m_long.begin (),
m_long.end (),
[] (const auto &a, const auto &b)
{
auto const &[a_key,a_obj] = a;
auto const &[b_key,b_obj] = b;
return a_key.size () < b_key.size ();
});
auto longwidth = largestwidth->first.size ();
// find the longest example text
auto largestexample = std::max_element (
m_options.cbegin (),
m_options.cend (),
[] (const auto &a, const auto &b)
{
return a.handler->example ().size () > b.handler->example ().size ();
});
auto longexample = largestexample->handler->example ().size ();
// field width requires an alignment. we don't care about preserving
// state as we're about to bail anyway
std::cout << std::left;
// print all the option info
std::cout << "usage: " << argv[0] << '\n';
for (auto &opt: m_options) {
auto s = std::find_if (
std::cbegin (m_short),
std::cend (m_short),
[&] (auto j) { return &j.second.get () == opt.handler.get (); }
);
auto l = std::find_if (
std::cbegin (m_long),
std::cend (m_long),
[&] (auto j) { return &j.second.get () == opt.handler.get (); }
);
std::cout << '\t';
if (s != std::cend (m_short))
std::cout << '-' << s->first << '\t';
else
std::cout << '\t';
std::cout << std::setw (cruft::cast::lossless<int> (longwidth));
if (l != std::cend (m_long))
std::cout << l->first << '\t';
else
std::cout << ' ' << '\t';
std::cout << std::setw (cruft::cast::lossless<int> (longexample)) << opt.handler->example () << '\t'
<< std::setw (0) << opt.description
<< '\n';
}
exit (0);
}
///////////////////////////////////////////////////////////////////////////////
invalid_key::invalid_key (std::string _key):
m_key (std::move (_key))
{ ; }
//-----------------------------------------------------------------------------
const char*
invalid_key::what (void) const noexcept
{
return m_key.c_str ();
}
///////////////////////////////////////////////////////////////////////////////
invalid_value::invalid_value (std::string _value):
m_value (std::move (_value))
{ ; }
//-----------------------------------------------------------------------------
const char*
invalid_value::what (void) const noexcept
{
return m_value.c_str ();
}
///////////////////////////////////////////////////////////////////////////////
const char*
invalid_null::what (void) const noexcept
{
static const char WHAT[] = "unexpected null option was encountered";
return WHAT;
}
///////////////////////////////////////////////////////////////////////////////
const char*
invalid_required::what (void) const noexcept
{
static const char WHAT[] = "required option not seen";
return WHAT;
}
///////////////////////////////////////////////////////////////////////////////
unhandled_argument::unhandled_argument (int _index):
m_index (_index)
{ ; }
//-----------------------------------------------------------------------------
int
unhandled_argument::index (void) const noexcept
{
return m_index;
}
//-----------------------------------------------------------------------------
const char*
unhandled_argument::what (void) const noexcept
{
static const char WHAT[] = "unhandled argument";
return WHAT;
}