Command grouping and GOTO label

Jun 29, 2008
3
0
Consider this batch program, stored in "test.btm":

@echo off

if "%1" == "" goto helplabel

if "%1" == "help" (
:helplabel
echo this
echo is
echo helpful
goto end
)

:end


With CMD.EXE, both "test" and "test help" produce the same 3-line output. With TC11 (TCC 11.00.44), "test" produces the 3 lines, but "test help" produces no output.

It looks like TC11 doesn't like the occurrence of a label within a command group. There's no mention of this in the "Command Grouping" documentation. In the GOTO command description, there's a discussion of "to avoid errors in the processing of nested statements", but there's no specific mention of command grouping.

Is this a doc omission?

Notes:

* Removing the "goto end" statement and the :end label produces an error: Unknown command ")"

* I realize that it would be better to implement this using GOSUB. This is code written by someone else and targeted at CMD.EXE, which I'm executing under TC11.

Tx,
John
 
Jun 29, 2008
3
0
Remember that a command group is a single line, so that works out to be the same as:

Code:
if "%1" == "help" :helplabel echo this echo is echo helpful goto end
If the first argument is help, then do nothing....

Thanks, Charles, but I'm not convinced. :) Your explanation doesn't account for the error I got when removing the "goto end" statement. And it doesn't explain the behavior of this script:

Code:
if "%1" == "help" (
echo MARK 1
echo MARK 2
:foobar
echo MARK 3
)
Script invocation:

Code:
> [B]test2 help[/B]
MARK 1
MARK 2

>
It seems to me that the presence of the label inside the command group somehow "breaks the group". That would explain why removing the "goto end" statement caused an error: the script thought it was no longer inside a command group, but then it encountered the closing parenthesis, and so generated the error ... Unknown command ")"
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,425
85
Albuquerque, NM
prospero.unm.edu
It seems to me that the presence of the label inside the command group somehow "breaks the group".

Precisely! All the lines after the label wind up on the same logical line, therefore part of the label and ignored.

I don't really know what would be the "correct" way to handle such a peculiar construct. Perhaps an immediate error message when the parser encounters a label inside a command group...? Or maybe just ignore the label altogether.

I can't speak for Rex, but strongly suspect that mimicking CMD.EXE's behavior in the first situation (jumping into the middle of a command group) would not be feasible without a complete rewrite of the parser.
 
May 20, 2008
11,292
95
Syracuse, NY, USA
On Tue, 23 Mar 2010 19:00:35 -0400, Charles Dye <> wrote:

|---Quote (Originally by John Posner)---
|It seems to me that the presence of the label inside the command group somehow "breaks the group".
|---End Quote---

It doesn't answer the questions, especially the CMD compatibility one, but using
the much more powerful IFF/ENDIFF would, I believe, make all the problems go
away. I've never needed CMD compatibility and wouldn't mind if IF simply went
away.
--
- Vince
 
May 20, 2008
3,515
4
Elkridge, MD, USA
| It doesn't answer the questions, especially the CMD compatibility
| one, but using the much more powerful IFF/ENDIFF would, I believe,
| make all the problems go away.

Agreed.

| I've never needed CMD compatibility

Nor I - I have never needed to code for CMD, and do not know its intricacies
or foibles.

| and wouldn't mind if IF simply went away.

That I would mind - I would hate to convert all my "IF x DO y" statements
(with DO actually just implied, not physically present) to "IFF x then %+ y
%+ ENDIFF" multiliners. However, I never use multiline command groups.
Either they fit on a single physical line, or they are already written with
IFF / ENDIFF.

Luckily, I have serious doubts Rex would ever do such a thing.
--
Steve
 
Jun 29, 2008
3
0
Thank you all for your helpful comments!

Does anyone think that this situation merits a sentence in the Command Grouping section, stating that labels shouldn't be used in a command group?

Tx,
John
 
May 20, 2008
3,515
4
Elkridge, MD, USA
| Does anyone think that this situation merits a sentence in the
| *Command Grouping* section, stating that labels shouldn't be used in
| a command group?

It would be harmless, but it would be a duplication of the sentence below in
topic "goto.htm":

The label must begin with a colon [:] and appear on a line by itself.

If anything is added to topic "grouping.htm", it ought to include all the
other commands which need to be on a line by themselves and thus ineligible
to be in a command group, e.g., DO and ENDDO, TEXT and ENDTEXT.
--
Steve
 
May 29, 2008
565
4
Groton, CT
| Does anyone think that this situation merits a sentence in the
| *Command Grouping* section, stating that labels shouldn't be used in
| a command group?

It would be harmless, but it would be a duplication of the sentence below in
topic "goto.htm":

The label must begin with a colon [:] and appear on a line by itself.

If anything is added to topic "grouping.htm", it ought to include all the
other commands which need to be on a line by themselves and thus ineligible
to be in a command group, e.g., DO and ENDDO, TEXT and ENDTEXT.
--
Steve

