AutoHotkey Community

It is currently May 25th, 2012, 6:45 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: May 25th, 2007, 9:42 pm 
Offline

Joined: April 3rd, 2007, 7:42 pm
Posts: 31
Hello,

Has somebody of you already written a parser function for console parameters, like:

Code:
script.exe par1=0 par2="second parameter" par3=-445


If not, I'll write one.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 7:22 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Can you explain more? :roll:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 9:02 am 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
http://www.autohotkey.com/docs/Scripts.htm#cmd :?:

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 9:09 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
He wants to parse each parameter, ie. 1, 2, etc., separating a parameter name from its value, a bit like GNU's getopt.
A simple StringSplit can do the trick, or a regex to take care of quoted parameters.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 9:28 am 
Offline

Joined: June 15th, 2006, 6:29 am
Posts: 59
Location: Oriel College
To get standard windows argument delimitation you can use the API call CommandLineToArgvW (this is one of the only functions which only has a unicode version unfortunately), in shell32.dll.
You should beware windows' arcane treatment of quotes and backslashes though, as I have been bitten by that before...

Win32 Programmer's Reference
Code:
The CommandLineToArgvW function parses a wide-character Unicode command-line string. It returns a pointer to a set of wide-character Unicode argument strings and a count of arguments, similar to the standard C run-time argv and argc values. The function provides a way to obtain a Unicode set of argv and argc values from a Unicode command-line string.

LPWSTR *  CommandLineToArgvW(

    LPCWSTR lpCmdLine,   // pointer to a command-line string
    int *pNumArgs    // pointer to a variable that receives the argument count
   );   
 

Parameters

lpCmdLine

Pointer to a null-terminated Unicode command-line string. An application will usually directly pass on the value returned by a call to GetCommandLineW.

*pNumArgs

Pointer to an integer variable that the function sets to the count of arguments parsed.

 

Return Values

If the function succeeds, the return value is a non-NULL pointer to the constructed argument list, which is a set of Unicode wide-character argument strings.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

It is the caller's responsibility to free the memory used by the argument list when it is no longer needed. To free the memory, use a single call to either the GlobalFree or LocalFree function.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 12:59 pm 
Is this what you are looking for: processing command line parameters

Or if you want the "raw" command line with quotes, white-space and
everything else try:

Code:
commandline := DllCall( "GetCommandLine", "str" )


If I remember corrrectly, this returns the command line as typed.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 27th, 2007, 9:49 pm 
Offline

Joined: April 3rd, 2007, 7:42 pm
Posts: 31
Thanks a lot all, some very useful information here. If anybody is interested in my results, I'll post them tomorrow.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Cerberus, KenC, kwfine and 73 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group