1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: antsCommandLineParser.h,v $
Language: C++
Date: $Date: 2009/01/22 22:43:11 $
Version: $Revision: 1.1 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __antsCommandLineParser_h
#define __antsCommandLineParser_h
#include "antsCommandLineOption.h"
#include "itkDataObject.h"
#include "itkObjectFactory.h"
#include "itkMacro.h"
#include "itkNumericTraits.h"
#include <list>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <typeinfo>
namespace itk
{
namespace ants
{
/**
* A untilty function to convert internal typeid(???).name() to
* the human readable equivalent format.
*/
extern std::string ConvertToHumanReadable(const std::string input);
/** \class CommandLineParser
\brief Simple command line parser.
\par
Parses the standard ( argc, argv ) variables which are stored
as options in the helper class antsCommandLineOption. Also contains
routines for converting types including std::vectors using 'x' as a
delimiter. For example, I can specify the 3-element std::vector
{10, 20, 30} as "10x20x30".
*/
class CommandLineParser
: public DataObject
{
public:
/** Standard class typedefs. */
typedef CommandLineParser Self;
typedef DataObject Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro( Self );
/** Run-time type information (and related methods). */
itkTypeMacro( CommandLineParser, DataObject );
typedef CommandLineOption OptionType;
typedef std::list<OptionType::Pointer> OptionListType;
typedef std::list<std::string> StringListType;
/**
* Interface routines
*/
OptionType::Pointer GetOption( char );
OptionType::Pointer GetOption( std::string );
bool starts_with( const std::string &, const std::string & );
int Parse( unsigned int, char * * );
bool ValidateFlag(const std::string & currentFlag);
void AddOption( OptionType::Pointer );
void PrintMenu( std::ostream& os, Indent indent, bool printShortVersion = false ) const;
itkSetStringMacro( Command );
itkGetStringMacro( Command );
itkSetStringMacro( CommandDescription );
itkGetStringMacro( CommandDescription );
OptionListType GetOptions() const
{
return this->m_Options;
}
OptionListType GetUnknownOptions() const
{
return this->m_UnknownOptions;
}
/**
* This feature is designed for a more advanced command line usage
* where multiple option values are used per stage (e.g.
* antsRegistration). Multiple option value are considered to be of
* the same stage if they are situated adjacently on the command line.
*/
void AssignStages();
template <class TValue>
TValue Convert( std::string optionString ) const
{
//Strip whitespace at end
optionString.erase(optionString.find_last_not_of(" \n\r\t")+1);
TValue value;
std::istringstream iss( optionString );
if (!(iss >> value) //Conversion did not fail
|| !( iss.peek() == EOF ) // All content parsed
)
{
std::string internalTypeName( typeid(value).name() );
itkExceptionMacro( "ERROR: Parse error occured during command line argument processing\n"
<< "ERROR: Unable to convert '" << optionString
<< "' to type '" << internalTypeName << "' as " << ConvertToHumanReadable(internalTypeName) << std::endl);
}
return value;
}
template <class TValue>
std::vector<TValue> ConvertVector( std::string optionString ) const
{
//Strip whitespace at end
optionString.erase(optionString.find_last_not_of(" \n\r\t")+1);
std::vector<std::string> optionElementString;
std::istringstream f(optionString);
std::string s;
while( std::getline(f, s, 'x'))
{
optionElementString.push_back(s);
}
std::vector< TValue > values;
for ( std::vector< std::string >::const_iterator oESit = optionElementString.begin();
oESit != optionElementString.end(); ++oESit)
{
const TValue & value = this->Convert<TValue>( *oESit );
values.push_back ( value );
}
return values;
}
protected:
CommandLineParser();
virtual ~CommandLineParser()
{
}
void PrintSelf( std::ostream& os, Indent indent ) const ITK_OVERRIDE;
private:
CommandLineParser( const Self & ); // purposely not implemented
void operator=( const Self & ); // purposely not implemented
std::vector<std::string> RegroupCommandLineArguments( unsigned int, char * * );
std::string BreakUpStringIntoNewLines( std::string, const std::string, unsigned int ) const;
void TokenizeString( std::string, std::vector<std::string> &, std::string ) const;
OptionListType m_Options;
std::string m_Command;
std::string m_CommandDescription;
OptionListType m_UnknownOptions;
char m_LeftDelimiter;
char m_RightDelimiter;
};
} // end namespace ants
} // end namespace itk
#endif
|