bestlong 怕失憶論壇

 

 

搜索
bestlong 怕失憶論壇 論壇 Delphi 分析命令列參數
查看: 3309|回復: 0
go

分析命令列參數 [複製鏈接]

Rank: 9Rank: 9Rank: 9

1#
發表於 2009-12-17 11:15 |只看該作者 |倒序瀏覽 |打印
Delphi's ParamStr and ParamCount functions are designed to help operate the parameters (command-line arguments) passed to the application.

Due to the way ParamStr function is implemented in Delphi, parameters passed using double quotes ("") will not be parsed correctly (quotes will be removed).

The "CmdLineHelper" unit provides 3 custom Delphi functions to overcome the problem of the RTL's ParamStr implementation.
  1. //original code author: Remy Lebeau (TeamB)
  2. unit CmdLineHelper;

  3. interface

  4. uses windows;

  5. //command line parameters + program path
  6. function GetCommandLine: string;
  7. //number of parameters
  8. function GetParamCount: Integer;
  9. //parameter by index
  10. function GetParamStr(Index: Integer): String;

  11. implementation

  12. function GetCommandLine : string;
  13. begin
  14.   result := windows.GetCommandLine;
  15. end;

  16. function GetNextParam(var CmdLine: PChar; Buffer: PChar; Len: PInteger): Boolean;
  17. var
  18.   InQuotedStr, IsOdd: Boolean;
  19.   NumSlashes, NewLen, cnt: Integer;
  20. begin
  21.   Result := False;
  22.   if Len <> nil then Len^ := 0;
  23.   if CmdLine = nil then Exit;
  24.   while (CmdLine^ <= ' ') and (CmdLine^ <> #0) do CmdLine := CharNext(CmdLine) ;
  25.   if CmdLine^ = #0 then Exit;
  26.   InQuotedStr := False;
  27.   NewLen := 0;
  28.   repeat
  29.     if CmdLine^ = '\' then
  30.     begin
  31.       NumSlashes := 0;
  32.       repeat
  33.         Inc(NumSlashes) ;
  34.         CmdLine := CharNext(CmdLine) ;
  35.       until CmdLine^ <> '\';
  36.       if CmdLine^ = '"' then
  37.       begin
  38.         IsOdd := (NumSlashes mod 2) <> 0;
  39.         NumSlashes := NumSlashes div 2;
  40.         Inc(NewLen, NumSlashes) ;
  41.         if IsOdd then Inc(NewLen) ;
  42.         if Buffer <> nil then
  43.         begin
  44.           for cnt := 0 to NumSlashes-1 do
  45.           begin
  46.             Buffer^ := '\';
  47.             Inc(Buffer) ;
  48.           end;
  49.           if IsOdd then
  50.           begin
  51.             Buffer^ := '"';
  52.             Inc(Buffer) ;
  53.           end;
  54.         end;
  55.         if IsOdd then CmdLine := CharNext(CmdLine) ;
  56.       end else
  57.       begin
  58.         Inc(NewLen, NumSlashes) ;
  59.         if Buffer <> nil then
  60.         begin
  61.           for cnt := 0 to NumSlashes-1 do
  62.           begin
  63.             Buffer^ := '\';
  64.             Inc(Buffer) ;
  65.           end;
  66.         end;
  67.       end;
  68.       Continue;
  69.     end;
  70.     if CmdLine^ <> '"' then
  71.     begin
  72.       if (CmdLine^ <= ' ') and (not InQuotedStr) then Break;
  73.       Inc(NewLen) ;
  74.       if Buffer <> nil then
  75.       begin
  76.         Buffer^ := CmdLine^;
  77.         Inc(Buffer) ;
  78.       end;
  79.     end
  80.     else
  81.       InQuotedStr := not InQuotedStr;
  82.     CmdLine := CharNext(CmdLine) ;
  83.   until CmdLine^ = #0;
  84.   if Len <> nil then Len^ := NewLen;
  85.   Result := True;
  86. end;

  87. function GetParamCount: Integer;
  88. var
  89.   CmdLine: PChar;
  90. begin
  91.   Result := 0;
  92.   CmdLine := windows.GetCommandLine;
  93.   GetNextParam(CmdLine, nil, nil) ;
  94.   while GetNextParam(CmdLine, nil, nil) do Inc(Result) ;
  95. end;

  96. function GetParamStr(Index: Integer): String;
  97. var
  98.   Buffer: array[0..MAX_PATH] of Char;
  99.   CmdLine, P: PChar;
  100.   Len: Integer;
  101. begin
  102.   Result := '';
  103.   if Index <= 0 then
  104.   begin
  105.     Len := GetModuleFileName(0, Buffer, MAX_PATH+1) ;
  106.     SetString(Result, Buffer, Len) ;
  107.   end else
  108.   begin
  109.     CmdLine := windows.GetCommandLine;
  110.     GetNextParam(CmdLine, nil, nil) ;
  111.     repeat
  112.       Dec(Index) ;
  113.       if Index = 0 then Break;
  114.       if not GetNextParam(CmdLine, nil, nil) then Exit;
  115.     until False;
  116.     P := CmdLine;
  117.     if GetNextParam(P, nil, @Len) then
  118.     begin
  119.       SetLength(Result, Len) ;
  120.       GetNextParam(CmdLine, PChar(Result), nil) ;
  121.     end;
  122.   end;
  123. end;

  124. end.
複製代碼
Note: for testing, you can pass the parameter from the IDE under Run-Parameters menu option.

The screen shot displays the output when the application is called using:

/about delphi "programming" 123.45 67,89
Drop a TMemo ("Memo1") on a Delphi form. Here's a sample usage of the "CmdLineHelper" unit:
  1. uses CmdLineHelper;
  2. ...
  3. var
  4.   idx : integer;
  5. begin
  6.   with Memo1.Lines do
  7.   begin
  8.     Clear;

  9.     Add('CMD Line: ' + CmdLineHelper.GetCommandLine + #13#10) ;
  10.     Add('Number of params: ' + IntToStr(CmdLineHelper.GetParamCount) + #13#10) ;

  11.     for idx := 1 to CmdLineHelper.GetParamCount do
  12.     begin
  13.       Memo1.Lines.Add(CmdLineHelper.GetParamStr(idx)) ;
  14.     end;
  15.   end;
  16. end;
複製代碼
參考來源 http://delphi.about.com/od/delphitips2007/qt/parse_cmd_line.htm
我是雪龍
http://blog.bestlong.idv.tw
http://www.bestlong.idv.tw
‹ 上一主題|下一主題

Archiver|怕失憶論壇

GMT+8, 2024-5-1 00:43 , Processed in 0.013629 second(s), 10 queries .

Powered by Discuz! X1.5

© 2001-2010 Comsenz Inc.