From fdce4f38f0fe6f95d614484f15fb2a31d21f5626 Mon Sep 17 00:00:00 2001 From: Mike Smoot Date: Sun, 22 May 2011 12:05:21 -0700 Subject: [PATCH] Allows xor args to be optional --- include/tclap/CmdLine.h | 21 ++++++++++++--------- include/tclap/CmdLineInterface.h | 6 ++++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index 0fec8d8..fc47ed8 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -224,15 +224,17 @@ private: * not need to be called. * \param a - Argument to be added and xor'd. * \param b - Argument to be added and xor'd. + * \param required - Require at least one option (defaults to true) */ - void xorAdd( Arg& a, Arg& b ); + void xorAdd( Arg& a, Arg& b, bool required=true ); /** * Add a list of Args that will be xor'd. If this method is used, * add does not need to be called. * \param xors - List of Args to be added and xor'd. + * \param required - Require at least one option (defaults to true) */ - void xorAdd( std::vector& xors ); + void xorAdd( std::vector& xors, bool required=true ); /** * Parses the command line. @@ -390,24 +392,25 @@ inline void CmdLine::_constructor() deleteOnExit(v); } -inline void CmdLine::xorAdd( std::vector& ors ) +inline void CmdLine::xorAdd( std::vector& ors, bool required ) { _xorHandler.add( ors ); - for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++) - { - (*it)->forceRequired(); - (*it)->setRequireLabel( "OR required" ); + for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++) { + if (required) { + (*it)->forceRequired(); + (*it)->setRequireLabel( "OR required" ); + } add( *it ); } } -inline void CmdLine::xorAdd( Arg& a, Arg& b ) +inline void CmdLine::xorAdd( Arg& a, Arg& b, bool required ) { std::vector ors; ors.push_back( &a ); ors.push_back( &b ); - xorAdd( ors ); + xorAdd( ors, required ); } inline void CmdLine::add( Arg& a ) diff --git a/include/tclap/CmdLineInterface.h b/include/tclap/CmdLineInterface.h index 1b25e9b..9f17d70 100644 --- a/include/tclap/CmdLineInterface.h +++ b/include/tclap/CmdLineInterface.h @@ -67,15 +67,17 @@ class CmdLineInterface * not need to be called. * \param a - Argument to be added and xor'd. * \param b - Argument to be added and xor'd. + * \param required - Require at least one option (defaults to true) */ - virtual void xorAdd( Arg& a, Arg& b )=0; + virtual void xorAdd( Arg& a, Arg& b, bool required=true )=0; /** * Add a list of Args that will be xor'd. If this method is used, * add does not need to be called. * \param xors - List of Args to be added and xor'd. + * \param required - Require at least one option (defaults to true) */ - virtual void xorAdd( std::vector& xors )=0; + virtual void xorAdd( std::vector& xors, bool required=true )=0; /** * Parses the command line.