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) # define the minimum cmake version (as required by cmake)
cmake_minimum_required(VERSION 2.8) 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 # Define the main-project name
project(Hyperion) project(Hyperion)

View File

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

View File

@ -14,10 +14,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "getoptpp.h" #include "getoptpp.h"
#include <stdexcept> #include <stdexcept>
#include <cassert>
using namespace std; using namespace std;
@ -199,7 +198,8 @@ Parameter::~Parameter() {}
const string& Parameter::description() const { return fdescription; } const string& Parameter::description() const { return fdescription; }
const string& Parameter::longOption() const { return flongOption; } 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 */ /** Test whether the parameter has been set */
virtual bool isSet() const = 0; 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() */ /** This parameter's line in OptionsParser::usage() */
virtual std::string usageLine() const = 0; virtual std::string usageLine() const = 0;
@ -193,6 +184,9 @@ public:
/** The long name of this parameter (e.g. "--option"), without the dash. */ /** The long name of this parameter (e.g. "--option"), without the dash. */
const std::string& longOption() const; 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. */ /** The short name of this parameter (e.g. "-o"), without the dash. */
char shortOption() const; char shortOption() const;

View File

@ -30,16 +30,6 @@ T &ParameterSet::add(char shortName, const char* longName, const char* descripti
return *p; 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 * Class CommonParameter implementation
@ -64,7 +54,14 @@ std::string CommonParameter<SwitchingBehavior>::usageLine() const {
std::stringstream strstr; std::stringstream strstr;
strstr.width(10); strstr.width(10);
if (hasShortOption())
{
strstr << std::left<< std::string("-") + shortOption(); strstr << std::left<< std::string("-") + shortOption();
}
else
{
strstr << " ";
}
strstr.width(20); strstr.width(20);
strstr << std::left << "--" + longOption(); strstr << std::left << "--" + longOption();
@ -82,7 +79,6 @@ bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Param
if(arg.at(0) != '-') return false; if(arg.at(0) != '-') return false;
if(arg.at(1) == '-') { /* Long form parameter */ if(arg.at(1) == '-') { /* Long form parameter */
try { try {
unsigned int eq = arg.find_first_of("="); unsigned int eq = arg.find_first_of("=");
@ -112,7 +108,8 @@ bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Param
throw Parameter::ParameterRejected("--" + longOption() + " (unspecified error)"); throw Parameter::ParameterRejected("--" + longOption() + " (unspecified error)");
} }
} }
else if (hasShortOption())
{
try { try {
if(arg.at(1) == shortOption()) { if(arg.at(1) == shortOption()) {
/* Matched argument on the form -f or -fsomething */ /* Matched argument on the form -f or -fsomething */
@ -133,7 +130,7 @@ bool CommonParameter<SwitchingBehavior>::receive(ParserState& state) throw(Param
} catch(Switchable::SwitchingError &e) { } catch(Switchable::SwitchingError &e) {
throw ParameterRejected(std::string("-") + shortOption() + ": parameter already set"); throw ParameterRejected(std::string("-") + shortOption() + ": parameter already set");
} }
}
} catch(std::out_of_range& o) { } catch(std::out_of_range& o) {
return false; return false;
} }
@ -181,7 +178,14 @@ std::string PODParameter<T>::usageLine() const {
std::stringstream strstr; std::stringstream strstr;
strstr.width(10); strstr.width(10);
if (hasShortOption())
{
strstr << std::left << std::string("-") + shortOption() +"arg"; strstr << std::left << std::string("-") + shortOption() +"arg";
}
else
{
strstr << "";
}
strstr.width(20); strstr.width(20);
strstr << std::left << "--" + longOption() + "=arg"; strstr << std::left << "--" + longOption() + "=arg";

View File

@ -52,3 +52,5 @@ if(PNG_FOUND)
endif(PNG_FOUND) endif(PNG_FOUND)
add_subdirectory(dispmanx-png) 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"); 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 & 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 & 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 & 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 & 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 & 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)"); 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"); SwitchParameter & argHelp = parameters.add<SwitchParameter>('h', "help" , "Show this help message and exit");
try try
{ {