Added the option to leave out the short parameter option

This commit is contained in:
johan 2013-08-11 13:13:19 +02:00
parent daabeaa03b
commit 29f6e41923
7 changed files with 59 additions and 57 deletions

View File

@ -1,7 +1,8 @@
# define the minimum cmake version (as required by cmake)
cmake_minimum_required(VERSION 2.8)
#set(CMAKE_TOOLCHAIN_FILE /opt/raspberrypi/Toolchain-RaspberryPi.cmake)
set(CMAKE_TOOLCHAIN_FILE /home/johan/raspberrypi/Toolchain-RaspberryPi.cmake)
# Define the main-project name
project(Hyperion)

View File

@ -7,3 +7,4 @@
# otherwise agree to in writing.
add_subdirectory(jsoncpp)
add_subdirectory(getoptPlusPlus)

View File

@ -14,10 +14,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "getoptpp.h"
#include <stdexcept>
#include <cassert>
using namespace std;
@ -199,7 +198,8 @@ Parameter::~Parameter() {}
const string& Parameter::description() const { return fdescription; }
const string& Parameter::longOption() const { return flongOption; }
char Parameter::shortOption() const { return fshortOption; }
bool Parameter::hasShortOption() const { return fshortOption != 0x0; }
char Parameter::shortOption() const { assert(hasShortOption()); return fshortOption; }
/*
*

View File

@ -175,15 +175,6 @@ public:
/** Test whether the parameter has been set */
virtual bool isSet() const = 0;
/** Attempt to down-cast to PODParameter<T>.
*
* This is very convenient, but also an unholy crime against
* most principles of sane OOP design.
*/
template<typename T>
T get() const;
/** This parameter's line in OptionsParser::usage() */
virtual std::string usageLine() const = 0;
@ -193,6 +184,9 @@ public:
/** The long name of this parameter (e.g. "--option"), without the dash. */
const std::string& longOption() const;
/** Check if this parameters has a short option */
bool hasShortOption() const;
/** The short name of this parameter (e.g. "-o"), without the dash. */
char shortOption() const;

View File

