Skip to content

Commit 4602cf2

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/3-code-gen-tool
2 parents 181c7d0 + 3245584 commit 4602cf2

4 files changed

Lines changed: 83 additions & 56 deletions

File tree

docs/api-reference.md

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,14 @@ Each helper method:
121121

122122
#### Getting Parameter Values
123123

124-
`TBaseCommand` provides overloaded `GetParameterValue` methods for type safety:
124+
`TBaseCommand` currently exposes a single `GetParameterValue` overload:
125125

126126
```pascal
127127
function GetParameterValue(const Flag: string; out Value: string): Boolean;
128-
function GetParameterValue(const Flag: string; out Value: Integer): Boolean;
129-
function GetParameterValue(const Flag: string; out Value: Double): Boolean;
130-
function GetParameterValue(const Flag: string; out Value: Boolean): Boolean;
131128
```
132-
- Returns `True` if the parameter was provided or has a default value.
133-
- Automatically converts to the correct type and raises an error if the value is invalid.
129+
- For non-boolean parameters, it returns `True` when the parameter was provided or a default value exists.
130+
- For boolean parameters, it writes the string value to `Value` and returns `True` only when the flag/value was explicitly provided.
131+
- Convert string results yourself with helpers such as `TryStrToInt`, `TryStrToFloat`, and `SameText`.
134132

