COMMAND$

From QB64 Wiki
Revision as of 01:34, 27 May 2015 by imported>Clippy
Jump to navigation Jump to search

The COMMAND$ function returns the DOS commandline arguments passed when a program is run.


== Syntax == variable$ = COMMAND$


  • The string return value is any parameter following the filename in a RUN or command line statement.
  • Qbasic returns uppercase STRING parameters no matter what case they were sent originally.
  • QB64 does not require or return all uppercase values so keep that fact in mind when checking parameters passed!
  • Useful when the programmer wants to add options to the command line of a program for later use by another program.
  • In QB64 only, COMMAND$ can work as an array to return specific elements passed to the command line. COMMAND$(2) would return the second parameter passed at the command line. This can be used on modern operating systems to successfully retrieve filenames and arguments which might contain spaces properly. See Example 2.
  • To find the number of parameters passed to a program via the command line, use the _COMMANDCOUNT function.
  • COMMAND$ was not available in QuickBasic versions below 4.0


Example 1: Compile both programs. ProgramA RUNs ProgramB with a parameter passed following the filename:

LOCATE 12, 36: PRINT "ProgramA" LOCATE 23, 25: PRINT "Press any key to run ProgramB" K$ = INPUT$(1) RUN "ProgramB FS" 'pass FS parameter to ProgramB in QB64 END

ProgramB checks for fullscreen parameter pass in QB64 and goes full screen.

LOCATE 12, 36: PRINT "ProgramB" parameter$ = UCASE$(COMMAND$) LOCATE 20, 33: PRINT "Parameter = " + parameter$ IF LEFT$(parameter$, 2) = "FS" THEN _FULLSCREEN 'parameter changes to full screen END

Parameter = FS.EXE


Example 2: Program gets the number of parameters passed to the program, and then prints those parameters to the screen one at a time.

limit = _COMMANDCOUNT FOR i = 1 TO limit PRINT COMMAND$(i) NEXT

Explanation: If we start ThisProgram.exe with the command line ThisProgram -l "a data file", COMMAND$ will return a single string of "-1 a data file" which might be hard to process and interpret properly, but COMMAND$(1) would return "-l" and COMMAND$(2) would return "a data file" as separate entries for easier parsing and processing.


Example 3: As part of the command array upgrade, you can also just read the array to see how many commands were sent:

DO count = count + 1 cmd$ = COMMAND$(count) IF cmd$ = "" THEN EXIT DO PRINT cmd$ 'or code to handle each parameter sent LOOP

Note: When using this command read procedure, the commands sent must not be empty strings as the count will end!


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page