-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathantsCommandLineParser.h
executable file
·169 lines (138 loc) · 4.96 KB
/
antsCommandLineParser.h
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
/*=========================================================================
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 <string>
#include <vector>
namespace itk
{
namespace ants
{
/** \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 ITK_EXPORT 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 );
void Parse( unsigned int, char ** );
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;
}
template<class TValue>
TValue Convert( std::string optionString )
{
TValue value;
std::istringstream iss( optionString );
iss >> value;
return value;
}
template<class TValue>
std::vector<TValue> ConvertVector( std::string optionString )
{
std::vector<TValue> values;
std::string::size_type crosspos = optionString.find( 'x', 0 );
if( crosspos == std::string::npos )
{
values.push_back( this->Convert<TValue>( optionString ) );
}
else
{
std::string element = optionString.substr( 0, crosspos );
TValue value;
std::istringstream iss( element );
iss >> value;
values.push_back( value );
while( crosspos != std::string::npos )
{
std::string::size_type crossposfrom = crosspos;
crosspos = optionString.find( 'x', crossposfrom + 1 );
if( crosspos == std::string::npos )
{
element = optionString.substr(
crossposfrom + 1, optionString.length() );
}
else
{
element = optionString.substr( crossposfrom + 1, crosspos );
}
std::istringstream iss2( element );
iss2 >> value;
values.push_back( value );
}
}
return values;
}
protected:
CommandLineParser();
virtual ~CommandLineParser() {}
void PrintSelf( std::ostream& os, Indent indent ) const;
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