135133
Example usage:
136134
```pascal
@@ -142,36 +140,35 @@ type
142140
143141
function TTestCommand.Execute: Integer;
144142
var
145-
Name: string;
143+
Name, CountStr, RateStr, Level, VerboseStr, DateStr, Url, Tags, ApiKey: string;
146144
Count: Integer;
147145
Rate: Double;
148-
Level: string;
149146
begin
150-
// Get parameter values using helper methods
147+
// Get parameter values using the string-based helper
151148
if GetParameterValue('--name', Name) then
152149
WriteLn('Name: ', Name);
153-
154-
if GetParameterValue('--count', Count) then
150+
151+
if GetParameterValue('--count', CountStr) and TryStrToInt(CountStr, Count) then
155152
WriteLn('Count: ', Count);
156-
157-
if GetParameterValue('--rate', Rate) then
153+
154+
if GetParameterValue('--rate', RateStr) and TryStrToFloat(RateStr, Rate) then
158155
WriteLn('Rate: ', Rate:0:2);
159-
156+
160157
if GetParameterValue('--level', Level) then
161158
WriteLn('Level: ', Level);
162159
163-
if GetParameterValue('--verbose', Verbose) then
164-
WriteLn('Verbose: ', Verbose);
165-
160+
if GetParameterValue('--verbose', VerboseStr) and SameText(VerboseStr, 'true') then
161+
WriteLn('Verbose: true');
162+
166163
if GetParameterValue('--date', DateStr) then
167164
WriteLn('Date: ', DateStr);
168-
165+
169166
if GetParameterValue('--url', Url) then
170167
WriteLn('URL: ', Url);
171-
168+
172169
if GetParameterValue('--tags', Tags) then
173170
WriteLn('Tags: ', Tags);
174-
171+
175172
if GetParameterValue('--api-key', ApiKey) then
176173
WriteLn('API Key: ', ApiKey);
177174
@@ -499,7 +496,7 @@ type
499496
500497
function TCopyCommand.Execute: Integer;
501498
var
502-
Source, Dest: string;
499+
Source, Dest, ForceValue: string;
503500
Force: Boolean;
504501
begin
505502
// Get required parameters
@@ -515,8 +512,9 @@ begin
515512
Exit(1);
516513
end;
517514
518-
// Get optional parameter
519-
Force := GetParameterValue('--force', Force);
515+
// Get optional boolean flag as text, then interpret it
516+
GetParameterValue('--force', ForceValue);
517+
Force := SameText(ForceValue, 'true');
520518
521519
// Show operation details
522520
TConsole.WriteLn('Copying file:', ccCyan);
@@ -561,17 +559,19 @@ type
561559
562560
function TTestCommand.Execute: Integer;
563561
var
562+
ForceValue, VerboseValue: string;
564563
IsForced, IsVerbose: Boolean;
565564
begin
566-
// Get flag value (true when present)
567-
GetParameterValue('--force', IsForced);
565+
// Get boolean results as strings, then interpret them
566+
GetParameterValue('--force', ForceValue);
567+
IsForced := SameText(ForceValue, 'true');
568568
if IsForced then
569569
TConsole.WriteLn('Force flag is enabled', ccGreen)
570570
else
571571
TConsole.WriteLn('Force flag is disabled', ccYellow);
572572
573-
// Get boolean value (explicit true/false)
574-
GetParameterValue('--verbose', IsVerbose);
573+
GetParameterValue('--verbose', VerboseValue);
574+
IsVerbose := SameText(VerboseValue, 'true');
575575
if IsVerbose then
576576
TConsole.WriteLn('Verbose mode is ON', ccGreen)
577577
else
@@ -627,13 +627,16 @@ type
627627
628628
function TProcessCommand.Execute: Integer;
629629
var
630+
CountStr, VerboseStr: string;
630631
Count: Integer;
631632
Verbose: Boolean;
632633
Progress: IProgressIndicator;
633634
i: Integer;
634635
begin
635-
GetParameterValue('--count', Count);
636-
GetParameterValue('--verbose', Verbose);
636+
GetParameterValue('--count', CountStr);
637+
TryStrToInt(CountStr, Count);
638+
GetParameterValue('--verbose', VerboseStr);
639+
Verbose := SameText(VerboseStr, 'true');
637640
638641
Progress := CreateProgressBar('Processing files', Count);
639642
try
@@ -692,13 +695,14 @@ type
692695
693696
function TValidateCommand.Execute: Integer;
694697
var
695-
Path: string;
698+
Path, StopOnErrorValue: string;
696699
StopOnError: Boolean;
697700
ErrorCount: Integer;
698701
i: Integer;
699702
begin
700703
GetParameterValue('--path', Path);
701-
GetParameterValue('--stop-on-error', StopOnError);
704+
GetParameterValue('--stop-on-error', StopOnErrorValue);
705+
StopOnError := SameText(StopOnErrorValue, 'true');
702706
ErrorCount := 0;
703707
704708
for i := 1 to 10 do

docs/technical-docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ classDiagram
6464
+AddSubCommand(Command: ICommand)
6565
+SetParsedParams(Params: TStringList)
6666
+Execute(): Integer
67-
#GetParameterValue(Flag: string): Boolean
67+
#GetParameterValue(Flag: string, out Value: string): Boolean
6868
}
6969
7070
class TCommandParameter {

docs/user-manual.md

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,24 @@ Cmd.AddUrlParameter('-u', '--url', 'Description', True);
5959
### Get Parameter Values
6060
```pascal
6161
var
62-
StrValue: string;
62+
StrValue, IntValueStr, FloatValueStr, BoolValueStr: string;
6363
IntValue: Integer;
6464
FloatValue: Double;
6565
BoolValue: Boolean;
6666
begin
67-
// Returns True if parameter exists or has default value
67+
// For non-boolean parameters, returns True when a value or default exists
6868
if GetParameterValue('--param-name', StrValue) then
6969
// Use StrValue...
7070
71-
// Framework automatically converts to correct type
72-
GetParameterValue('--count', IntValue);
73-
GetParameterValue('--rate', FloatValue);
74-
GetParameterValue('--verbose', BoolValue);
71+
// Retrieve text, then convert explicitly
72+
if GetParameterValue('--count', IntValueStr) then
73+
TryStrToInt(IntValueStr, IntValue);
74+
75+
if GetParameterValue('--rate', FloatValueStr) then
76+
TryStrToFloat(FloatValueStr, FloatValue);
77+
78+
GetParameterValue('--verbose', BoolValueStr);
79+
BoolValue := SameText(BoolValueStr, 'true');
7580
end;
7681
```
7782

@@ -752,21 +757,20 @@ To retrieve parameter values in your command's Execute method:
752757
```pascal
753758
function TMyCommand.Execute: Integer;
754759
var
755-
Name: string;
760+
Name, CountStr, RateStr, Level: string;
756761
Count: Integer;
757762
Rate: Double;
758-
Level: string;
759763
begin
760764
// Get parameter values with error checking
761765
if GetParameterValue('--name', Name) then
762766
WriteLn('Name: ', Name);
763-
764-
if GetParameterValue('--count', Count) then
767+
768+
if GetParameterValue('--count', CountStr) and TryStrToInt(CountStr, Count) then
765769
WriteLn('Count: ', Count);
766-
767-
if GetParameterValue('--rate', Rate) then
770+
771+
if GetParameterValue('--rate', RateStr) and TryStrToFloat(RateStr, Rate) then
768772
WriteLn('Rate: ', Rate:0:2);
769-
773+
770774
if GetParameterValue('--level', Level) then
771775
WriteLn('Level: ', Level);
772776
@@ -776,14 +780,24 @@ end;
776780

