Command Line Argument/Switch

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Command Line Argument/Switch

Post by Mike Alexander »

I am writing a small application where I will need to use a few command line arguments... I have not done this before and have always just used a config.cfg file for my applications. However, this time I decided rather than create yet another config file, I could use a command line argument list (actually for a filename.inp and filename.out) to send to my program via a batch file variable...

I was thinking the call to my program would be something like this:

NEWPRG.EXE OLDFILE.NAM NEWFILE.NAM

Any hints or examples of how to set this up?

Thanks all!

Mike Alexander
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Checkout the COMMAND$ function.

Using the same command-line that you posted, your program can get the names of the 2 files as follows:

Code: Select all

c$ = command$     'get the command-line, except for the .EXE name.
s = instr(c$," ")     'find location of space delimiter between filenames.
if s = 0 then print "Invalid or missing filenames" : system
filold$ = left$(c$,s-1)     'Get leftmost filename.
filold$ = ltrim$(rtrim$(filold$))     'Get rid of any leading or trailing spaces.
filnew$ = mid$(c$,s+1)     'Get leftmost filename.
filnew$ = ltrim$(rtrim$(filnew$))
Now you have the filenames so you can open them.
The only problem now is that the name of the input "old" file might be invalid, and you'll get an error when you open it. But, that's another subject.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

Whoa... cool... That was fast.... Thanks for he help I will plug it in and give it a shot now...

:-)
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:Now you have the filenames so you can open them.
The only problem now is that the name of the input "old" file might be invalid, and you'll get an error when you open it. But, that's another subject.

Regards..... Moneo
Ok... So that worked exactly like you said... so now what were you meaning about an error.... Why would the old file be invalid? You mean that if the file does not exist I will get the error?
-----------edited-----------
What I have done is put On Error Goto MyErrorThing before the Open command. Then create this>>>
MyErrorThing:
Print FILENAME$ +"Does not exist"
END
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Right, if the user makes a typo error on the filename, you'll get a "file not found" error on the open. Using the ON ERROR is a good way to catch this.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:Right, if the user makes a typo error on the filename, you'll get a "file not found" error on the open. Using the ON ERROR is a good way to catch this.

Regards..... Moneo
Can I have an error trap for my whole program? By that I mean at the very top (just before my mainline) I can put an "on error goto errortrap" and then build a case switch for each error number???? of course I would need the catch all for errors that dont have an error code in my trap....
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Mike Alexander wrote:..
Can I have an error trap for my whole program? By that I mean at the very top (just before my mainline) I can put an "on error goto errortrap" and then .all for errors that dont have an error code in my trap.
Here's the recommended way to handle error trapping, by Mac, over at Qbasic.com.

As you can see, he generally has ON ERROR GOTO 0 to trap all the unknown errors that might occur. When he suspects the possibility of an error on a particular instruction, he first does an ON ERROR GOTO GetMyError, does the suspect instruction, and then turns error trapping off again. I've tested this method and it works very well.

See his sample program below. Notice instructions flagged with REM NOTE...

Code: Select all

DECLARE SUB aMain ()
DECLARE FUNCTION StatusFile% (f AS STRING)
DIM SHARED MyErr AS INTEGER
CALL aMain
SYSTEM

GetMyErr: MyErr = ERR: RESUME NEXT    REM NOTE....

SUB aMain
CLS : LOCATE 2, 20: PRINT "Testing function StatusFile": PRINT
PRINT "Enter file names to be tested. Just press Enter when finished."
DO
  PRINT
  LINE INPUT "fname: "; f$
  IF f$ = "" THEN EXIT DO
  SELECT CASE StatusFile(f$)
  CASE 0: PRINT "Bad File Name"
  CASE 1: PRINT "Exists"
  CASE 2: PRINT "Does not exist"
  END SELECT
LOOP
SYSTEM
END SUB

FUNCTION StatusFile% (f AS STRING)
IF LEN(f) = 0 THEN EXIT FUNCTION
' Ensure no spaces anywhere
IF INSTR(t$, " ") > 0 THEN EXIT FUNCTION
' Separate file name from path, if any
DIM y AS INTEGER
FOR y = LEN(f) TO 1 STEP -1
  SELECT CASE MID$(f, y, 1)
  CASE "", "/", ":": EXIT FOR
  CASE " ": EXIT FUNCTION
  END SELECT
