script command line parameters

Discuss the future of the AutoHotkey language
autocart
Posts: 218
Joined: 12 May 2014, 07:42

script command line parameters

31 Aug 2021, 17:41

Hi all devs,

In the v2 docs it still says the following:
Script Parameters: The string(s) you want to pass into the script, with each separated from the next by a space. Any parameter that contains spaces should be enclosed in quotation marks. A literal quotation mark may be passed in by preceding it with a backslash (\"). Consequently, any trailing slash in a quoted parameter (such as "C:\My Documents\") is treated as a literal quotation mark (that is, the script would receive the string C:\My Documents"). To remove such quotes, use A_Args[1] := StrReplace(A_Args[1], '"')

Source: https://lexikos.github.io/v2/docs/Scripts.htm#cmd
I was seriously hoping that this would at least be fixed in v2.
The fact that a literal quotation mark can be passed in by preceding it with a backslash is just stupid to say it in simple terms.

To use less simple terms:
If one parameter is a quoted file path and is passed with a trailing backslash, and is followed by other parameters afterwards, it just messes up the command line parameters, no matter what. StrReplace(A_Args[1], '"') does not help.

For one thing, the quoted trail-backslashed parameter and the parameter following it are both always seen as one incoming parameter. Whether or not further following parameters are also appended to that first parameter and how much more everything gets messed up depends on whether the following parameters are also quoted and also trail-backslashed.

This way it is really ugly to write a script for maybe the broad general public with, let's say, a variable number of file paths as incoming command line parameters. Even with a fixed number it is ugly. The broad general public will think that I as the programmer am stupid or lazy because I can't or won't allow trailing backslashes in the command line parameters.

The simple solution would be:
If you, the devs, would change ahk to not allow escaping literal quotation marks with a backslash. Use something else that at least can't be part of a file path. Or, afaik the double quotation mark "" already works to mean one literal quotation mark. I think that is enough.

Thanks. Regards, S.
Last edited by autocart on 01 Sep 2021, 16:56, edited 1 time in total.
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

01 Sep 2021, 05:40

Parsing C++ command-line arguments
  • ...
  • A double quote mark preceded by a backslash (\") is interpreted as a literal double quote mark (").
  • ...
You should ask Microsoft to change it. ;)
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

01 Sep 2021, 07:22

Are you saying that ahk CANNOT implement a different parser on their own?
Afaik, even I *could* do it with DllCall("GetCommandLine", "Str") but this is cumbersome for a hobby programmer, and should be unnecessarily so, especially if there already is a completely new version of ahk where I thought that the devs wanted to improve everything that is not working right.
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

01 Sep 2021, 10:53

@safetycar,

Generally, this is exactly what DllCall("GetCommandLine", "Str") does, afaik.
Compare this thread: viewtopic.php?f=6&t=4357

According to my understanding, with the function presented here: viewtopic.php?f=6&t=4357#p24670 you can even get the full command line string of another running 3rd party application and see from which path and with which command line parameters it was started.

The problem for me with these full command line strings is that they are unfinished solutions. Especially with ahk, the command line can also include various other elements that I do not want, e.g. the path to both the ahk main application, ahk switches, the path to the script, and, in case of a reload, there would be a /restart parameter. I am not sure if there could be anything else as well. And then, of course, I would still have to code the part that parses the command line without treating \" as a double quote character but still respecting that any parameter could be quoted or not.

EDIT:
Did you delete your comment again, safetycar?
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: script command line parameters

01 Sep 2021, 10:59

Yes, sorry, I thought my comment was a bit pointless knowing that the reply would be to use GetCommandLine.
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

01 Sep 2021, 11:00

Oh, ok, no problem. :D
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

01 Sep 2021, 16:35

@autocart,
you're kidding, aren't you? Why don't you want to do what's documented to solve your problem?
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

01 Sep 2021, 16:48

I don't know what you are talking about. Did I overlook something in the help or in that linked thread?
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

02 Sep 2021, 05:20

Script parameters wrote:... To remove such quotes, use A_Args[1] := StrReplace(A_Args[1], '"')
Also, to prevent such quotes use:
"C:\My Scripts Directory\\"
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

02 Sep 2021, 05:36

Now I am a little disappointed, since it seems to me that you have not even really read the description of the problem up above to begin with.

Removing literal quotes with A_Args[1] := StrReplace(A_Args[1], '"') does not help unless that parameter in question is the last one. As a general rule it is no solution.

And asking my users to ensure the existence of two trailing backslashes is even worse than telling them to ensure that none exist.
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

03 Sep 2021, 04:47

Your users shall create command line parameters on their own? :o
It would be good if they'd know how to, then.
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

03 Sep 2021, 06:53

just me wrote:
03 Sep 2021, 04:47
Your users shall create command line parameters on their own? :o
It would be good if they'd know how to, then.
Obviously we must be talking about different things. However, I don't know what you could be talking about.

Who else but the users of a software should be creating the actual command line parameters?
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

03 Sep 2021, 07:53

For example, a tiny AHK setup tool with built-in parameter check.
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

03 Sep 2021, 11:00

???? If I write an ahk tool that checks parameters, how should that tool receive them? Besides any ahk coded solution I could anyway integrate into my main tool. I think we really have to different things in mind. Please forgive me, in case I don't write back anymore.
just me
Posts: 9763
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: script command line parameters

04 Sep 2021, 12:10

Maybe I don't understand what you're talking about. You say, your users must define command line parameters to call your script. How do they do it? If they do it in the CLI window, they must the rules.

My first use of AHK were small GUI scripts to query and check the values of command line parameters and/or environment variables for CMD files. All of us were quite happy with it. Almost all errors could be avoided.
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: script command line parameters

04 Sep 2021, 13:17

autocart wrote:
02 Sep 2021, 05:36
Now I am a little disappointed, since it seems to me that you have not even really read the description of the problem up above to begin with.
instead of wasting 10 posts arguing and explaining, why dont you just post a literal specific command line example of what you want and then people can offer solutions

autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

04 Sep 2021, 15:38

guest3456 wrote:
04 Sep 2021, 13:17
instead of wasting 10 posts arguing and explaining, why dont you just post a literal specific command line example of what you want and then people can offer solutions
Thx, but I don't need a work around "solution" for one specific problem. What my request is all about is clearly layed out in the first post. Also, it's directed at the ahk devs. Other users could max help if they agree that the request makes sense.
Kind regards
autocart
Posts: 218
Joined: 12 May 2014, 07:42

Re: script command line parameters

04 Sep 2021, 15:40

just me wrote:
04 Sep 2021, 12:10
My first use of AHK were small GUI scripts to query and check the values of command line parameters and/or environment variables for CMD files. All of us were quite happy with it. Almost all errors could be avoided.
Could be a good idea for special cases. I hope I remember it if I one day have such a case.
Astoria
Posts: 4
Joined: 01 Feb 2021, 07:57

Re: script command line parameters

05 Dec 2021, 12:41

OMG, it's very ugly indeed!
The second day I'm writing this ugly code to pass and parse source and target paths from Total Commander to AutoHotkey with quotes, ending backslashes and spaces.
I'm very sorry that there is no better way...
User avatar
tank
Posts: 3164
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: script command line parameters

06 Dec 2021, 10:46

so the standard within windows of passing command line params for all windows commands isnt acceptable?. are you trolling us? this isnt an ahk defined scope. this has to do with how command line argiments are parsed. sure we could do something different but then whatever we do will be unlike anything else and i would wager piss off many experienced IT professionals. there are many so called quirks in every programing language. but command line params and escapes are darn close to universal. i think u just need more vitamin D
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 14 guests