@ -30,16 +30,6 @@ T &ParameterSet::add(char shortName, const char* longName, const char* descripti
return *p;
}
template<typename T>
T Parameter::get() const{
const PODParameter<T> *ppt = dynamic_cast<const PODParameter<T>*>(this);
if(ppt) {
return ppt->getValue();
}
throw new std::runtime_error("Type conversion not possible");
}
/*
*
* Class CommonParameter implementation
@ -63,8 +53,15 @@ template<typename SwitchingBehavior>
std::string CommonParameter<SwitchingBehavior>::usageLine() const {
std::stringstream strstr;
strstr.width(10);
strstr.width(10);
if (hasShortOption())
{
strstr << std::left<< std::string("-") + shortOption();
}
else
{
strstr << " ";
}
strstr.width(20);
strstr << std::left << "--" + longOption();
@ -82,21 +79,20 @@ bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Param
if(arg.at(0) != '-') return false;
if(arg.at(1) == '-') { /* Long form parameter */
try {
unsigned int eq = arg.find_first_of("=");
if(eq == std::string::npos) {
if(arg.substr(2) != longOption())
return false;
if(arg.substr(2) != longOption())
return false;
this->receiveSwitch();
} else {
if(arg.substr(2, eq-2) != longOption())
return false;
this->receiveSwitch();
} else {
if(arg.substr(2, eq-2) != longOption())
return false;
this->receiveArgument(arg.substr(eq+1));
}
this->receiveArgument(arg.substr(eq+1));
}
return true;
} catch(Parameter::ExpectedArgument &ea) {
throw ExpectedArgument("--" + longOption() + ": expected an argument");
@ -112,28 +108,29 @@ bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Param
throw Parameter::ParameterRejected("--" + longOption() + " (unspecified error)");
}
}
else if (hasShortOption())
{
try {
if(arg.at(1) == shortOption()) {
/* Matched argument on the form -f or -fsomething */
if(arg.length() == 2) { /* -f */
this->receiveSwitch();
try {
if(arg.at(1) == shortOption()) {
/* Matched argument on the form -f or -fsomething */
if(arg.length() == 2) { /* -f */
this->receiveSwitch();
return true;
} else { /* -fsomething */
this->receiveArgument(arg.substr(2));
return true;
}
}
} catch(Parameter::ExpectedArgument &ea) {
throw ExpectedArgument(std::string("-") + shortOption() + ": expected an argument");
} catch(Parameter::UnexpectedArgument &ua) {
throw UnexpectedArgument(std::string("-") + shortOption() + ": did not expect an argument");
} catch(Switchable::SwitchingError &e) {
throw ParameterRejected(std::string("-") + shortOption() + ": parameter already set");
}
return true;
} else { /* -fsomething */
this->receiveArgument(arg.substr(2));
return true;
}
}
} catch(Parameter::ExpectedArgument &ea) {
throw ExpectedArgument(std::string("-") + shortOption() + ": expected an argument");
} catch(Parameter::UnexpectedArgument &ua) {
throw UnexpectedArgument(std::string("-") + shortOption() + ": did not expect an argument");
} catch(Switchable::SwitchingError &e) {
throw ParameterRejected(std::string("-") + shortOption() + ": parameter already set");
}
}
} catch(std::out_of_range& o) {
return false;
}
@ -180,8 +177,15 @@ template<typename T>
std::string PODParameter<T>::usageLine() const {
std::stringstream strstr;
strstr.width(10);
strstr << std::left<< std::string("-") + shortOption() +"arg";
strstr.width(10);
if (hasShortOption())
{
strstr << std::left << std::string("-") + shortOption() +"arg";
}
else
{
strstr << "";
}
strstr.width(20);
strstr << std::left << "--" + longOption() + "=arg";

View File

@ -52,3 +52,5 @@ if(PNG_FOUND)
endif(PNG_FOUND)
add_subdirectory(dispmanx-png)
add_subdirectory(hyperion-remote)

View File

@ -20,12 +20,12 @@ int main(int argc, const char * argv[])
StringParameter & argImage = parameters.add<StringParameter>('i', "image" , "Set the leds to the colors according to the given image file");
SwitchParameter & argList = parameters.add<SwitchParameter>('l', "list" , "List all priority channels which are in use");
SwitchParameter & argClear = parameters.add<SwitchParameter>('x', "clear" , "Clear data for the priority channel provided by the -p option");
SwitchParameter & argClearAll = parameters.add<SwitchParameter>('y', "clear-all" , "Clear data for all priority channels");
SwitchParameter & argClearAll = parameters.add<SwitchParameter>(0x0, "clear-all" , "Clear data for all priority channels");
DoubleParameter & argGamma = parameters.add<DoubleParameter>('g', "gamma" , "Set the gamma of the leds (requires 3 values)");
DoubleParameter & argThreshold = parameters.add<DoubleParameter>('t', "threshold" , "Set the threshold of the leds (requires 3 values between 0.0 and 1.0)");
DoubleParameter & argBlacklevel = parameters.add<DoubleParameter>('b', "blacklevel", "Set the blacklevel of the leds (requires 3 values which are normally between 0.0 and 1.0)");
DoubleParameter & argWhitelevel = parameters.add<DoubleParameter>('w', "whitelevel", "Set the whitelevel of the leds (requires 3 values which are normally between 0.0 and 1.0)");
SwitchParameter & argPrint = parameters.add<SwitchParameter>('z', "print" , "Print the json input and output messages on stdout");
SwitchParameter & argPrint = parameters.add<SwitchParameter>(0x0, "print" , "Print the json input and output messages on stdout");
SwitchParameter & argHelp = parameters.add<SwitchParameter>('h', "help" , "Show this help message and exit");
try
{