Perhaps the statement in file goto.htm could be amended to say:
Code:
The label must begin with a colon [:] and appear on a line by itself, and not within a command group.
 

rconn

Administrator
Staff member
May 14, 2008
12,316
148
> Consider this batch program, stored in "test.btm":
>
> @echo off
>
> if "%1" == "" goto helplabel
>
> if "%1" == "help" (
> :helplabel
> echo this
> echo is
> echo helpful
> goto end
> )
>
> :end
>
>
> With CMD.EXE, both "test" and "test help" produce the same 3-line
> output. With TC11, "test" produces the 3 lines, but "test help"
> produces no output.
>
> It looks like TC11 doesn't like the occurrence of a label within a
> command group. There's no mention of this in the "Command Grouping"
> documentation. In the GOTO command description, there's a discussion of
> "to avoid errors in the processing of nested statements", but there's
> no specific mention of command grouping.
>
> Is this a doc omission?

This is not the worst example of CMD syntax mangling I've ever seen, but it
definitely belongs in the top 10!

The reason it works with no argument is because the parser has not collapsed
the command group into a single line, and the GOTO will find ":helplabel" on
a separate line. The reason it doesn't work *with* an argument is because
everything in the command group HAS been collapsed to one line, and
everything following the ":helplabel" is assumed to be local GOSUB variables
(a TCC feature which CMD lacks).

I'm not going to remove local variables in order to support this
(never-seen-before) syntax, particularly since I cannot imagine why the
author needed to do this in the first place, even with CMD.

Rex Conn
JP Software
 
Similar threads
Thread starter Title Forum Replies Date
Jay Sage Bug With Flow Control and/or Command Grouping Support 2
L WAD bad interaction between echox and command grouping Support 9
Charles Dye Command grouping broken? Support 8
epement Command grouping in for... loops Support 5
L command grouping with TEXT...ENDTEXT Support 1
T VIEW only works from command line Support 10
rconn News Take Command / TCC / CMDebug / TCC-RT v28 Released Support 0
T Take Command 28: Lua reports version 5.4.0 (5.4.3 expected) Support 0
Jay Sage WAD Possible Bug With OPTION Command With @FILE Support 5
rconn News Take Command / TCC / CMDebug / TCC-RT 27.01.24 uploaded Support 0
J Take command does not start on Windows 10 Support 3
vefatica `Back quotes` - command line vs. batch file Support 5
Jay Sage WAD Failure of Alias Command with /Z Option Support 14
Phileosophos Is there any way to open the Take Command options dialog from the command line? Support 7
Phileosophos Command-line Editing Shortkeys That Fail Support 6
fpefpe How to? free (internal) command Support 0
rconn News Take Command / TCC / CMDebug / TCC-RT 27.01.23 uploaded Support 0
T How to? retain command history across reboot? Support 6
rconn News Take Command / TCC / CMDebug / TCC-RT v27.01 Build 22 Uploaded Support 0
GermanDirk How to? EVENTLOG Command with simple User Rights not functional Support 4
Jay Sage Take Command Is Too Fast! Support 4
Jay Sage Comparison of IF with Command Group and IFF Support 6
D Open Windows 10 Photos app from command line Support 11
rconn News Take Command / TCC / CMDebug / TCC-RT 27.0.21 uploaded Support 0
rconn News Take Command / TCC / CMDebug / TCC-RT 27.0.20 uploaded Support 0
U Can not get/start Take Command gui Support 1
rconn News Take Command / TCC / CMDebug / TCC-RT 27.0.19 Support 0
S Increasing Font Point Size in Command Tabs Support 2
mfarah Fixed The DATE command produces the wrong error message. Support 1
vefatica Command name mix-up Support 3
FreezerBurnt "Missing SETLOCAL" when initializing Visual Studio Command Prompt. Support 9
E How to? Closing desktop created by DESKTOP command Support 10
rconn News Take Command / TCC / CMDebug / TCC-RT v27.0.18 Support 0
K Take Command fox XP? Support 7
Joe Caverly Documentation Replace internal command with LIBRARY command Support 0
fpefpe How to? command echo in title line/bar Support 2
U Command help on file names Support 5
vefatica Command line DO with no closing parenthesis? Support 9
vefatica Command history search is off Support 7
H How to? How can I say "if the command separator is not ~ then setdos /C~" Support 8
N Command execution slows down in TCC Prompt after a while Support 6
R Long commands retrieved from command history have blank line when wrapped Support 20
jpeni Take Command Language Examples Support 1
rconn News Take Command / TCC / CMDebug / TCC-RT 26.02.41 Uploaded Support 0
fpefpe winfr command Support 4
Dmitry L. Kobyakov Fixed The SETARRAY /R command works incorrectly with cyrillic files Support 2
M Selecting test "off by one" problem in Take command Support 4
Dmitry L. Kobyakov Documentation Error in the Help: the WINDOW command Support 0
R How to? Open Take Command at current folder Support 7
rconn News Take Command / TCC / CMDebug / TCC-RT 26.01.40 uploaded Support 0

Similar threads