-
Notifications
You must be signed in to change notification settings - Fork 0
/
UCommandlineHandler.pas
182 lines (160 loc) · 5.94 KB
/
UCommandlineHandler.pas
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
{* Copyright (C) 2010-2011 Karl-Michael Schindler
*
* This file is part of Heat Wizard.
*
* Heat Wizard is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* Heat Wizard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Heat Wizard; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/> or write to the
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*}
unit UCommandlineHandler;
{$mode objfpc}
{$H+}
interface
uses
Classes;
type
TValueType = (TNone,
TRealOptional, TIntegerOptional, TCharOptional,
TRealMust, TIntegerMust, TCharMust
);
TCommandlineHandler = class
public
Debug: boolean;
constructor Create;
destructor Destroy;
procedure AddOption(const NewShortOption: string);
procedure AddOption(const NewShortOption: string; const NewValueType: TValueType);
procedure AddOption(const NewShortOption: string; const NewLongOption: string);
procedure AddOption(const NewShortOption: string; const NewLongOption: string; const NewValueType: TValueType);
procedure AddOption(const NewShortOption: string; const NewLongOption: string; const NewDefaultValue: string; const NewValueType: TValueType);
function GetOptionIsSet(const query: string): boolean;
function GetOptionIsSet(const queryChar: char; const queryString: string): boolean;
function GetOptionValue(const query: string): string;
function GetOptionValue(const queryChar: char; const queryString: string): string;
function CheckOptions: string;
private
ShortOption: string;
LongOption: TStringList;
DefaultValue: TStringList;
Value: TStringList;
ValueType: array of TValueType;
OptionIsSet: array of boolean;
end;
implementation
uses
CustApp, StrUtils, SysUtils;
var
HeatWizardApplication: TCustomApplication;
constructor TCommandlineHandler.Create;
begin
Inherited;
Debug := false;
LongOption := TStringList.Create;
DefaultValue := TStringList.Create;
Value := TStringList.Create;
HeatWizardApplication := TCustomApplication.Create(Nil);
end;
destructor TCommandlineHandler.Destroy;
begin
Inherited;
HeatWizardApplication.Destroy;
end;
function TCommandlineHandler.GetOptionIsSet(const query: string): boolean;
begin
Result := HeatWizardApplication.HasOption(query);
end;
function TCommandlineHandler.GetOptionIsSet(const queryChar: char; const queryString: string): boolean;
begin
Result := HeatWizardApplication.HasOption(queryChar, queryString);
end;
function TCommandlineHandler.GetOptionValue(const query: string): string;
begin
Result := HeatWizardApplication.GetOptionValue(query);
end;
function TCommandlineHandler.GetOptionValue(const queryChar: char; const queryString: string): string;
begin
Result := HeatWizardApplication.GetOptionValue(queryChar, queryString);
end;
function TCommandlineHandler.CheckOptions: string;
var
index: integer;
valueString: string;
integerTest: integer;
realTest: real;
begin
Result := HeatWizardApplication.CheckOptions(ShortOption, LongOption);
if Result = '' then
for index := 0 to LongOption.Count - 1 do
begin
valueString := HeatWizardApplication.GetOptionValue(DelChars(ShortOption, ':')[index+1], Copy2Symb(LongOption[index], ':'));
if valueString <> '' then
begin
case valueType[index] of
TRealOptional, TRealMust: if not (TryStrToFloat(valueString, realTest)) then
Result := 'Error: "' + valuestring + '" could not be converted to a real number.';
TIntegerOptional, TIntegerMust : if not (TryStrToInt(valueString, integerTest)) then
Result := 'Error: "' + valuestring + '" could not be converted to an integer number.';
end;
end;
end;
end;
procedure TCommandlineHandler.AddOption(const NewShortOption: string);
begin
AddOption(NewShortOption, '', '', TNone);
end;
procedure TCommandlineHandler.AddOption(const NewShortOption: string; const NewValueType: TValueType);
begin
AddOption(NewShortOption, '', '', NewValueType);
end;
procedure TCommandlineHandler.AddOption(const NewShortOption: string; const NewLongOption: string);
begin
AddOption(NewShortOption, NewLongOption, '', TNone);
end;
procedure TCommandlineHandler.AddOption(const NewShortOption: string; const NewLongOption: string; const NewValueType: TValueType);
begin
AddOption(NewShortOption, NewLongOption, '', NewValueType);
end;
procedure TCommandlineHandler.AddOption(const NewShortOption: string; const NewLongOption: string; const NewDefaultValue: string;const NewValueType: TValueType);
begin
case NewValueType of
TRealMust, TIntegerMust, TCharMust:
begin
ShortOption := ShortOption + NewShortOption[2] + ':';
LongOption.Add(copy(NewLongOption,3,Length(NewLongOption)-2) + ':');
end;
TRealOptional, TIntegerOptional, TCharOptional:
begin
ShortOption := ShortOption + NewShortOption[2] + '::';
LongOption.Add(copy(NewLongOption,3,Length(NewLongOption)-2) + '::');
end;
else
begin
ShortOption := ShortOption + NewShortOption[2];
LongOption.Add(copy(NewLongOption,3,Length(NewLongOption)-2));
end;
end;
DefaultValue.Add(NewDefaultValue);
Value.Add(NewDefaultValue);
SetLength(ValueType, Length(ValueType) + 1);
ValueType[high(ValueType)] := NewValueType;
SetLength(OptionIsSet, Length(OptionIsSet) + 1);
if NewDefaultValue <> '' then
OptionIsSet[high(OptionIsSet)] := true;
end;
end.