NEXT y
IF y = 1 THEN t$ = f ELSE t$ = RIGHT$(f, LEN(f) - y)
' Test for prefix(8 max).suffix(3 max)
FOR y = LEN(t$) TO 1 STEP -1
  IF MID$(t$, y, 1) = "." THEN
    IF LEN(t$) - y > 3 THEN EXIT FUNCTION' suffix>3
    IF y = 1 THEN EXIT FUNCTION ' name was just "."
    t$ = LEFT$(t$, y - 1): EXIT FOR
  END IF
NEXT y
IF LEN(t$) > 8 THEN EXIT FUNCTION
' OK, now test whole thing for reaMaining errors
MyErr = 0: ON ERROR GOTO GetMyErr   REM NOTE....
OPEN f FOR INPUT AS #1: CLOSE
ON ERROR GOTO 0                          REM NOTE...
IF MyErr = 0 THEN StatusFile% = 1   REM NOTE....
IF MyErr = 53 THEN StatusFile% = 2  REM NOTE....
END FUNCTION
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:Checkout the COMMAND$ function.

Using the same command-line that you posted, your program can get the names of the 2 files as follows:

Code: Select all

c$ = command$     'get the command-line, except for the .EXE name.
s = instr(c$," ")     'find location of space delimiter between filenames.
if s = 0 then print "Invalid or missing filenames" : system
filold$ = left$(c$,s-1)     'Get leftmost filename.
filold$ = ltrim$(rtrim$(filold$))     'Get rid of any leading or trailing spaces.
filnew$ = mid$(c$,s+1)     'Get leftmost filename.
filnew$ = ltrim$(rtrim$(filnew$))
Now you have the filenames so you can open them.
The only problem now is that the name of the input "old" file might be invalid, and you'll get an error when you open it. But, that's another subject.

Regards..... Moneo
More on my program...

So how can I add non printable Characters in a Print statement? Specifically an ASCii Value of 001
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Mike Alexander wrote:.....More on my program...
So how can I add non printable Characters in a Print statement? Specifically an ASCii Value of 001
You can add any valid ASCII characters you want, either directly to a PRINT command or to a string, like:
PRINT "BLABLABLA"+CHR$(001) ... or more
or
A$="BLABLABLA"+CHR$(001)
PRINT A$

I'm guessing that you-re going to print to file or a comm line or a special printer, otherwise printing to the screen won't show the special characters.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:
Mike Alexander wrote:.....More on my program...
So how can I add non printable Characters in a Print statement? Specifically an ASCii Value of 001
You can add any valid ASCII characters you want, either directly to a PRINT command or to a string, like:
PRINT "BLABLABLA"+CHR$(001) ... or more
or
A$="BLABLABLA"+CHR$(001)
PRINT A$

I'm guessing that you-re going to print to file or a comm line or a special printer, otherwise printing to the screen won't show the special characters.

Regards..... Moneo
I was not aware I could Print non-printable characters... makes perfect sense... :roll:

Either way, I will admit, I did ask the question before actually trying. I just assumed you can't print non-printable characters.

CHR$(001) is the character the Synchronet BBS uses to determine what color will be displayed in the BBS Software.... For example, to display Red text, it is STRINGTOPRINT$ = CHR$(001) + "R" + STRINGTOPRINT$

I did get it going... and it is a good thing, but it has become cumbersome keeping track of CHR$(001) plus color codes... so now I am going to define the colors as a pre-defined string and just sdd it instead of the other stuff... For example:

REDTXT$ = CHR$(001) + "R"
BLUETXT$ = CHR$(001) +"B"

STRINGTOPRINT = REDTXT$ +STRINGTOPRINT$
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Sounds like a good way to do it.

BTW, unprintable just means that you can't actually see the character when you print it to the screen or printer, but it's there and gets sent to the device.

Regards..... Moneo
Post Reply