mirror of
https://github.com/cuberite/TCLAP.git
synced 2025-09-08 03:40:21 -04:00
fix for strings with spaces
This commit is contained in:
parent
3055cfa15c
commit
510f1435e0
@ -29,138 +29,7 @@
|
||||
#include <tclap/ValueArg.h>
|
||||
#include <tclap/UnlabeledValueArg.h>
|
||||
#include <tclap/Visitor.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
#include <tclap/OrHandler.h>
|
||||
#include <tclap/CommandLine.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace TCLAP {
|
||||
|
||||
class CmdLine
|
||||
{
|
||||
protected:
|
||||
|
||||
/**
|
||||
* The list of arguments that will be tested against the
|
||||
* command line.
|
||||
*/
|
||||
list<Arg*> _argList;
|
||||
|
||||
/**
|
||||
* The name of the program. Set to argv[0].
|
||||
*/
|
||||
string _progName;
|
||||
|
||||
/**
|
||||
* A message used to describe the program. Used in the usage output.
|
||||
*/
|
||||
string _message;
|
||||
|
||||
/**
|
||||
* The version to be displayed with the --version switch.
|
||||
*/
|
||||
string _version;
|
||||
|
||||
/**
|
||||
* The number of arguments that are required to be present on
|
||||
* the command line. This is set dynamically, based on the
|
||||
* Args added to the CmdLine object.
|
||||
*/
|
||||
int _numRequired;
|
||||
|
||||
/**
|
||||
* The character that is used to separate the argument flag/name
|
||||
* from the value. Defaults to ' ' (space).
|
||||
*/
|
||||
char _delimiter;
|
||||
|
||||
/**
|
||||
* Checks whether a name/flag string matches entirely matches
|
||||
* the Arg::blankChar. Used when multiple switches are combined
|
||||
* into a single argument.
|
||||
* \param s - The message to be used in the usage.
|
||||
*/
|
||||
bool _emptyCombined(const string& s);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Encapsulates the code common to the constructors (which is all
|
||||
* of it).
|
||||
*/
|
||||
void _constructor();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Command line constructor. DEPRECATED!!! This is here to maintain
|
||||
* backwards compatibility with earlier releases. Note that the
|
||||
* program name will be overwritten with argv[0]. The delimiter
|
||||
* used is ' ' (as before).
|
||||
* \param name - The program name - will be overwritten with argv[0].
|
||||
* \param message - The message to be used in the usage output.
|
||||
* \param version - The version number to be used in the
|
||||
* --version switch.
|
||||
*/
|
||||
CmdLine(const string& name,
|
||||
const string& message,
|
||||
const string& version = "none" );
|
||||
|
||||
/**
|
||||
* Command line constructor. Defines how the arguments will be
|
||||
* parsed.
|
||||
* \param message - The message to be used in the usage
|
||||
* output.
|
||||
* \param delimiter - The character that is used to separate
|
||||
* the argument flag/name from the value. Defaults to ' ' (space).
|
||||
* \param version - The version number to be used in the
|
||||
* --version switch.
|
||||
*/
|
||||
CmdLine(const string& message,
|
||||
const char delimiter = ' ',
|
||||
const string& version = "none" );
|
||||
|
||||
/**
|
||||
* Adds an argument to the list of arguments to be parsed.
|
||||
* \param a - Argument to be added.
|
||||
*/
|
||||
void add( Arg& a );
|
||||
|
||||
/**
|
||||
* Prints the usage to stdout and exits.
|
||||
* \param exitVal - Value to exit with.
|
||||
*/
|
||||
void usage( int exitVal = 0 );
|
||||
|
||||
/**
|
||||
* Prints the version to stdout and exits.
|
||||
* \param exitVal - Value to exit with.
|
||||
*/
|
||||
void version( int exitVal = 0 );
|
||||
|
||||
/**
|
||||
* Parses the command line.
|
||||
* \param argc - Number of arguments.
|
||||
* \param argv - Array of arguments.
|
||||
*/
|
||||
void parse(int argc, char** argv);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// These have to be included _after_ CmdLine is declared, because
|
||||
// they rely on CmdLine existing. Don't know why I can't forward
|
||||
// declare CmdLine... this works.
|
||||
|
||||
#include "HelpVisitor.h"
|
||||
#include "VersionVisitor.h"
|
||||
#include "IgnoreRestVisitor.h"
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
172
include/tclap/CommandLine.h
Normal file
172
include/tclap/CommandLine.h
Normal file
@ -0,0 +1,172 @@
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* file: CmdLine.h
|
||||
*
|
||||
* Copyright (c) 2003, Michael E. Smoot .
|
||||
* All rights reverved.
|
||||
*
|
||||
* See the file COPYING in the top directory of this distribution for
|
||||
* more information.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef __COMMANDLINE_HH__
|
||||
#define __COMMANDLINE_HH__
|
||||
|
||||
#include <tclap/Arg.h>
|
||||
#include <tclap/SwitchArg.h>
|
||||
//#include <tclap/MultiArg.h>
|
||||
//#include <tclap/UnlabeledMultiArg.h>
|
||||
//#include <tclap/ValueArg.h>
|
||||
//#include <tclap/UnlabeledValueArg.h>
|
||||
#include <tclap/Visitor.h>
|
||||
#include <tclap/OrHandler.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace TCLAP {
|
||||
|
||||
class CmdLine
|
||||
{
|
||||
protected:
|
||||
|
||||
/**
|
||||
* The list of arguments that will be tested against the
|
||||
* command line.
|
||||
*/
|
||||
list<Arg*> _argList;
|
||||
|
||||
/**
|
||||
* The name of the program. Set to argv[0].
|
||||
*/
|
||||
string _progName;
|
||||
|
||||
/**
|
||||
* A message used to describe the program. Used in the usage output.
|
||||
*/
|
||||
string _message;
|
||||
|
||||
/**
|
||||
* The version to be displayed with the --version switch.
|
||||
*/
|
||||
string _version;
|
||||
|
||||
/**
|
||||
* The number of arguments that are required to be present on
|
||||
* the command line. This is set dynamically, based on the
|
||||
* Args added to the CmdLine object.
|
||||
*/
|
||||
int _numRequired;
|
||||
|
||||
/**
|
||||
* The character that is used to separate the argument flag/name
|
||||
* from the value. Defaults to ' ' (space).
|
||||
*/
|
||||
char _delimiter;
|
||||
|
||||
OrHandler _xorHandler;
|
||||
|
||||
/**
|
||||
* Checks whether a name/flag string matches entirely matches
|
||||
* the Arg::blankChar. Used when multiple switches are combined
|
||||
* into a single argument.
|
||||
* \param s - The message to be used in the usage.
|
||||
*/
|
||||
bool _emptyCombined(const string& s);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Encapsulates the code common to the constructors (which is all
|
||||
* of it).
|
||||
*/
|
||||
void _constructor();
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Command line constructor. DEPRECATED!!! This is here to maintain
|
||||
* backwards compatibility with earlier releases. Note that the
|
||||
* program name will be overwritten with argv[0]. The delimiter
|
||||
* used is ' ' (as before).
|
||||
* \param name - The program name - will be overwritten with argv[0].
|
||||
* \param message - The message to be used in the usage output.
|
||||
* \param version - The version number to be used in the
|
||||
* --version switch.
|
||||
*/
|
||||
CmdLine(const string& name,
|
||||
const string& message,
|
||||
const string& version = "none" );
|
||||
|
||||
/**
|
||||
* Command line constructor. Defines how the arguments will be
|
||||
* parsed.
|
||||
* \param message - The message to be used in the usage
|
||||
* output.
|
||||
* \param delimiter - The character that is used to separate
|
||||
* the argument flag/name from the value. Defaults to ' ' (space).
|
||||
* \param version - The version number to be used in the
|
||||
* --version switch.
|
||||
*/
|
||||
CmdLine(const string& message,
|
||||
const char delimiter = ' ',
|
||||
const string& version = "none" );
|
||||
|
||||
/**
|
||||
* Adds an argument to the list of arguments to be parsed.
|
||||
* \param a - Argument to be added.
|
||||
*/
|
||||
void add( Arg& a );
|
||||
void add( Arg* a );
|
||||
void xorAdd( Arg& a, Arg& b );
|
||||
void xorAdd( vector<Arg*>& ors );
|
||||
|
||||
/**
|
||||
* Prints the usage to stdout and exits.
|
||||
* \param exitVal - Value to exit with.
|
||||
*/
|
||||
void usage( int exitVal = 0 );
|
||||
|
||||
/**
|
||||
* Prints the version to stdout and exits.
|
||||
* \param exitVal - Value to exit with.
|
||||
*/
|
||||
void version( int exitVal = 0 );
|
||||
|
||||
/**
|
||||
* Parses the command line.
|
||||
* \param argc - Number of arguments.
|
||||
* \param argv - Array of arguments.
|
||||
*/
|
||||
void parse(int argc, char** argv);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// These have to be included _after_ CmdLine is declared, because
|
||||
// they rely on CmdLine existing. Don't know why I can't forward
|
||||
// declare CmdLine... this works.
|
||||
|
||||
#include "HelpVisitor.h"
|
||||
#include "VersionVisitor.h"
|
||||
#include "IgnoreRestVisitor.h"
|
||||
|
||||
|
||||
#endif
|
@ -205,6 +205,18 @@ void MultiArg<T>::_extractValue( const string& val )
|
||||
_checkWithVisitor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialization to handle strings with spaces in them. This is needed
|
||||
* because there is no way to tell operator>> to ignore spaces.
|
||||
*/
|
||||
template<>
|
||||
void MultiArg<string>::_extractValue( const string& val )
|
||||
{
|
||||
_values.push_back(val);
|
||||
|
||||
_checkWithVisitor();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -139,7 +139,6 @@ class ValueArg : public Arg
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor implementation.
|
||||
*/
|
||||
@ -181,6 +180,7 @@ bool ValueArg<T>::processArg(int *i, vector<string>& args)
|
||||
return false;
|
||||
|
||||
string flag = args[*i];
|
||||
|
||||
string value = "";
|
||||
trimFlag( flag, value );
|
||||
|
||||
@ -225,6 +225,17 @@ void ValueArg<T>::_extractValue( const string& val )
|
||||
throw( ArgException("Couldn't read argument value!", toString() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialization for string. This is necessary because istringstream
|
||||
* operator>> is not able to ignore spaces... meaning -x "X Y" will only
|
||||
* read 'X'... and thus the specialization.
|
||||
*/
|
||||
template<>
|
||||
void ValueArg<string>::_extractValue( const string& val )
|
||||
{
|
||||
_value = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of shortID.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user