2015-06-10 21:28:52 +10:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2013 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cmdopt.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2016-02-05 14:27:21 +11:00
|
|
|
#include <iostream>
|
2015-09-11 19:55:32 +10:00
|
|
|
#include <iomanip>
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
using util::cmdopt::option::base;
|
|
|
|
using util::cmdopt::option::bytes;
|
|
|
|
using util::cmdopt::option::count;
|
2015-06-30 22:04:54 +10:00
|
|
|
using util::cmdopt::option::null;
|
|
|
|
using util::cmdopt::option::present;
|
2015-06-10 21:28:52 +10:00
|
|
|
using util::cmdopt::option::value;
|
|
|
|
using util::cmdopt::parser;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-11 19:55:32 +10:00
|
|
|
base::base (std::string _name, std::string _description):
|
2015-06-30 22:04:54 +10:00
|
|
|
m_required (false),
|
2015-09-11 19:55:32 +10:00
|
|
|
m_seen (false),
|
|
|
|
m_name (std::move (_name)),
|
|
|
|
m_description (std::move (_description))
|
2015-06-10 21:28:52 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
base::~base ()
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
base::execute (void)
|
|
|
|
{
|
|
|
|
throw invalid_null (m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
base::execute (const char *restrict)
|
|
|
|
{
|
|
|
|
throw invalid_value (m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
base::start (void)
|
2015-06-30 22:04:54 +10:00
|
|
|
{
|
|
|
|
m_seen = false;
|
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
base::finish (void)
|
2015-06-30 22:04:54 +10:00
|
|
|
{
|
|
|
|
if (m_required && !m_seen)
|
|
|
|
throw invalid_required (m_name);
|
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-09-11 19:55:32 +10:00
|
|
|
const std::string&
|
2015-06-10 21:28:52 +10:00
|
|
|
base::name (void) const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-11 19:55:32 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const std::string&
|
|
|
|
base::description (void) const
|
|
|
|
{
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 22:04:54 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
base::required (void) const
|
|
|
|
{
|
|
|
|
return m_required;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
base::required (bool _required)
|
|
|
|
{
|
|
|
|
return m_required = _required;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
base::seen (void) const
|
|
|
|
{
|
|
|
|
return m_seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
base::seen (bool _seen)
|
|
|
|
{
|
|
|
|
return m_seen = _seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-11 19:55:32 +10:00
|
|
|
null::null (std::string _name, std::string _description):
|
|
|
|
base (std::move (_name), std::move (_description))
|
2015-06-30 22:06:10 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
null::execute (void)
|
|
|
|
{
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
null::execute (const char *restrict)
|
|
|
|
{
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 22:07:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-11 19:55:32 +10:00
|
|
|
present::present (std::string _name, std::string _description, bool &_data):
|
|
|
|
base (std::move (_name), std::move (_description)),
|
2015-06-30 22:07:02 +10:00
|
|
|
m_data (_data)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
present::execute (void)
|
|
|
|
{
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
present::finish (void)
|
|
|
|
{
|
|
|
|
m_data = seen ();
|
|
|
|
base::finish ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 22:08:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace util { namespace cmdopt { namespace option {
|
|
|
|
template <>
|
|
|
|
void
|
|
|
|
value<bool>::execute (const char *restrict str)
|
|
|
|
{
|
|
|
|
static const std::string TRUE_STRING[] = {
|
|
|
|
"true",
|
|
|
|
"yes",
|
|
|
|
"y",
|
|
|
|
"1"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (std::any_of (std::begin (TRUE_STRING),
|
|
|
|
std::end (TRUE_STRING),
|
|
|
|
[str] (auto i) { return i == str; }))
|
|
|
|
{
|
|
|
|
m_data = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string FALSE_STRING[] = {
|
|
|
|
"false",
|
|
|
|
"no",
|
|
|
|
"n",
|
|
|
|
"0"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (std::any_of (std::begin (FALSE_STRING),
|
|
|
|
std::end (FALSE_STRING),
|
|
|
|
[str] (auto i) { return i == str; }))
|
|
|
|
{
|
|
|
|
m_data = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::execute (str);
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
} } }
|
|
|
|
|
|
|
|
|
2015-06-30 22:06:10 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-06-10 21:28:52 +10:00
|
|
|
namespace util { namespace cmdopt { namespace option {
|
|
|
|
template class value<uint16_t>;
|
|
|
|
template class value<uint32_t>;
|
|
|
|
template class value<uint64_t>;
|
|
|
|
} } }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2015-09-11 19:55:32 +10:00
|
|
|
count<T>::count (std::string _name, std::string _description, T &_data):
|
|
|
|
value<T> (std::move (_name), std::move (_description), _data)
|
2015-06-15 17:47:18 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2015-06-10 21:28:52 +10:00
|
|
|
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);
|
2015-06-10 21:28:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
namespace util { namespace cmdopt { namespace option {
|
|
|
|
template class count<unsigned>;
|
|
|
|
} } }
|
|
|
|
|
|
|
|
|
2015-06-30 22:08:59 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static size_t
|
|
|
|
suffix_to_multiplier (char c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 6);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 5);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 4);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 3);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 2);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
2015-11-13 17:59:58 +11:00
|
|
|
return util::pow (1024UL, 1);
|
2015-06-30 22:08:59 +10:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw util::cmdopt::invalid_value ("bytes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-11 19:55:32 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-06-30 22:08:59 +10:00
|
|
|
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 (name ());
|
|
|
|
} else if (tail == last) {
|
|
|
|
data (val);
|
|
|
|
} else if (tail + 1 == last) {
|
|
|
|
data (val * suffix_to_multiplier (*tail));
|
|
|
|
} else
|
|
|
|
throw invalid_value (name ());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
2015-06-30 22:07:34 +10:00
|
|
|
parser::scan (int argc, const char *const *argv)
|
2015-06-10 21:28:52 +10:00
|
|
|
{
|
|
|
|
CHECK_GE (argc, 0);
|
|
|
|
CHECK (argv);
|
|
|
|
|
2015-06-30 22:04:54 +10:00
|
|
|
for (auto &j: m_options)
|
2015-09-11 19:55:32 +10:00
|
|
|
std::get<2> (j)->start ();
|
2015-06-30 22:04:54 +10:00
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
// start iterating after our program's name
|
|
|
|
int i = 1;
|
|
|
|
while (i < argc) {
|
|
|
|
auto len = strlen (argv[i]);
|
|
|
|
|
|
|
|
// bail if there's no potential for an option
|
|
|
|
if (len < 2 || argv[i][0] != '-')
|
|
|
|
return i;
|
|
|
|
|
|
|
|
// parse longopt
|
|
|
|
auto inc = argv[i][1] == '-'
|
|
|
|
? parse_long (i, argc, argv)
|
|
|
|
: parse_short (i, argc, argv);
|
2015-07-21 03:17:04 +10:00
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
CHECK_GT (inc, 0);
|
|
|
|
i += inc;
|
|
|
|
}
|
|
|
|
|
2015-06-30 22:04:54 +10:00
|
|
|
for (auto &j: m_options)
|
2015-09-11 19:55:32 +10:00
|
|
|
std::get<2> (j)->finish ();
|
2015-06-30 22:04:54 +10:00
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
2015-06-30 22:07:34 +10:00
|
|
|
parser::parse_long (int pos, int argc, const char *const *argv)
|
2015-06-10 21:28:52 +10:00
|
|
|
{
|
|
|
|
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 };
|
2015-09-11 19:55:32 +10:00
|
|
|
if (key == "help")
|
|
|
|
print_help (argc, argv);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
// find the handler
|
|
|
|
auto handle_pos = std::find_if (m_long.begin (),
|
|
|
|
m_long.end (),
|
|
|
|
[&] (auto i) { return std::get<0> (i) == key; });
|
|
|
|
if (handle_pos == m_long.end ())
|
|
|
|
throw invalid_key (key);
|
|
|
|
|
|
|
|
auto &handler = std::get<1> (*handle_pos);
|
|
|
|
|
|
|
|
// 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
|
2015-06-30 22:07:34 +10:00
|
|
|
parser::parse_short (int pos, int argc, const char *const *argv)
|
2015-06-10 21:28:52 +10:00
|
|
|
{
|
|
|
|
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];
|
2015-09-11 19:55:32 +10:00
|
|
|
if (letter == 'h')
|
|
|
|
print_help (argc, argv);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
auto hpos = std::find_if (m_short.begin (),
|
|
|
|
m_short.end (),
|
|
|
|
[letter] (auto j) { return std::get<0> (j) == letter; });
|
|
|
|
if (hpos == m_short.end ())
|
|
|
|
throw invalid_key (std::to_string (letter));
|
|
|
|
std::get<1> (*hpos).execute ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have a value following
|
|
|
|
auto letter = argv[pos][1];
|
|
|
|
auto hpos = std::find_if (m_short.begin (),
|
|
|
|
m_short.end (),
|
|
|
|
[letter] (auto i) { return std::get<0> (i) == letter; });
|
|
|
|
if (hpos == m_short.end ())
|
|
|
|
throw invalid_key (std::to_string (letter));
|
|
|
|
std::get<1> (*hpos).execute (argv[pos+1]);
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
2015-09-11 19:55:32 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
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 largest = std::max_element (m_long.begin (),
|
|
|
|
m_long.end (),
|
|
|
|
[] (const auto &a, const auto &b) {
|
|
|
|
return std::get<0> (a).size () < std::get<0> (b).size ();
|
|
|
|
});
|
|
|
|
int longwidth = std::get<0> (*largest).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 &o: m_options) {
|
|
|
|
std::cout << '\t'
|
|
|
|
<< '-' << std::get<0> (o) << '\t'
|
|
|
|
<< std::setw (longwidth) << std::get<1> (o) << '\t'
|
|
|
|
<< std::setw (0) << std::get<2> (o)->description ()
|
|
|
|
<< '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|