Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Using Variables CMDLINE and CMDLINE2

Jun
1,092
48
I've tried to use those system variables in a number of ways and only get a hung TCC session or error messages such as "Variable loop".

Here's an example:

Code:
->echo begin & set arg2="%cmdline2" & echo got cmdline2
begin
TCC: Command loop " set arg2="%cmdline2" & echo got cmdline2" & echo got cmdline2" & echo got cmdline2" & echo got cmdl
ine2" & echo got cmdline2" & echo got cmdline2" & echo got cmdline2" & echo got cmdline2" & echo got cmdline2" & echo g

Here's another try.

Code:
->setdos /x-5
->echo %@execarray[temparray,echo "%cmdline2"]
TCC: Variable loop
->echo %@execarray[temparray,@echo "%cmdline2"]
TCC: Variable loop
->@echo %@execarray[temparray,@echo "%cmdline2"]
TCC: Variable loop

I tried unsuccessfully to run similar code in batch scripts. What am I missing about how to use those variables?
 
What you're missing is that _cmdline is intended for use by key aliases that want to parse the current command line. Trying to echo it to the current command line is pointless., though users periodically try it for reasons that have never been clear to me.

And _cmdline2 is an environment variable (not an internal variable) that is intended to be used by external apps.
 
What you're missing is that _cmdline is intended for use by key aliases that want to parse the current command line. Trying to echo it to the current command line is pointless., though users periodically try it for reasons that have never been clear to me.

That really didn't answer my question. Per the title of the post and my examples, I was not talking about the _CMDLINE variable but the two variables without leading underscores that are listed in the help under "System Variables". They are described as follows:

CMDLINE is set by TCC to the fully expanded text of the currently executing command line just before invoking any external command (.EXE, .BTM, .BAT or .CMD), unless the command line is prefaced with @ to prevent echoing, in which case CMDLINE will be removed.​
CMDLINE2 is set by TCC to the original unexpanded command line before doing any alias expansion, variable expansion, compound command processing, etc.​

I was writing a script that wanted to see what the user had actually typed on the command line, before TCC did any expansions and evaluations. CMDLINE2 looked as though it was exactly what I wanted.

From reading the help on the SET command, I saw that another way to see the values of those commands was to run set cmd*. That did not hang TCC or produce an error message.

Code:
->set cmd*
CMDLINE=c:\commands\bat\cdtests.btm
CMDLINE2=set cmd*

The value of CMDLINE2 makes sense, but where did the value of CMDLINE come from. It is not the fully expanded form of the command line reported by CMDLINE2. I then displayed the file cdtests.btm and ran the set command again.

Code:
->set cmd*
CMDLINE=c:\commands\bat\v_fileviewer.btm C:\commands\bat\cdtests.btm
CMDLINE2=set cmd*

So it looks as though CMDLINE has the fully expanded version of the previous command line, not the current one.

These observations do not help me understand why running set arg=%cmdline2 causes TCC to hang.
 
And _cmdline2 is an environment variable (not an internal variable) that is intended to be used by external apps.

I was not aware of the environment variable _CMDLINE2, and it does not seem to exist on my system. Nor could I find it in the help.

Code:
->set _cmd*
TCC: Not in environment "_cmd*"
 
Now I experimented with using _CMDLINE instead of CMDLINE2 in my batch file. Here's the code:

Code:
setdos /x-5
set arg=%@quote[%_cmdline] & echo saved _cmdline
echo arg=%arg
setdos /x0
quit

The result was:

Code:
->test.btm
arg=test.btm & echo saved _cmdline

So apparently the value of _cmdline was test.btm, the command that invoked the batch file, not any of the commands run in the batch file. Oh, but if I run it again, the output is different.

Code:
->test.btm
arg= & echo saved _cmdline

I had the setdos /x-5 command there when I was experimenting with multiple commands on the command line. For the above test, it should not have mattered, so I removed it. Then I discovered the following.

Code:
->echo running test.btm & test.btm
running test.btm
saved _cmdline
arg="echo running test.btm & test.btm "

->echo running test.btm & test.btm
running test.btm
saved _cmdline
arg=

Variable arg got set to the command line the first time, but never again. Repeating the command line (which I was doing from the command hisotry) always produced "arg=" with no value. If I changed the command line, arg got the value, but again only the first time, not when I reran the command line (from the history).

Code:
->echo running test.btm again & test.btm
running test.btm again
saved _cmdline
arg="echo running test.btm again & test.btm "

If I edit the command line, even if the final result is the same, then arg does get the value. There are clearly some subtleties to the _cmdline variable. It seems to know whether the command line was run from the history or was newly typed. When I ran any version of that command line from the history, the variable was empty. When I typed the command, it had the value.
 
Back
Top