2013-02-26 18:31:14 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +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
|
2013-02-26 18:31:14 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-02-26 18:31:14 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
2013-02-26 18:31:14 +11:00
|
|
|
*
|
|
|
|
* Copyright 2013 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "options.hpp"
|
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
#include "debug.hpp"
|
2013-08-05 16:39:31 +10:00
|
|
|
#include "string.hpp"
|
2013-02-27 15:10:17 +11:00
|
|
|
#include "types.hpp"
|
|
|
|
#include "types/casts.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2013-02-26 18:31:14 +11:00
|
|
|
#include <cassert>
|
2013-02-26 18:56:25 +11:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
2013-02-26 18:31:14 +11:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2013-02-26 18:56:25 +11:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-04-29 17:12:34 +10:00
|
|
|
using util::option;
|
|
|
|
using util::nulloption;
|
|
|
|
using util::presentoption;
|
|
|
|
using util::valueoption;
|
|
|
|
using util::bytesoption;
|
|
|
|
using util::processor;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Generic option operations, failure or default modes
|
2013-02-26 18:31:14 +11:00
|
|
|
option::option (char _letter,
|
|
|
|
const char *_name,
|
|
|
|
const char *_desc,
|
|
|
|
bool _required):
|
|
|
|
m_shortopt (_letter),
|
|
|
|
m_longopt (_name),
|
|
|
|
m_description(_desc),
|
|
|
|
m_required (_required),
|
|
|
|
m_found (false)
|
|
|
|
{ reset(); }
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:56:25 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
option::execute (void)
|
|
|
|
{
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error(
|
2013-02-26 18:31:14 +11:00
|
|
|
"Cannot provide no value for the option '" + m_longopt + "'"
|
2014-08-18 22:14:31 +10:00
|
|
|
);
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
2013-02-26 18:56:25 +11:00
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:56:25 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
option::execute (const std::string& data)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
assert(data.size() > 0);
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error(
|
2013-02-26 18:31:14 +11:00
|
|
|
"Cannot provide a value for the option '" + m_longopt + "'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-04-29 17:12:34 +10:00
|
|
|
std::ostream&
|
2015-04-29 17:18:31 +10:00
|
|
|
operator<< (std::ostream & os, const option& opt)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
os << (opt.is_required () ? " -" : "[-" ) << opt.shortopt ()
|
|
|
|
<< (opt.is_required () ? " \t" : "]\t") << opt.longopt ()
|
|
|
|
<< "\t" << opt.description ();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:31:14 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
option::finish (void)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
if (m_required && !m_found)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error ("Required argument not found: " + m_longopt);
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-02-26 18:31:14 +11:00
|
|
|
nulloption::nulloption (char _letter,
|
|
|
|
const char *_name,
|
|
|
|
const char *_desc,
|
|
|
|
bool _required):
|
|
|
|
option (_letter, _name, _desc, _required)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-02-26 18:31:14 +11:00
|
|
|
presentoption::presentoption (char _letter,
|
|
|
|
const char *_name,
|
|
|
|
const char *_desc,
|
|
|
|
bool *_data,
|
|
|
|
bool _required):
|
|
|
|
option (_letter, _name, _desc, _required),
|
|
|
|
m_data (_data)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:31:14 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
presentoption::execute (void)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
*m_data = true;
|
|
|
|
m_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-08-18 22:09:57 +10:00
|
|
|
namespace util {
|
|
|
|
template<>
|
|
|
|
bool&
|
2015-04-29 17:18:31 +10:00
|
|
|
valueoption<bool>::get_arg(const std::string& arg, bool *val)
|
|
|
|
{
|
|
|
|
if (arg == "true" || arg == "yes" || arg == "1")
|
|
|
|
return *val = true;
|
|
|
|
else if (arg == "false" || arg == "no" || arg == "0")
|
|
|
|
return *val = false;
|
2014-08-18 22:09:57 +10:00
|
|
|
else
|
|
|
|
throw std::domain_error("Invalid form for boolean argument");
|
|
|
|
}
|
2014-10-21 21:47:10 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T&
|
|
|
|
valueoption<T>::get_arg(const std::string &arg,
|
|
|
|
T *val)
|
|
|
|
{
|
|
|
|
std::istringstream stream (arg, std::istringstream::in);
|
|
|
|
stream.exceptions (
|
|
|
|
std::istringstream::failbit
|
|
|
|
| std::istringstream::badbit
|
|
|
|
);
|
|
|
|
|
|
|
|
stream >> *val;
|
|
|
|
return *val;
|
|
|
|
}
|
2014-08-18 22:09:57 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-21 21:47:10 +11:00
|
|
|
template std::string& util::valueoption<std::string>::get_arg (const std::string&, std::string*);
|
|
|
|
|
|
|
|
template int16_t& util::valueoption< int16_t>::get_arg (const std::string&, int16_t*);
|
|
|
|
template int32_t& util::valueoption< int32_t>::get_arg (const std::string&, int32_t*);
|
|
|
|
template int64_t& util::valueoption< int64_t>::get_arg (const std::string&, int64_t*);
|
|
|
|
|
|
|
|
template uint16_t& util::valueoption<uint16_t>::get_arg (const std::string&, uint16_t*);
|
|
|
|
template uint32_t& util::valueoption<uint32_t>::get_arg (const std::string&, uint32_t*);
|
|
|
|
template uint64_t& util::valueoption<uint64_t>::get_arg (const std::string&, uint64_t*);
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-02-26 18:31:14 +11:00
|
|
|
bytesoption::bytesoption (char _letter,
|
|
|
|
const char *_name,
|
|
|
|
const char *_desc,
|
|
|
|
size_t *_data,
|
|
|
|
bytestype _type,
|
|
|
|
bytesmodifier _modifier,
|
|
|
|
bool _required):
|
2013-02-27 15:11:05 +11:00
|
|
|
valueoption<size_t> (_letter, _name, _desc, _data, _required),
|
2013-02-26 18:31:14 +11:00
|
|
|
m_type (_type),
|
|
|
|
m_modifier (_modifier)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:31:14 +11:00
|
|
|
bytesoption::bytestype
|
2015-04-29 17:18:31 +10:00
|
|
|
bytesoption::type_from_character (char c)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
return BYTES_EXA;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
return BYTES_PETA;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
return BYTES_TERA;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
return BYTES_GIGA;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
return BYTES_MEGA;
|
|
|
|
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
return BYTES_KILO;
|
|
|
|
|
2013-02-27 15:11:05 +11:00
|
|
|
default:
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::domain_error("Invalid magnitude specifier");
|
2013-02-27 15:11:05 +11:00
|
|
|
}
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:31:14 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
bytesoption::execute (const std::string& data)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
// We shouldn't get this far into processing with a zero sized argument,
|
|
|
|
// that's what the null data execute function is for.
|
|
|
|
assert (data.size () > 0);
|
|
|
|
|
|
|
|
size_t defaultvalue = *m_data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
bytesmodifier modifier = m_modifier;
|
2013-02-27 15:11:05 +11:00
|
|
|
size_t cursor = data.size () - 1;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Consume an optional trailing `byte' type
|
|
|
|
if (data[cursor] == 'B' || data[cursor] == 'b') {
|
2013-02-27 15:11:05 +11:00
|
|
|
if (cursor == 0)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::invalid_argument ("Size is too short");
|
2013-02-27 15:11:05 +11:00
|
|
|
--cursor;
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we explicitly request base2
|
|
|
|
if (data[cursor] == 'i') {
|
|
|
|
modifier = BYTES_BASE2;
|
2013-02-27 15:11:05 +11:00
|
|
|
if (cursor == 0)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::invalid_argument("Size is too short");
|
2013-02-27 15:11:05 +11:00
|
|
|
--cursor;
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine what constant factor is needed for the raw size
|
|
|
|
uint64_t multiplier = 1;
|
|
|
|
uint64_t modifier_factor;
|
|
|
|
switch (modifier) {
|
|
|
|
case BYTES_BASE2:
|
|
|
|
modifier_factor = 1024;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BYTES_BASE10:
|
|
|
|
modifier_factor = 1000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-02-27 15:11:05 +11:00
|
|
|
unreachable ();
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the difference in magnitude between what is desired, and what
|
|
|
|
// is specified by the user. Raise the multiplier by that magnitude.
|
|
|
|
bytestype specified = m_type;
|
|
|
|
try {
|
|
|
|
specified = type_from_character (data[cursor]);
|
2014-08-18 22:14:31 +10:00
|
|
|
// If the character is a digit, it just means the user skipped the
|
2013-02-26 18:31:14 +11:00
|
|
|
// size specifier, which is ok.
|
2015-04-29 17:12:34 +10:00
|
|
|
} catch (std::domain_error &x) {
|
2013-02-26 18:31:14 +11:00
|
|
|
if (!isdigit (data[cursor]))
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::invalid_argument ("Not a size");
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Falsely increment the cursor if there's no size specifier...
|
|
|
|
++cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... so that we can easily decrement the cursor without special logic
|
|
|
|
// after reading the specifiers.
|
|
|
|
--cursor;
|
|
|
|
|
2014-07-02 15:47:09 +10:00
|
|
|
multiplier = static_cast<uintmax_t> (std::pow (modifier_factor, (int)specified));
|
2013-02-26 18:31:14 +11:00
|
|
|
get_arg (data.substr(0, cursor + 1), m_data);
|
|
|
|
*m_data *= multiplier;
|
|
|
|
} catch (...) {
|
|
|
|
// Ensure that we haven't nuked a default value due to a half
|
|
|
|
// completed calculation followed by an exception.
|
|
|
|
|
|
|
|
*m_data = defaultvalue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_found = true;
|
|
|
|
}
|
|
|
|
|
2013-02-26 18:56:25 +11:00
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
|
|
|
/// Internal helper options. Print help and usage.
|
|
|
|
/// A callback to the processor which triggers output of the help message.
|
|
|
|
///
|
|
|
|
/// This should never be instanced by a user of the system. The processor will
|
|
|
|
/// automatically adds this to its available options where needed.
|
|
|
|
|
|
|
|
class helpoption : public option
|
|
|
|
{
|
2015-06-09 15:41:08 +10:00
|
|
|
protected:
|
|
|
|
static const char HELP_CHARACTER;
|
|
|
|
static const char *HELP_NAME;
|
|
|
|
static const char *HELP_DESCRIPTION;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2015-06-09 15:41:08 +10:00
|
|
|
processor * m_processor;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2015-06-09 15:41:08 +10:00
|
|
|
public:
|
|
|
|
helpoption (processor * _processor):
|
|
|
|
option (HELP_CHARACTER, HELP_NAME, HELP_DESCRIPTION, false),
|
|
|
|
m_processor (_processor)
|
|
|
|
{ ; }
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-06-09 15:41:08 +10:00
|
|
|
virtual void execute (void);
|
|
|
|
virtual void execute (const std::string& data)
|
|
|
|
{ option::execute (data); }
|
2013-02-26 18:31:14 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:31:14 +11:00
|
|
|
const char helpoption::HELP_CHARACTER = 'h';
|
|
|
|
const char *helpoption::HELP_NAME = "help";
|
|
|
|
const char *helpoption::HELP_DESCRIPTION =
|
2013-02-26 18:56:25 +11:00
|
|
|
"display help and usage information";
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:56:25 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
helpoption::execute (void)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
m_processor->print_usage ();
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
processor::processor ()
|
|
|
|
{
|
2015-04-29 17:12:34 +10:00
|
|
|
add_option (std::make_unique<helpoption> (this));
|
2013-02-27 15:10:17 +11:00
|
|
|
}
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-27 15:10:17 +11:00
|
|
|
processor::~processor ()
|
|
|
|
{ ; }
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:56:25 +11:00
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
processor::print_usage (void)
|
|
|
|
{
|
2015-04-29 17:12:34 +10:00
|
|
|
std::cout << "Usage: " << m_command << " [options]\n";
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
for (const auto &i: m_options)
|
2015-04-29 17:12:34 +10:00
|
|
|
std::cout << '\t' << *i << '\n';
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
/// Parse a single argument in short form. We are given an offset into the
|
|
|
|
/// argument array, and arguments. Establishes whether a value is present and
|
|
|
|
/// passes control over to the option.
|
|
|
|
///
|
|
|
|
/// The format of the options are "-f [b]" || "-abcde"
|
|
|
|
///
|
|
|
|
/// @param pos the current offset into the argument array
|
|
|
|
/// @param argc the size of the argument array
|
|
|
|
/// @param argv the argument array given to the application
|
|
|
|
///
|
|
|
|
/// @return the number of tokens consumed for this option; must be at least 1.
|
|
|
|
|
2013-02-26 18:56:25 +11:00
|
|
|
unsigned int
|
2015-04-29 17:18:31 +10:00
|
|
|
processor::parse_short (int pos, int argc, const char **argv)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
assert (pos > 0);
|
|
|
|
assert (pos < argc);
|
|
|
|
|
|
|
|
assert (argv[pos] != NULL);
|
2013-02-26 18:56:25 +11:00
|
|
|
const char *arg = argv[pos];
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Must begin with a dash, then have at least one non-dash character
|
|
|
|
assert (arg[0] == '-');
|
|
|
|
assert (arg[1] != '-');
|
|
|
|
assert (strlen(arg) >= 2);
|
|
|
|
|
|
|
|
// Check if we have a value option, Ie `-a [val]`
|
|
|
|
// First: must be of form '-[a-zA-Z]'.
|
|
|
|
if (strlen (arg) == 2) {
|
|
|
|
// Second: the next segment (which contains the value) shouldn't begin
|
|
|
|
// with a dash.
|
|
|
|
if (pos + 1 < argc && argv[pos+1][0] != '-') {
|
|
|
|
option * o = m_shortopt[arg[1]];
|
|
|
|
if (!o)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error ("Cannot match option");
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
o->execute (argv[pos+1]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We must have a string of no-value flags, `-abcdef...`, execute each
|
|
|
|
// option in the list after the dash.
|
|
|
|
for(unsigned int i = 1; i < strlen (arg); ++i) {
|
|
|
|
option * o = m_shortopt[arg[i]];
|
|
|
|
|
|
|
|
if (!o)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error ("Cannot match option");
|
2013-02-26 18:31:14 +11:00
|
|
|
o->execute ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
/// Parse a single argument in long form. We are given an offset into the
|
|
|
|
/// argument array, and arguments. Establishes whether a value is present and
|
|
|
|
/// passes control over to the option.
|
|
|
|
///
|
|
|
|
/// The format of the options are "--foo[=bar]"
|
|
|
|
///
|
|
|
|
/// @param pos the current offset into the argument array
|
|
|
|
/// @param argc the size of the argument array
|
|
|
|
/// @param argv the argument array given to the application`--foo[=bar]`
|
|
|
|
///
|
|
|
|
/// @return the number of tokens consumed for this option; must be 1.
|
|
|
|
|
2013-02-26 18:56:25 +11:00
|
|
|
unsigned int
|
2015-04-29 17:18:31 +10:00
|
|
|
processor::parse_long (int pos, int argc, const char ** argv)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
assert (pos > 0);
|
|
|
|
assert (pos < argc);
|
|
|
|
|
|
|
|
assert (argv[pos] != NULL);
|
2013-02-26 18:56:25 +11:00
|
|
|
const char *arg = argv[pos];
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// We must have at least two hyphens, and two letters (else a short opt)
|
|
|
|
assert (arg[0] == '-');
|
|
|
|
assert (arg[1] == '-');
|
2013-02-26 18:56:25 +11:00
|
|
|
assert (strlen (arg) >= 4);
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Skip past the dashes to the argument name
|
|
|
|
arg += 2;
|
|
|
|
|
2015-04-29 17:10:33 +10:00
|
|
|
// If there's an equals it has a value to extract
|
2013-02-27 15:11:05 +11:00
|
|
|
const char *data = strchr (arg, '=');
|
2015-04-29 17:10:33 +10:00
|
|
|
option *o = data ? m_longopt[std::string (arg, data++)]
|
|
|
|
: m_longopt[arg];
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2014-08-18 22:14:31 +10:00
|
|
|
if (!o)
|
2015-04-29 17:10:33 +10:00
|
|
|
throw std::runtime_error ("Cannot match option");
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2013-02-26 18:56:25 +11:00
|
|
|
if (data)
|
2014-08-18 22:14:31 +10:00
|
|
|
o->execute (data);
|
2013-02-26 18:31:14 +11:00
|
|
|
else
|
|
|
|
o->execute ();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
///----------------------------------------------------------------------------
|
|
|
|
///
|
|
|
|
/// Pass long and short options to specific parsing handlers.
|
|
|
|
///
|
|
|
|
/// Establishes enough context to determine if the argument is long, short, or
|
|
|
|
/// none. The functions parse_long and parse_short are given the task of
|
|
|
|
/// dispatching control to the relevant option handlers.
|
|
|
|
///
|
|
|
|
/// @param argc the raw parameter from the application main
|
|
|
|
/// @param argv the raw parameter from the application main
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
void
|
2015-04-29 17:18:31 +10:00
|
|
|
processor::parse_args (int argc, const char ** argv)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
// While technically we can have zero arguments, for practical purposes
|
|
|
|
// this is a reasonable method at catching odd errors in the library.
|
|
|
|
assert (argc > 0);
|
|
|
|
assert (argv[0]);
|
|
|
|
|
|
|
|
m_command.assign (argv[0]);
|
|
|
|
|
|
|
|
// Must make sure each option has a chance to reset state (clear flags,
|
|
|
|
// etc) between parses
|
2013-02-27 15:10:17 +11:00
|
|
|
for (auto &i: m_options)
|
2013-02-26 18:56:25 +11:00
|
|
|
i->reset ();
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2013-08-05 16:39:40 +10:00
|
|
|
const int FIRST_ARGUMENT = 1;
|
2013-02-26 18:31:14 +11:00
|
|
|
try {
|
2013-08-05 16:39:40 +10:00
|
|
|
for (int i = FIRST_ARGUMENT; i < argc; ++i) {
|
2013-02-26 18:31:14 +11:00
|
|
|
// An argument must begin with a dash, if not we've reached the
|
|
|
|
// end of the argument lists or we have a parsing error.
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
return;
|
|
|
|
|
|
|
|
// An argument must consist of at least one non-dash character
|
|
|
|
if (strlen (argv[i]) <= 1)
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::runtime_error ("Invalid argument");
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2014-08-18 22:14:31 +10:00
|
|
|
// Actually hand off args to be parsed. Subtract one from the
|
2013-02-26 18:31:14 +11:00
|
|
|
// tokens consumed, as the for loop increments tokens too.
|
|
|
|
unsigned int consumed;
|
2014-08-18 22:14:31 +10:00
|
|
|
if (argv[i][1] != '-')
|
2013-02-26 18:31:14 +11:00
|
|
|
consumed = parse_short (i, argc, argv);
|
2014-08-18 22:14:31 +10:00
|
|
|
else
|
2013-08-05 16:39:40 +10:00
|
|
|
consumed = parse_long (i, argc, argv);
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
assert (consumed >= 1);
|
2013-08-05 16:39:40 +10:00
|
|
|
i += sign_cast<int> (consumed - 1);
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
2015-04-29 17:12:34 +10:00
|
|
|
} catch (std::runtime_error &x) {
|
2013-02-26 18:31:14 +11:00
|
|
|
print_usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
for (auto &i: m_options)
|
2013-02-26 18:56:25 +11:00
|
|
|
i->finish ();
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-26 18:56:25 +11:00
|
|
|
void
|
2015-06-09 15:41:08 +10:00
|
|
|
processor::add_option (std::unique_ptr<option> opt)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::logic_error ("Short option already exists");
|
2013-02-26 18:31:14 +11:00
|
|
|
if (m_longopt.find (opt->longopt ()) != m_longopt.end ())
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::logic_error ("Long option already exists");
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
m_shortopt[opt->shortopt ()] = opt.get ();
|
|
|
|
m_longopt [opt->longopt ()] = opt.get ();
|
2013-02-26 18:31:14 +11:00
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
m_options.push_back (move (opt));
|
2013-02-26 18:31:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-27 15:10:17 +11:00
|
|
|
std::unique_ptr<option>
|
2015-06-09 15:41:08 +10:00
|
|
|
processor::remove_option (char letter)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
// Locate the option by short name
|
2013-02-26 18:56:25 +11:00
|
|
|
const auto s_candidate = m_shortopt.find (letter);
|
2013-02-26 18:31:14 +11:00
|
|
|
if (s_candidate == m_shortopt.end ())
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::logic_error ("Cannot remove an option which is not present");
|
2013-02-27 15:10:17 +11:00
|
|
|
option *target = (*s_candidate).second;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Locate the long option entry
|
2013-02-27 15:10:17 +11:00
|
|
|
const auto l_candidate = m_longopt.find (target->longopt ());
|
2013-02-26 18:31:14 +11:00
|
|
|
assert (l_candidate != m_longopt.end ());
|
|
|
|
|
|
|
|
// Remove all references and return
|
2013-02-27 15:10:17 +11:00
|
|
|
auto prime = std::find_if (
|
|
|
|
m_options.begin (),
|
|
|
|
m_options.end (),
|
|
|
|
[&target] (std::unique_ptr<option> &rhs) { return rhs.get () == target; }
|
|
|
|
);
|
|
|
|
std::unique_ptr<option> opt = move (*prime);
|
2014-08-18 22:14:31 +10:00
|
|
|
|
2013-02-26 18:31:14 +11:00
|
|
|
m_shortopt.erase (s_candidate);
|
2013-02-27 15:10:17 +11:00
|
|
|
m_longopt.erase (l_candidate);
|
|
|
|
m_options.erase (prime);
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-29 17:18:31 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-02-27 15:10:17 +11:00
|
|
|
std::unique_ptr<option>
|
2015-06-09 15:41:08 +10:00
|
|
|
processor::remove_option (const char *name)
|
|
|
|
{
|
2013-02-26 18:31:14 +11:00
|
|
|
// Locate the option by long name
|
2013-02-26 18:56:25 +11:00
|
|
|
const auto l_candidate = m_longopt.find (name);
|
2013-02-26 18:31:14 +11:00
|
|
|
if (l_candidate == m_longopt.end ())
|
2015-04-29 17:12:34 +10:00
|
|
|
throw std::logic_error ("Cannot remove an option which is not present");
|
2013-02-27 15:10:17 +11:00
|
|
|
option *target = (*l_candidate).second;
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
// Locate the short option entry
|
2013-02-27 15:10:17 +11:00
|
|
|
const auto s_candidate = m_shortopt.find (target->shortopt ());
|
2013-02-26 18:31:14 +11:00
|
|
|
assert (s_candidate != m_shortopt.end ());
|
|
|
|
|
2013-02-27 15:10:17 +11:00
|
|
|
// Remove all references and return
|
|
|
|
auto prime = std::find_if (
|
|
|
|
m_options.begin (),
|
|
|
|
m_options.end (),
|
|
|
|
[&target] (std::unique_ptr<option> &rhs) { return rhs.get () == target; }
|
|
|
|
);
|
|
|
|
std::unique_ptr<option> opt = move (*prime);
|
2014-08-18 22:14:31 +10:00
|
|
|
|
2013-02-26 18:31:14 +11:00
|
|
|
m_shortopt.erase (s_candidate);
|
2013-02-27 15:10:17 +11:00
|
|
|
m_longopt.erase (l_candidate);
|
|
|
|
m_options.erase (prime);
|
2013-02-26 18:31:14 +11:00
|
|
|
|
|
|
|
return opt;
|
|
|
|
}
|