Wie kann man einfach auf Kommandozeilenparameter zugreifen? Besonders z.B. über den Namen?
Vorschlag:
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:
| type TForm1 = class(TForm) public ParamList: TStringList; end;
procedure TForm1.FormCreate(Sender: TObject); begin ParamList := TStringList.Create; {$WARNINGS OFF} ParamList.CommaText := CmdLine; {$WARNINGS ON} if (ParamList.Count > 0) then begin Edit1.Text := ParamList.Strings[0]; ParamList.Delete(0); end; ListBox1.Items.Assign(ParamList); if (UpperCase(ParamList.Values['/SWITCH']) = 'ON') then ShowMessage('Switch ist ON!'); end;
procedure TForm1.ListBox1Click(Sender: TObject); var i: Integer; begin i := ListBox1.ItemIndex; if (i >= 0) then begin Edit2.Text := ParamList.Names[i]; Edit3.Text := ParamList.ValueFromIndex[i]; end else begin Edit2.Clear; Edit3.Clear; end; end;
procedure TForm1.FormDestroy(Sender: TObject); begin ParamList.Free; end; |
Beispiel für eine Kommandozeile:
Quelltext
1:
| test.exe /test=hallo "/welcome=hello world!" /switch=on |
Einzige "Unschönheit": wenn der Parameter-Wert Whitespaces oder Kommata enthält, muss der ganze Parameter in Anführungszeichen (siehe DOH zur Eigenschaft .CommaText), nicht nur der Wert.
Im Anhang das zugehörige Testprojekt.
There are 10 types of people - those who understand binary and those who don´t.