-
Notifications
You must be signed in to change notification settings - Fork 2
/
CmdLineParser.cpp
182 lines (160 loc) · 5.81 KB
/
CmdLineParser.cpp
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
//---------------------------------------------------------------------------
#pragma hdrstop
#include <System.SysUtils.hpp>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <memory>
#include "CmdLineParser.h"
#include "RegexDefs.h"
using std::endl;
using std::max;
using std::setw;
using std::make_unique;
using std::lower_bound;
using std::get;
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
namespace CmdLineParser {
//---------------------------------------------------------------------------
struct CmpName {
template<typename T1, typename T2>
bool operator()( T1 const & Lhs, T2 const & Rhs ) const {
return GetName( Lhs ) < Rhs;
}
};
void ParseCmdLine( OptionType* const Options, size_t OptionCount )
{
regex_type re( _T( "^\\s*[-\?]+([^=\\s-]+)(\?:=([^\\s]+))\?\\s*$" ) );
for ( int Idx = 1 ; Idx <= ParamCount() ; ++Idx ) {
String const Param = ParamStr( Idx ).c_str();
cmatch_type m;
if ( regex_match( Param.c_str(), m, re ) ) {
OptionType* const it =
lower_bound(
Options,
Options + OptionCount,
String( m[1].str().c_str(), m[1].str().length() ),
CmpName()
);
if ( it != Options + OptionCount ) {
if ( GetName( *it ) == m[1].str().c_str() ) {
if ( WasFound( *it ) ) {
throw Exception(
_T( "Duplicated option \'%s\'" ),
ARRAYOFCONST(( ParamStr( Idx ) ))
);
}
get<ToIdx( OptPar::Found )>( *it ) = true;
if ( IsValueRequired( *it ) ) {
if ( !m[2].matched ) {
throw Exception(
_T( "Option \'%s\' requires a value" ),
ARRAYOFCONST((
GetName( *it )
))
);
}
get<ToIdx( OptPar::Value )>( *it ) = m[2].str().c_str();
}
continue;
}
}
throw Exception(
_T( "Invalid option \'%s\'" ),
ARRAYOFCONST(( ParamStr( Idx ) ))
);
}
else {
throw Exception(
_T( "Invalid parameter \'%s\'" ),
ARRAYOFCONST(( ParamStr( Idx ) ))
);
}
}
}
//---------------------------------------------------------------------------
void Validate( OptionType const * const Options, size_t OptionCount )
{
for ( OptionType const * it = Options ; it != Options + OptionCount ; ++it ) {
if ( IsMandatory( *it ) && IsTextEmpty( GetValue( *it ) ) ) {
throw Exception(
_T( "Option \'%s\' is mandatory" ),
ARRAYOFCONST(( GetName( *it ) ))
);
}
}
}
//---------------------------------------------------------------------------
String StripFileExt( String Val )
{
regex_type re( _T( "^(.*\?).\\w+$" ) );
cmatch_type m;
if ( regex_match( Val.c_str(), m, re ) ) {
return m[1].str().c_str();
}
return Val;
}
//---------------------------------------------------------------------------
String GetHelpText( OptionType const * const Options, size_t OptionCount )
{
auto SB = make_unique<TStringBuilder>();
size_t MaxParWidth = 0;
SB->AppendLine( _T( "Usage:" ) );
SB->Append( LowerCase( StripFileExt( ExtractFileName( String( ParamStr( 0 ) ) ) ) ) );
for ( OptionType const * Opt = Options ; Opt != Options + OptionCount ; ++Opt ) {
SB->Append( _T( ' ' ) );
if ( !IsMandatory( *Opt ) ) {
SB->Append( _T( '[' ) );
}
SB->Append( String( _T( "-" ) ) );
SB->Append( GetName( *Opt ) );
if ( IsValueRequired( *Opt ) ) {
SB->AppendFormat( _T( "={%s}" ),
ARRAYOFCONST(( GetValueDesc( *Opt ) )) );
}
if ( !IsMandatory( *Opt ) ) {
SB->Append( _T( ']' ) );
}
MaxParWidth = max( MaxParWidth, GetTextLen( GetName( *Opt ) ) );
}
SB->AppendLine();
SB->AppendLine();
SB->AppendLine( _T( "Parameters:" ) );
for ( OptionType const * Opt = Options ; Opt != Options + OptionCount ; ++Opt ) {
SB->AppendFormat(
_T( " -%s" ), ARRAYOFCONST(( GetName( *Opt ) ))
);
if ( IsValueRequired( *Opt ) && !IsTextEmpty( GetValueDesc( *Opt ) ) ) {
SB->AppendFormat(
_T( " = %s" ), ARRAYOFCONST(( GetValueDesc( *Opt ) ))
);
}
SB->AppendFormat( _T( " ; %s" ), ARRAYOFCONST(( GetDesc( *Opt ) )) );
if ( !IsTextEmpty( GetValueDesc( *Opt ) ) ) {
SB->AppendLine();
SB->AppendFormat(
_T( " %s" ), ARRAYOFCONST(( GetValueDesc( *Opt ) ))
);
if( !IsTextEmpty( GetValueLongDesc( *Opt ) ) ) {
SB->AppendFormat(
_T( ": %s" ), ARRAYOFCONST(( GetValueLongDesc( *Opt ) ))
);
}
}
if ( !IsTextEmpty( GetValue( *Opt ) ) ) {
SB->AppendLine();
SB->AppendFormat(
_T( " default=%s" ),
ARRAYOFCONST(( GetValue( *Opt ) ))
);
}
SB->AppendLine();
SB->AppendLine();
}
return SB->ToString();
}
//---------------------------------------------------------------------------
}; // End of namespace CmdLineParser
//---------------------------------------------------------------------------