Q(uick)BASIC Function: COMMAND$

Quick View

COMMAND$

A function that interfaces with DOS to return the command line used to invoke the program

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • COMMAND$
Description/Parameter(s)

The COMMAND$ function returns the complete command line entered after your BASIC program name, including optional parameters. COMMAND$ removes all leading blanks from the command line and converts all letters to uppercase (capital letters). The COMMAND$ function can be used in stand-alone executable files or, if you are executing from the QuickBASIC environment, by using the /CMD command line option or by selecting Modify COMMAND$ on the Run menu.

Example

CMD_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the COMMAND$ function. To look at the program in the View window and, optionally, to run it, load CMD_EX.BAS using the File menu's Open Program command.
The program breaks the command line into separate arguments and stores them in an array. Each argument is separated from adjoining arguments by one or more blanks or tabs on the command line.
The following is a sample command line and output for a stand-alone executable file (assumes program name is arg.exe):arg one  two   three    four     five      six

Sample Output:

Number of arguments = 6 Arguments are: ONE TWO THREE FOUR FIVE SIX
Syntax
  • COMMAND$
Description/Parameter(s)

The COMMAND$ function returns the complete command line entered after your BASIC program name, including optional parameters.

Usage Notes

  • COMMAND$ converts all letters to uppercase (capital letters) and removes all leading blanks from the command line.
  • The COMMAND$ function can be used in stand-alone executable files or, if you are executing from the QBX environment, by using the /CMD command-line option, or by selecting Modify COMMAND$ on the RUN menu.
Examples

This program uses the COMMAND$ function to break the command line into separate arguments. Each argument is separated from adjoining arguments by one or more blanks or tabs on the command line.

'Default variable type is integer in this module. DEFINT A-Z 'Declare the Comline SUB procedure, as well as the number and 'type of its parameters. DECLARE SUB Comline (N, A$(), Max) DIM A$(1 TO 15) 'Get what was typed on the command line. CALL Comline(N, A$(), 10) 'Print out each part of the command line. PRINT "Number of arguments = "; N PRINT "Arguments are: " FOR I = 1 TO N: PRINT A$(I): NEXT I 'SUB procedure to get command line and split into arguments. 'Parameters: NumArgs : Number of command line args found. ' Args$() : Array in which to return arguments. ' MaxArgs : Maximum number of arguments array can return. SUB Comline (NumArgs, Args$(), MaxArgs) STATIC CONST TRUE = -1, FALSE = 0 NumArgs = 0: In = FALSE 'Get the command line using the COMMAND$ function. Cl$ = COMMAND$ L = LEN(Cl$) 'Go through the command line a character at a time. FOR I = 1 TO L C$ = MID$(Cl$, I, 1) 'Test for character being a blank or a tab. IF (C$ <> " " AND C$ <> CHR$(9)) THEN 'Neither blank nor tab. Test if you're already inside an argument. IF NOT In THEN 'You've found the start of a new argument. 'Test for too many arguments. IF NumArgs = MaxArgs THEN EXIT FOR NumArgs = NumArgs + 1 In = TRUE END IF 'Add the character to the current argument. Args$(NumArgs) = Args$(NumArgs) + C$ ELSE 'Found a blank or a tab. 'Set "Not in an argument" flag to FALSE. In = FALSE END IF NEXT I END SUB

The following is a sample command line and output for a stand-alone executable file (assumes program name is arg.exe):arg one  two   three    four     five      six

Sample Output:

Number of arguments = 6 Arguments are: ONE TWO THREE FOUR FIVE SIX