Whitespace and minor c++11 fixups
This commit is contained in:
parent
0b30b56c58
commit
d482f1fde4
84
options.cpp
84
options.cpp
@ -22,17 +22,15 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cmath>
|
#include <cstring>
|
||||||
#include <stdint.h>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <stdint.h>
|
#include <stdexcept>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -57,13 +55,16 @@ option::option (char _letter,
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void option::execute (void) {
|
void
|
||||||
|
option::execute (void) {
|
||||||
throw runtime_error(
|
throw runtime_error(
|
||||||
"Cannot provide no value for the option '" + m_longopt + "'"
|
"Cannot provide no value for the option '" + m_longopt + "'"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void option::execute (const string& data) {
|
|
||||||
|
void
|
||||||
|
option::execute (const string& data) {
|
||||||
assert(data.size() > 0);
|
assert(data.size() > 0);
|
||||||
throw runtime_error(
|
throw runtime_error(
|
||||||
"Cannot provide a value for the option '" + m_longopt + "'"
|
"Cannot provide a value for the option '" + m_longopt + "'"
|
||||||
@ -71,7 +72,8 @@ void option::execute (const string& data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ostream& operator<< (ostream & os, const option& opt) {
|
ostream&
|
||||||
|
operator<< (ostream & os, const option& opt) {
|
||||||
os << (opt.is_required () ? " -" : "[-" ) << opt.shortopt ()
|
os << (opt.is_required () ? " -" : "[-" ) << opt.shortopt ()
|
||||||
<< (opt.is_required () ? " \t" : "]\t") << opt.longopt ()
|
<< (opt.is_required () ? " \t" : "]\t") << opt.longopt ()
|
||||||
<< "\t" << opt.description ();
|
<< "\t" << opt.description ();
|
||||||
@ -177,7 +179,6 @@ bytesoption::execute (const std::string& data) {
|
|||||||
|
|
||||||
size_t defaultvalue = *m_data;
|
size_t defaultvalue = *m_data;
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bytesmodifier modifier = m_modifier;
|
bytesmodifier modifier = m_modifier;
|
||||||
off_t cursor = data.size () - 1;
|
off_t cursor = data.size () - 1;
|
||||||
@ -231,7 +232,7 @@ bytesoption::execute (const std::string& data) {
|
|||||||
--cursor;
|
--cursor;
|
||||||
assert (cursor >= 0);
|
assert (cursor >= 0);
|
||||||
|
|
||||||
multiplier = pow ((double)modifier_factor, (int)specified);
|
multiplier = std::pow (modifier_factor, (int)specified);
|
||||||
get_arg (data.substr(0, cursor + 1), m_data);
|
get_arg (data.substr(0, cursor + 1), m_data);
|
||||||
*m_data *= multiplier;
|
*m_data *= multiplier;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
@ -245,6 +246,7 @@ bytesoption::execute (const std::string& data) {
|
|||||||
m_found = true;
|
m_found = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Internal helper options. Print help and usage.
|
* Internal helper options. Print help and usage.
|
||||||
* A callback to the processor which triggers output of the help message.
|
* A callback to the processor which triggers output of the help message.
|
||||||
@ -279,7 +281,8 @@ const char *helpoption::HELP_DESCRIPTION =
|
|||||||
"display help and usage information";
|
"display help and usage information";
|
||||||
|
|
||||||
|
|
||||||
void helpoption::execute (void) {
|
void
|
||||||
|
helpoption::execute (void) {
|
||||||
m_processor->print_usage ();
|
m_processor->print_usage ();
|
||||||
exit (EXIT_SUCCESS);
|
exit (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
@ -294,17 +297,17 @@ processor::processor ()
|
|||||||
|
|
||||||
|
|
||||||
processor::~processor () {
|
processor::~processor () {
|
||||||
for(auto i = m_options.begin(); i != m_options.end(); i++)
|
for (auto i: m_options)
|
||||||
delete *i;
|
delete i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void processor::print_usage (void) {
|
void
|
||||||
|
processor::print_usage (void) {
|
||||||
cout << "Usage: " << m_command << " [options]" << endl;
|
cout << "Usage: " << m_command << " [options]" << endl;
|
||||||
|
|
||||||
for(list<option *>::const_iterator i = m_options.begin ();
|
for (const auto i: m_options)
|
||||||
i != m_options.end (); i++)
|
cout << '\t' << *i << endl;
|
||||||
cout << '\t' << **i << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -321,7 +324,8 @@ void processor::print_usage (void) {
|
|||||||
*
|
*
|
||||||
* @return the number of tokens consumed for this option; must be at least 1.
|
* @return the number of tokens consumed for this option; must be at least 1.
|
||||||
*/
|
*/
|
||||||
unsigned int processor::parse_short (int pos, int argc, const char ** argv) {
|
unsigned int
|
||||||
|
processor::parse_short (int pos, int argc, const char **argv) {
|
||||||
assert (pos > 0);
|
assert (pos > 0);
|
||||||
assert (pos < argc);
|
assert (pos < argc);
|
||||||
|
|
||||||
@ -375,7 +379,8 @@ unsigned int processor::parse_short (int pos, int argc, const char ** argv) {
|
|||||||
*
|
*
|
||||||
* @return the number of tokens consumed for this option; must be 1.
|
* @return the number of tokens consumed for this option; must be 1.
|
||||||
*/
|
*/
|
||||||
unsigned int processor::parse_long (int pos, int argc, const char ** argv) {
|
unsigned int
|
||||||
|
processor::parse_long (int pos, int argc, const char ** argv) {
|
||||||
assert (pos > 0);
|
assert (pos > 0);
|
||||||
assert (pos < argc);
|
assert (pos < argc);
|
||||||
|
|
||||||
@ -432,8 +437,8 @@ processor::parse_args (int argc, const char ** argv) {
|
|||||||
|
|
||||||
// Must make sure each option has a chance to reset state (clear flags,
|
// Must make sure each option has a chance to reset state (clear flags,
|
||||||
// etc) between parses
|
// etc) between parses
|
||||||
for(auto i = m_options.begin (); i != m_options.end (); i++)
|
for (auto i: m_options)
|
||||||
(*i)->reset ();
|
i->reset ();
|
||||||
|
|
||||||
const unsigned int FIRST_ARGUMENT = 1;
|
const unsigned int FIRST_ARGUMENT = 1;
|
||||||
try {
|
try {
|
||||||
@ -463,12 +468,13 @@ processor::parse_args (int argc, const char ** argv) {
|
|||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto i = m_options.begin (); i != m_options.end (); ++i)
|
for (auto i: m_options)
|
||||||
(*i)->finish ();
|
i->finish ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void processor::add_option (option * opt) {
|
void
|
||||||
|
processor::add_option (option *opt) {
|
||||||
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
|
if (m_shortopt.find (opt->shortopt ()) != m_shortopt.end ())
|
||||||
throw logic_error ("Short option already exists");
|
throw logic_error ("Short option already exists");
|
||||||
if (m_longopt.find (opt->longopt ()) != m_longopt.end ())
|
if (m_longopt.find (opt->longopt ()) != m_longopt.end ())
|
||||||
@ -481,16 +487,16 @@ void processor::add_option (option * opt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
option* processor::remove_option (char letter) {
|
option*
|
||||||
|
processor::remove_option (char letter) {
|
||||||
// Locate the option by short name
|
// Locate the option by short name
|
||||||
std::map<char, option*>::iterator s_candidate = m_shortopt.find (letter);
|
const auto s_candidate = m_shortopt.find (letter);
|
||||||
|
|
||||||
if (s_candidate == m_shortopt.end ())
|
if (s_candidate == m_shortopt.end ())
|
||||||
throw logic_error ("Cannot remove an option which is not present");
|
throw logic_error ("Cannot remove an option which is not present");
|
||||||
option *opt = (*s_candidate).second;
|
option *opt = (*s_candidate).second;
|
||||||
|
|
||||||
// Locate the long option entry
|
// Locate the long option entry
|
||||||
std::map<string, option*>::iterator l_candidate = m_longopt.find (opt->longopt ());
|
const auto l_candidate = m_longopt.find (opt->longopt ());
|
||||||
assert (l_candidate != m_longopt.end ());
|
assert (l_candidate != m_longopt.end ());
|
||||||
|
|
||||||
// Remove all references and return
|
// Remove all references and return
|
||||||
@ -502,16 +508,16 @@ option* processor::remove_option (char letter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
option* processor::remove_option (const char * name) {
|
option*
|
||||||
|
processor::remove_option (const char *name) {
|
||||||
// Locate the option by long name
|
// Locate the option by long name
|
||||||
std::map<string, option*>::iterator l_candidate = m_longopt.find (name);
|
const auto l_candidate = m_longopt.find (name);
|
||||||
|
|
||||||
if (l_candidate == m_longopt.end ())
|
if (l_candidate == m_longopt.end ())
|
||||||
throw logic_error ("Cannot remove an option which is not present");
|
throw logic_error ("Cannot remove an option which is not present");
|
||||||
option * opt = (*l_candidate).second;
|
option * opt = (*l_candidate).second;
|
||||||
|
|
||||||
// Locate the short option entry
|
// Locate the short option entry
|
||||||
std::map<char, option*>::iterator s_candidate = m_shortopt.find (opt->shortopt ());
|
const auto s_candidate = m_shortopt.find (opt->shortopt ());
|
||||||
assert (s_candidate != m_shortopt.end ());
|
assert (s_candidate != m_shortopt.end ());
|
||||||
|
|
||||||
// Remove all references and return the option object
|
// Remove all references and return the option object
|
||||||
@ -526,12 +532,6 @@ option* processor::remove_option (const char * name) {
|
|||||||
/* Parse args from a stream, one arg per line
|
/* Parse args from a stream, one arg per line
|
||||||
*/
|
*/
|
||||||
void parse_stream (std::istream & is) {
|
void parse_stream (std::istream & is) {
|
||||||
char * buffer = new char[MAX_CHUNK_LENGTH + 1];
|
unique_ptr<char[]> buffer (new char [MAX_CHUNK_LENGTH + 1]);
|
||||||
try {
|
|
||||||
|
|
||||||
} catch (...) {
|
|
||||||
delete [] buffer;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
options.hpp
14
options.hpp
@ -222,13 +222,13 @@ namespace util {
|
|||||||
/* Description of types available for parsing
|
/* Description of types available for parsing
|
||||||
*/
|
*/
|
||||||
enum bytestype {
|
enum bytestype {
|
||||||
BYTES_SINGLE,
|
BYTES_SINGLE = 0,
|
||||||
BYTES_KILO,
|
BYTES_KILO = 1,
|
||||||
BYTES_MEGA,
|
BYTES_MEGA = 2,
|
||||||
BYTES_GIGA,
|
BYTES_GIGA = 3,
|
||||||
BYTES_TERA,
|
BYTES_TERA = 4,
|
||||||
BYTES_PETA,
|
BYTES_PETA = 5,
|
||||||
BYTES_EXA,
|
BYTES_EXA = 6,
|
||||||
|
|
||||||
// Currently does not support yota or zeta as there can be
|
// Currently does not support yota or zeta as there can be
|
||||||
// trouble converting them without loss into 64bit quantities.
|
// trouble converting them without loss into 64bit quantities.
|
||||||
|
@ -18,7 +18,8 @@ using namespace util;
|
|||||||
|
|
||||||
|
|
||||||
// Check that null options don't throw anything
|
// Check that null options don't throw anything
|
||||||
void test_null_opt(void) {
|
void
|
||||||
|
test_null_opt (void) {
|
||||||
unique_ptr<processor> p(new processor());
|
unique_ptr<processor> p(new processor());
|
||||||
|
|
||||||
p->add_option(new nulloption('n', "null", "testing null option"));
|
p->add_option(new nulloption('n', "null", "testing null option"));
|
||||||
@ -34,7 +35,8 @@ void test_null_opt(void) {
|
|||||||
|
|
||||||
|
|
||||||
// Check if presence options can be used successfully
|
// Check if presence options can be used successfully
|
||||||
void test_present_opt(void) {
|
void
|
||||||
|
test_present_opt (void) {
|
||||||
unique_ptr<processor> p(new processor());
|
unique_ptr<processor> p(new processor());
|
||||||
bool is_present;
|
bool is_present;
|
||||||
p->add_option(new presentoption('p', "present", "option is present", &is_present));
|
p->add_option(new presentoption('p', "present", "option is present", &is_present));
|
||||||
@ -60,12 +62,12 @@ void test_present_opt(void) {
|
|||||||
|
|
||||||
|
|
||||||
// Check all forms of boolean inputs
|
// Check all forms of boolean inputs
|
||||||
void test_bool_opt(void) {
|
void
|
||||||
|
test_bool_opt (void) {
|
||||||
unique_ptr<processor> p(new processor());
|
unique_ptr<processor> p(new processor());
|
||||||
bool value = false;
|
bool value = false;
|
||||||
|
|
||||||
p->add_option(new valueoption<bool>('b', "bool",
|
p->add_option (new valueoption<bool>('b', "bool", "testing boolean actions", &value));
|
||||||
"testing boolean actions", &value));
|
|
||||||
|
|
||||||
// List all legal forms of positive or negative boolean values
|
// List all legal forms of positive or negative boolean values
|
||||||
const char *argv[] = { "./foo", "-b", NULL };
|
const char *argv[] = { "./foo", "-b", NULL };
|
||||||
@ -99,11 +101,11 @@ void test_bool_opt(void) {
|
|||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void test_numeric_opt(void) {
|
void
|
||||||
|
test_numeric_opt (void) {
|
||||||
unique_ptr<processor> p(new processor ());
|
unique_ptr<processor> p(new processor ());
|
||||||
T value;
|
T value;
|
||||||
p->add_option(new valueoption<T>('t', "type",
|
p->add_option (new valueoption<T> ('t', "type", "testing type option", &value));
|
||||||
"testing type option", &value));
|
|
||||||
|
|
||||||
T values[] = {
|
T values[] = {
|
||||||
// TODO: Enable minimum value testing. Currently disabled as
|
// TODO: Enable minimum value testing. Currently disabled as
|
||||||
@ -141,7 +143,8 @@ void test_numeric_opt(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_bytes_opt(void) {
|
void
|
||||||
|
test_bytes_opt(void) {
|
||||||
unique_ptr<processor> p(new processor ());
|
unique_ptr<processor> p(new processor ());
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
@ -202,7 +205,8 @@ void test_bytes_opt(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_insert_remove_opt(void) {
|
void
|
||||||
|
test_insert_remove_opt (void) {
|
||||||
unique_ptr<processor> p(new processor ());
|
unique_ptr<processor> p(new processor ());
|
||||||
nulloption opt ('n', "null-option", "null testing action");
|
nulloption opt ('n', "null-option", "null testing action");
|
||||||
|
|
||||||
@ -219,7 +223,8 @@ void test_insert_remove_opt(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_required (void) {
|
void
|
||||||
|
test_required (void) {
|
||||||
unique_ptr<processor> p (new processor ());
|
unique_ptr<processor> p (new processor ());
|
||||||
p->add_option (new nulloption ('n',
|
p->add_option (new nulloption ('n',
|
||||||
"null",
|
"null",
|
||||||
|
Loading…
Reference in New Issue
Block a user