777781
### Best Practices
778782

779-
1. **Always Check Return Value**: The `GetParameterValue` function returns `False` if the parameter wasn't provided and has no default value.
783+
1. **Always Check Return Value**: `GetParameterValue` returns `True` for non-boolean parameters when a value or default exists. Boolean parameters still write to the output string, but return `True` only when the flag/value was explicitly provided.
780784

781-
2. **Use Strong Typing**: The framework will automatically convert parameter values to the correct type:
785+
2. **Convert Retrieved Strings Explicitly**: `GetParameterValue` returns strings, so use `SysUtils` helpers after reading the value:
782786
```pascal
783787
var
784-
Count: Integer;
785-
Rate: Double;
786-
IsEnabled: Boolean;
788+
CountValue, RateValue, EnabledValue: string;
789+
Count: Integer;
790+
Rate: Double;
791+
IsEnabled: Boolean;
792+
793+
GetParameterValue('--count', CountValue);
794+
TryStrToInt(CountValue, Count);
795+
796+
GetParameterValue('--rate', RateValue);
797+
TryStrToFloat(RateValue, Rate);
798+
799+
GetParameterValue('--enabled', EnabledValue);
800+
IsEnabled := SameText(EnabledValue, 'true');
787801
```
788802

789803
3. **Provide Clear Descriptions**: Parameter descriptions appear in help text:
@@ -997,19 +1011,24 @@ Cmd.AddUrlParameter('-u', '--url', 'Description', True);
9971011
### Get Parameter Values
9981012
```pascal
9991013
var
1000-
StrValue: string;
1014+
StrValue, IntValueStr, FloatValueStr, BoolValueStr: string;
10011015
IntValue: Integer;
10021016
FloatValue: Double;
10031017
BoolValue: Boolean;
10041018
begin
1005-
// Returns True if parameter exists or has default value
1019+
// For non-boolean parameters, returns True when a value or default exists
10061020
if GetParameterValue('--param-name', StrValue) then
10071021
// Use StrValue...
10081022
1009-
// Framework automatically converts to correct type
1010-
GetParameterValue('--count', IntValue);
1011-
GetParameterValue('--rate', FloatValue);
1012-
GetParameterValue('--verbose', BoolValue);
1023+
// Retrieve text, then convert explicitly
1024+
if GetParameterValue('--count', IntValueStr) then
1025+
TryStrToInt(IntValueStr, IntValue);
1026+
1027+
if GetParameterValue('--rate', FloatValueStr) then
1028+
TryStrToFloat(FloatValueStr, FloatValue);
1029+
1030+
GetParameterValue('--verbose', BoolValueStr);
1031+
BoolValue := SameText(BoolValueStr, 'true');
10131032
end;
10141033
```
10151034

src/cli.application.pas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,10 @@ function TCLIApplication.ValidateParameterValue(const Param: ICommandParameter;
994994
Result := False;
995995
end;
996996
end;
997+
ptString,
998+
ptPath,
999+
ptArray,
1000+
ptPassword: ; // no validation
9971001
end;
9981002
end;
9991003

0 commit comments

Comments
 (0)