Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

processing command line parameters


  • Please log in to reply
6 replies to this topic
Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005
Since I couldn't find a function that processes command line parameters, I went to write one myself.
Not sure if it's perfect, but it seems to work fine (improvements are always welcome though):
; process command line parameters
getParams:
	Loop, %0% ; for each parameter
	{
		param := %A_Index%
		; check for switches
		StringLeft, paramType, param, 1
		If paramType = - ; switch indicator
		{
		    ; determine type of switch
		    StringMid, switch, param, 2, 1
		    ; settings file switch
			If switch = s
			{
			    ; access value (= next parameter)
				param = % A_Index + 1
				file_settings := %param%
				MsgBox, settings file:`n%file_settings%
			}
		}
	}
Return
This script checks whether there are any parameters at all, then goes on to check whether a parameter is a switch (i.e. starting with a minus char - this can easily be modified to check for slashes though) and retrieves the accompanying value (in this case, the desired settings file).

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
You're right, I don't recall seeing any others than yours posted. This kind of processing will definitely be useful.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
I shorted the function to its bare bone:
Loop, %0% {                       ;for each command line parameter

    If (%A_Index% = "/in")        ;check if known command line parameter exists

        param_in := A_Index + 1   ;assign next command line parameter as value

    Else If (%A_Index% = "/log")

        param_log := A_Index + 1

  }

Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

Ace_NoOne
  • Members
  • 299 posts
  • Last active: May 02 2008 08:19 AM
  • Joined: 10 Oct 2005

I shorted the function to its bare bone:

Loop, %0% {                       ;for each command line parameter
    If (%A_Index% = "/in")        ;check if known command line parameter exists
        param_in := A_Index + 1   ;assign next command line parameter as value
    Else If (%A_Index% = "/log")
        param_log := A_Index + 1
  }

I might be a little late with this, but I just noticed that param_log := A_Index + 1 will just assign a number, not the actual content of the respective variable.
This you will need something like
param_log := A_Index + 1
param_log := %param_log%
Anyways, I really like your solution, toralf!

PS: I've removed the If 0 > 0 part from my original code; it's redundant, as Loop, %0% does not run if there are no command line parameters.
Improving my world, one script at a time.
Join the AutoHotkey IRC channel: irc.freenode.net #autohotkey

hyz1984
  • Members
  • 22 posts
  • Last active: Mar 28 2010 07:56 PM
  • Joined: 12 Nov 2008
Hi! I have a question, how could I make a ahk (say test.ahk) keep running and have it take command line parameters, for example, execute this:

test.ahk -test1

so that the script will respond to the command line parameter?

Now I make the script quit after handling each parameter but it's really not efficient.
David H.

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
As far as I know, you can't have an AHK-script accept command line parameters while it is running. You could, however, make a 'helper' AHK-script that accepts the parameters and sends them to the other script through Send/OnMessage, or something similar.

gwarble
  • Guests
  • Last active:
  • Joined: --
you can do it without a seperate "helper" script, all inclusive, like so:

#SingleInstance Off  ;Force
#Persistent
#NoTrayIcon
#NoEnv
SetWorkingDir, %A_ScriptDir%
DetectHiddenWindows, On
OnExit, ExitShopMon

 If %1% ;Command Line
  GoSub, SMServer
 WinGet, InstanceID, List, %A_ScriptFullPath%
 Loop %InstanceID%
  If A_Index <> 1
   PostMessage, 0x1111, 1, 65134, , % "ahk_id " InstanceID%A_Index%
 OnMessage(0x1111, "AnotherInstance")

 GoSub, Initialize
 GoSub, EnableHotKeys
 SetTimer, CheckForUpdate, % RefreshTime * 1000
Return

;OnMessage 0x1111
AnotherInstance(wParam, lParam)
{
  If lParam = 65134
    GoSub, ExitShopMon
}

I'm new to ahk and don't expect that to be the best way, but its how i'm using it (fyi, smserver: ends with an exitapp, and initialize: sets the tray icon back on after #notrayicon) The loop is to send to each instance, and the lparam is arbitrary in this example

- gwarble