Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Example of COMMAND$ in MS QBASIC

127 views
Skip to first unread message

Timbo

unread,
Feb 12, 2001, 8:13:00 PM2/12/01
to
I see by the "help" screen in MS QBASIC that you can use COMMAND$ to pass
parameters into a QBASIC program from the command line -- but I can't find
any examples in working programs.

Please direct me to a working program that uses COMMAND$ to receive
parameters from the command line.

Thanx

Tim Carr


Daniel Johnson

unread,
Feb 12, 2001, 10:27:58 PM2/12/01
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(cross posted)
Timbo wrote in message <96a1ir$cok$1...@slb5.atl.mindspring.net>...
=>I see by the "help" screen in MS QBASIC that you can use COMMAND$
to pass
=>parameters into a QBASIC program from the command line -- but I
can't find
=>any examples in working programs.
=>
=>Please direct me to a working program that uses COMMAND$ to receive
=>parameters from the command line.

Here's a snippet of code from one the apps I wrote a while back. If
you ran the program with a ? or HELP (or help, HeLp, hElP, HElp, etc)
in the options/settings string, it ran a "Help" routine. Otherwise
it reset a variable. Note two errors in this code: (1) If the file
name you pass contains any form of "help", you get instructions, the
program does not proceed with that filename. (2) I used a GOTO.
<gasp> Actually, I couldn't care less, but some people think this is
a cardinal sin.

IF INSTR(COMMAND$, "?") = 0 THEN GOTO Help
IF INSTR(UCASE$(COMMAND$), "HELP") = 0 THEN GOTO Help
IF COMMAND$ <> "" THEN LogFile$ = COMMAND$

If you actually want the full code and a sample data file, you're
welcome to it, but I doubt it would be very interesting to you. :)
It was a quick-n-dirty utility to graph connection speeds from a
modem's log file. Considering how and when it was made, it worked
great. Oh, BTW, QBasic v1.1 really hates the COMMAND$. I only left
that in when I was about to compile it in QuickBASIC v4.5 (Yes, I
prefer the interpreter-only over the compiler. Sue me...)

Through the modem, off the server, over the T1, past the frame-relay,
< < NOTHIN' BUT NET > >

Daniel Johnson
N.o.S.p.A.m.P.r.o.g.m.a.n.2.0.0.0.@usa.net.
- -Remove N.o.S.p.A.m. and all dots but the obvious one to reply-
Public PGP Keys & other info: http://dannyj.come.to/pgp/
> > My news server "misses" posts occasionally. If I don't reply to
> > < < a question or something, please repost and/or e-mail me. < <

-----BEGIN PGP SIGNATURE-----
Version: 6.5.8.02ckt - http://irfaiad.virtualave.net/
Comment: http://DannyJ.Come.To/PGP/
Comment: KeyID: 0xEAF19C50163E81EF

iQA/AwUBOoipkerxnFAWPoHvEQKbfwCcDQiIMM+W+yaq8/9lwKX6jL8bVd0An2U1
ISyWKcCO6Plo7rpiQvxH9Ymh
=QLjC
-----END PGP SIGNATURE-----

Jon Bettencourt

unread,
Feb 13, 2001, 2:15:32 AM2/13/01
to
"Timbo" <carrf...@mindspring.com> wrote in message
news:96a1ir$cok$1...@slb5.atl.mindspring.net...

This fairly long program I wrote accepts a file name in COMMAND$, but you
can parse the COMMAND$ any way you want. In order to run this example,
you'll need SABINE.BAS or SABINE.QLB (both of which are unavailable at the
moment), but you can download the EXE at
http://kreativekorp.cjb.net/download/sabine/progress.htm.

ON ERROR GOTO ErrorHandler
DIM SHARED WINLOCX
DIM SHARED WINLOCY
WINLOCX = 8: WINLOCY = 24

