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:
- ProgramB checks for fullscreen parameter pass in QB64 and goes full screen.
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.
- 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:
- Note: When using this command read procedure, the commands sent must not be empty strings as the count will end!