MBAR$ = "New Open Save Save As Exit"
SABAPPSTDINIT MBAR$, "Sabine Icon Editor"
DRAWWINDOWFRAME "Icon Editor", WINLOCX, WINLOCY, 256 + WINLOCX, 248 +
WINLOCY, 1, 0, 0, 0, 1
FILLRECT 8 + WINLOCX, 24 + WINLOCY, 247 + WINLOCX, 239 + WINLOCY, 15
FL = 0
iOpen:
IF COMMAND$ <> "" AND FL = 0 THEN F$ = COMMAND$
IF FL = 1 THEN
DRAWASCIISTRING SPACE$(30), 8 + WINLOCX, 24 + WINLOCY, 15, 15, ""
DRAWASCIISTRING "Icon to open:", 8 + WINLOCX, 24 + WINLOCY, 0, 15, ""
SABINEINPUT 120 + WINLOCX, 24 + WINLOCY, 0, 15, F$
END IF
IF F$ = "" THEN F$ = "UNTITLED"
IF FL > 0 THEN FL = 0
IF F$ <> "UNTITLED" THEN
IF RIGHT$(F$, 4) <> ".SIC" THEN F$ = F$ + ".SIC"
IF INSTR(F$, "\") = 0 THEN F$ = GETSABPATH$("ICON\" + F$)
END IF
F$ = UCASE$(CORRECTFILENAME$(F$))
DRAWASCIISTRING SPACE$(30), 8 + WINLOCX, 24 + WINLOCY, 15, 15, ""
EDFLN$ = F$
IF LEN(EDFLN$) > 17 THEN EDFLN$ = "â" + RIGHT$(EDFLN$, 16)
DRAWASCIISTRING ("Editing file " + EDFLN$), 8 + WINLOCX, 24 + WINLOCY, 0,
15, ""
PLOTSABINE 0, 208 + WINLOCX, 76 + WINLOCY, 0, 0
PLOTSABINE 0, 216 + WINLOCX, 76 + WINLOCY, 1, 1
PLOTSABINE 0, 224 + WINLOCX, 76 + WINLOCY, 2, 2
PLOTSABINE 0, 232 + WINLOCX, 76 + WINLOCY, 3, 3
PLOTSABINE 0, 208 + WINLOCX, 84 + WINLOCY, 4, 4
PLOTSABINE 0, 216 + WINLOCX, 84 + WINLOCY, 5, 5
PLOTSABINE 0, 224 + WINLOCX, 84 + WINLOCY, 6, 6
PLOTSABINE 0, 232 + WINLOCX, 84 + WINLOCY, 7, 7
PLOTSABINE 0, 208 + WINLOCX, 92 + WINLOCY, 8, 8
PLOTSABINE 0, 216 + WINLOCX, 92 + WINLOCY, 9, 9
PLOTSABINE 0, 224 + WINLOCX, 92 + WINLOCY, 10, 10
PLOTSABINE 0, 232 + WINLOCX, 92 + WINLOCY, 11, 11
PLOTSABINE 0, 208 + WINLOCX, 100 + WINLOCY, 12, 12
PLOTSABINE 0, 216 + WINLOCX, 100 + WINLOCY, 13, 13
PLOTSABINE 0, 224 + WINLOCX, 100 + WINLOCY, 14, 14
PLOTSABINE 0, 232 + WINLOCX, 100 + WINLOCY, 15, 15
PLOTSABINE 549, 208 + WINLOCX, 108 + WINLOCY, 5, 13
PLOTSABINE 549, 216 + WINLOCX, 108 + WINLOCY, 5, 13
PLOTSABINE 549, 224 + WINLOCX, 108 + WINLOCY, 5, 13
PLOTSABINE 549, 232 + WINLOCX, 108 + WINLOCY, 5, 13
FILLRECT 208 + WINLOCX, 124 + WINLOCY, 239 + WINLOCX, 139 + WINLOCY, 0
IF F$ = "" THEN F$ = "UNTITLED"
OPEN F$ FOR BINARY AS #3
IDAT$ = SPACE$(24 ^ 2)
GET #3, 1, IDAT$
CLOSE #3
IF F$ = "UNTITLED" THEN KILL "UNTITLED"
IF F$ = "UNTITLED" THEN IDAT$ = STRING$(576, "T")
SP = 0
CC = 0
iDraw IDAT$
40 DRAWMOUSEPTRPT1 B, RX, RY
IF POINTWITHINCLOSEBOX(RX, RY, WINLOCX, WINLOCY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
END
END IF
CHECKWINMOVE B, RX, RY, WINLOCX, WINLOCY, 256, 248, MBAR$, "Sabine Icon
Editor"
IF MOUSEONMENU("New", MBAR$, RX, RY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
F$ = "": DRAWMOUSEPTRPT2 RX, RY: FL = 3: GOTO iOpen
END IF
IF MOUSEONMENU("Open", MBAR$, RX, RY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
DRAWMOUSEPTRPT2 RX, RY: FL = 1: GOTO iOpen
END IF
IF MOUSEONMENU("Save", MBAR$, RX, RY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
iSave F$, IDAT$
END IF
IF MOUSEONMENU("Save As", MBAR$, RX, RY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
iSaveAs F$, IDAT$
END IF
IF MOUSEONMENU("Exit", MBAR$, RX, RY) = 1 AND B = 1 THEN
WHILE B = 1: DONTDRAWMOUSEPTR B, MX, MY: WEND
END
END IF
IF POINTWITHINRECT(RX, RY, 208 + WINLOCX, 76 + WINLOCY, 239 + WINLOCX, 115 +
WINLOCY) AND B = 1 THEN
CC = INT((RX - 208 - WINLOCX) / 8) + (4 * INT(((RY - 76 - WINLOCY) / 8)))
IF CC > 15 THEN CC = 16
IF CC < 16 THEN FILLRECT 208 + WINLOCX, 124 + WINLOCY, 239 + WINLOCX, 139 +
WINLOCY, CC
IF CC = 16 THEN
FOR Y = 124 TO 139 STEP 8
FOR X = 208 TO 239 STEP 8
PLOTSABINE 549, X + WINLOCX, Y + WINLOCY, 5, 13
NEXT X
NEXT Y
END IF
END IF
IF POINTWITHINRECT(RX, RY, 8 + WINLOCX, 40 + WINLOCY, 199 + WINLOCX, 231 +
WINLOCY) AND B = 1 THEN
SP = INT((RX - 8 - WINLOCX) / 8) + (24 * INT((RY - 40 - WINLOCY) / 8))
NWC$ = MID$("KBGCRPYwkbgcrpyWT", CC + 1, 1)
IDAT$ = LEFT$(IDAT$, SP) + NWC$ + RIGHT$(IDAT$, ((24 ^ 2) - SP) - 1)
DRAWMOUSEPTRPT2 RX, RY
IF CC <> 16 THEN PLOTSABINE 32, 8 * INT((RX - 8 - WINLOCX) / 8) + 8 +
WINLOCX, 8 * INT((RY - 40 - WINLOCY) / 8) + 40 + WINLOCY, CC, CC
IF CC = 16 THEN PLOTSABINE 549, 8 * INT((RX - 8 - WINLOCX) / 8) + 8 +
WINLOCX, 8 * INT((RY - 40 - WINLOCY) / 8) + 40 + WINLOCY, 5, 13
PSET (INT((RX - 8 - WINLOCX) / 8) + 208 + WINLOCX, INT((RY - 40 - WINLOCY) /
8) + 208 + WINLOCY), CC
DRAWMOUSEPTRPT2 RX, RY
END IF
DRAWMOUSEPTRPT2 RX, RY
GOTO 40
END
ErrorHandler:
REPORTERROR ERR, ERL
RESUME

SUB iDraw (IDAT$)
iDraw:
FOR Y = 0 TO 23
FOR X = 0 TO 23
T = (Y * 24) + X
D$ = MID$(IDAT$, T + 1, 1)
IF D$ <> "T" THEN PLOTSABINE 544, (8 * X) + 8 + WINLOCX, (8 * Y) + 40 +
WINLOCY, INSTR("BGCRPYwkbgcrpyW", D$), 0
IF D$ = "T" THEN PLOTSABINE 549, (8 * X) + 8 + WINLOCX, (8 * Y) + 40 +
WINLOCY, 5, 13
PSET (X + 208 + WINLOCX, Y + 208 + WINLOCY), INSTR("BGCRPYwkbgcrpyWT", D$)
NEXT X
NEXT Y
END SUB

SUB iSave (F$, IDAT$)
IF F$ = "UNTITLED" OR F$ = "" THEN
iSaveAs F$, IDAT$
ELSE
OPEN F$ FOR BINARY AS #3
PUT #3, 1, IDAT$
CLOSE #3
END IF
END SUB

SUB iSaveAs (F$, IDAT$)
DRAWASCIISTRING SPACE$(30), 8 + WINLOCX, 24 + WINLOCY, 15, 15, ""
DRAWASCIISTRING "New name of icon:", 8 + WINLOCX, 24 + WINLOCY, 0, 15, ""
SABINEINPUT 152 + WINLOCX, 24 + WINLOCY, 0, 15, F$
IF F$ = "" THEN
DRAWASCIISTRING SPACE$(30), 8 + WINLOCX, 24 + WINLOCY, 15, 15, ""
EDFLN$ = "UNTITLED"
IF LEN(EDFLN$) > 17 THEN EDFLN$ = "â" + RIGHT$(EDFLN$, 16)
DRAWASCIISTRING ("Editing file " + EDFLN$), 8 + WINLOCX, 24 + WINLOCY, 0,
15, ""
EXIT SUB
END IF
IF LEFT$(RIGHT$(F$, 4), 1) <> "." THEN F$ = F$ + ".SIC"
IF INSTR(F$, "\") = 0 THEN F$ = GETSABPATH$("ICON\" + F$)
F$ = UCASE$(CORRECTFILENAME$(F$))
DRAWASCIISTRING SPACE$(30), 8 + WINLOCX, 24 + WINLOCY, 15, 15, ""
EDFLN$ = F$
IF LEN(EDFLN$) > 17 THEN EDFLN$ = "â" + RIGHT$(EDFLN$, 16)
DRAWASCIISTRING ("Editing file " + EDFLN$), 8 + WINLOCX, 24 + WINLOCY, 0,
15, ""
iSave F$, IDAT$
END SUB


Caisson

unread,
Feb 13, 2001, 7:03:05 AM2/13/01
to
Timbo <carrf...@mindspring.com> schreef in artikel <96a1ir$cok$1...@slb5.atl.mindspring.net>...

Hello Timbo,

If you mean using the COMMAND$ variable in a QBASIC environment (QBASIC like in the version that comes free with DOS), you can't.

Open-up QBASIC, type the word "COMMAND$', place your cursor on the word and press F1. Read the Help-screen. It should mention
something about "Unsupported commands", and COMMAND$ is one of them.

If you want to use something like COMMAND$ anyway, you could use a Batch-file that groups together a bunch of argument-words and
places it into an Environment-variable. Like so:

set MYARGS=%1 %2 %3 %4 %5 %6 %7 %8 %9

On the next line you place the command to run your program.

QBASIC /run MYPROG

The last line erases the (temporary) QBASIC Command-line arguments.

set MYARGS=

To access the contents of the (DOS) Environment-variable MYARGS, you can use the command ENVIRON$. Like so :

MyCmdLine$ = ENVIRON$("MYARGS")

Now you can use the variable MyCmdLine$ as if it where the origional COMMAND$ variable.

Regards,
Rudy Wieser

co...@catherders.com

unread,
Feb 13, 2001, 8:24:09 AM2/13/01
to

REM get the command line
cline$ = COMMAND$ + " "
i = 0
WHILE cline$ <> ""
p = INSTR(cline$, " ")
arg$(i) = RTRIM$(LEFT$(cline$, p))
cline$ = RIGHT$(cline$, (LEN(cline$) - p))
i = i + 1
WEND
rem command line parms will be in arg$(), max 10


Mike-


In <96a1ir$cok$1...@slb5.atl.mindspring.net>, on 02/12/2001

>Thanx

>Tim Carr


-----------------------------------------------------------
Please note: My public PGP key is available on my web site.
The web page is at http://www.catherders.com

Because network administration is like herding cats.
-----------------------------------------------------------
It is currently impossible to email me from any address in the
hotmail.com domain. When they change their spam policies,
I'll change my trashcan filters. Sorry for the inconvienence.
-----------------------------------------------------------

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----

Pat Pridgen

unread,
Feb 13, 2001, 8:43:36 AM2/13/01
to


This is a ittle bit of code from a backup utility that I wrote for the
Production Scheduling Dept. This VBDos code, but you can see how you
would use COMMAND$ from this.


DECLARE SUB CopyFile (InFile$, OutFile$)


InFile$ = COMMAND$


OutFile$ = FORMAT$(NOW, "mmddhhmm")
OutFile$ = OutFile$ + ".xls"

CopyFile InFile$, OutFile$

END

--
Pat - LaGrande, OR. ppridgen<at>greencis<dot>net
http://www.greencis.net/~ppridgen
Red Hat 6.0 - Win95 dual boot.
Old-Tag: Sure you can trust the government ... ask any Indian.

Andreas Meile

unread,
Feb 14, 2001, 4:23:46 PM2/14/01
to
"Timbo" <carrf...@mindspring.com> schrieb im Newsbeitrag
news:96a1ir$cok$1...@slb5.atl.mindspring.net...

> I see by the "help" screen in MS QBASIC that you can use COMMAND$ to pass
> parameters into a QBASIC program from the command line -- but I can't find
> any examples in working programs.

The very basic example, but very good for your education:

' Print the line (CMDDEMO.BAS)
PRINT "Command line: >"; COMMAND$; "<"
' Produce a ASCII dump so any special characters will be visible
FOR i%=1 TO LEN(COMMAND$)
PRINT ASC(MID$(COMMAND$, i%, 1));
NEXT i%
PRINT

Note: This example has a more educational rather than useful character. But
it's good to compile this program into a .EXE file and extensively play with
it! For example

C:\QBASIC>cmddemo
Command line: ><
C:\QBASIC>cmddemo Aa Bb Cc
Command line: >AA BB CC<
65 65 32 66 66 32 67 67
Your first conclusion: Case sensitivity goes lost.
Add trailing blanks
C:\QBASIC>cmddemo Aa Bb Cc
Command line: >AA BB CC <
65 65 32 66 66 32 67 67 32 32
=> Result: will passed!
Add multiple blanks between the arguments
C:\QBASIC>cmddemo Aa Bb Cc
Command line: >AA BB CC<
65 65 32 32 32 66 66 32 32 32 32 32 32 67 67
=> will be passed except those at the beginning, i.e. it's always LTRIM$ed
Trying some special characters (Note: ^U = Ctrl+U) and <Tab> chars
C:\QBASIC>cmddemo Aa Bbéö ^U^U ll
Command line: >AA BBéö §§ LL<
65 65 9 66 66 130 148 32 32 21 21 32 76 76
Result: all will passed!
How about environment variables?
C:\QBASIC>cmddemo Andy %TEMP% Berta
Command line: >ANDY D:\TEMP BERTA<
65 78 68 89 32 68 58 92 84 69 77 80 32 66 69 82 84 65
Result: COMMAND.COM resolves such things!
How about stdin/stdout redirection?
C:\QBASIC>cmddemo Andy >%TEMP%\x.txt Berta

C:\QBASIC>type %TEMP%\x.txt
Command line: >ANDY BERTA<
65 78 68 89 32 32 32 32 32 66 69 82 84 65
Very important and good to remember: The "<" and ">" processing is always
done by COMMAND.COM. People who know shell script programming under UNIX and
Linux should be familiar with that behaviour!
No special things with the following:
C:\QBASIC>cmddemo Andy <%TEMP%\x.txt Berta
Command line: >ANDY BERTA<
65 78 68 89 32 32 32 32 32 66 69 82 84 65

C:\QBASIC>cmddemo Andy <%TEMP%\x.txt >%TEMP%\y.txt Berta

C:\QBASIC>type %TEMP%\y.txt
Command line: >ANDY BERTA<
65 78 68 89 32 32 32 32 32 66 69 82 84 65
Examination question: Why we have five space characters between "ANDY" and
"BERTA"?
How about pipes?
C:\QBASIC>cmddemo Alfa Beta |sort
65 76 70 65 32 32 66 69 84 65 32 32
Command line: >ALFA BETA <
Examination question: Why "|sort" does not appear in COMMAND$?

> Please direct me to a working program that uses COMMAND$ to receive
> parameters from the command line.

Now we stop the funny toy playing phase and go to the serious productive
example: I wrote a small but useful parser which should give the UNIX/Linux
like argv[1], argv[2], argv[3]... functionality to pass parameters as well
as position independent switch parsing.

' This can be used in your own projects
t$ = COMMAND$
WHILE t$ <> ""
' Split them to args
p% = INSTR(t$, " ")
IF p% = 0 THEN
g$ = t$
t$ = ""
ELSE
g$ = LEFT$(t$, p% - 1)
t$ = LTRIM$(MID$(t$, p%))
END IF
' extract switches
isArg% = -1
DO
q% = INSTR(g$, "/")
IF q% = 0 THEN
a$ = g$
g$ = ""
ELSE
a$ = LEFT$(g$, q% - 1)
g$ = MID$(g$, q% + 1)
END IF
IF isArg% THEN
IF a$ <> "" THEN
PRINT "Argument "; a$; " passed"
' argv%(argc%) = a$
' argc% = argc% + 1
END IF
isArg% = 0
ELSE
IF a$ = "" THEN
PRINT "Usage syntax error"
END
END IF
PRINT "Option switch /"; a$; " set"
' SELECT CASE a$
' CASE "I"
' ' ignoring case senstivity for example
' CASE "P"
' ' create a print file for example
' CASE "V"
' ' enable verify while writing on disk
' CASE ELSE
' ' Process the more complex parameters here
' IF LEFT$(a$, 2) = "F:" THEN
' FloppyDiskSize% = VAL(MID$(a$, 3))
' ' ^This is like /F:720 from FORMAT.COM
' ELSEIF LEFT$(a$, 2) = "O:" THEN
' OutputFile$ = MID$(a$, 3)
' ELSE
' PRINT "Usage syntax error"
' PRINT "Usage: ARG_PARS [ testfile1 testfile2 .. ] [ /V ] [ /P ]
[ /I ] [ /F:<Floppysize> ] [ /O:<Outputfile> ]"
' END
' END IF
' END SELECT
END IF
LOOP UNTIL q% = 0
WEND

Let this run, for example:

C:\QBASIC>arg_pars aaa.dat /b /j/l b.txt /q/l:720 /pp:1520 c.jpg/q
/o:d.htm
Argument AAA.DAT passed
Option switch /B set
Option switch /J set
Option switch /L set
Argument B.TXT passed
Option switch /Q set
Option switch /L:720 set
Option switch /PP:1520 set
Argument C.JPG passed
Option switch /Q set
Option switch /O:D.HTM set

Pay attention to the blanks! This routine really allows a very tolerant
parsing. I hope this COMMAND$ parser should satisfy your needs. :-) The
comment lines are only some example application ideas how to process the
extracted arguments.

Andreas


0 new messages