COMMAND$ function  

Purpose

Return the command-line arguments used to start the program.

Syntax

s$ = COMMAND$
s$ = COMMAND$(ArgNum)

Remarks

COMMAND$ returns everything that was typed following the program name.  Some operating system manuals refer to this text as the trailer or command tail. You can use COMMAND$ to collect run-time arguments, like filenames, and program options.

Depending upon the optional argument number, COMMAND$ will return either the complete trailer, or just one of the arguments.  If the ArgNum is zero (0), or not present, the complete trailer is returned. If the ArgNum is greater than zero, the trailer is parsed to return an individual argument (1 = first argument. 2 = second argument, etc.). If the ArgNum is greater than the number of arguments, a null string (zero-length) is returned.

Arguments are delimited by one or more blank spaces.  If blank spaces are significant, you should enclose the argument in double quotes ("). Any such double-quotes are stripped from the return value by COMMAND$. If a zero-length quoted string ("") is found, it is ignored entirely.

For example, consider a program named FASTSORT.EXE that reads data from one file, sorts it, and puts the result in a new file.  Using COMMAND$ lets you specify the input and output file names when the program is invoked:

FASTSORT.EXE cust.dta cust.new

When FASTSORT begins execution, COMMAND$ or COMMAND$(0) would return:

cust.dta cust.new

COMMAND$(1) would return:

cust.dta

COMMAND$(2) would return:

cust.new

Restrictions

In some recent versions of Windows, file association and drag-drop file operations cause filenames to be enclosed with double-quote marks when they are passed in COMMAND$.  It would be wise to ensure that your applications are prepared for this possibility.  Some operating systems automatically enclose the command-line in double-quote marks.

PowerBASIC imposes no arbitrary limits on the length of the string returned by COMMAND$ but, the operating system may impose limits.  Such limits may become evident, for example, when attempting to Drag and Drop a large number of files onto an EXE within Windows Explorer.  Usually, attempting to drop more files than the operating system permits will result in an operating system warning message.

Within the IDE, a COMMAND$ command-line parameter can be specified for the purposes of testing in both Compile and Execute and Compile and Debug modes.

See also

JOIN$, PARSE, PARSE$, PARSECOUNT, PATHNAME$, PATHSCAN$, WINMAIN

Example

#COMPILE EXE

FUNCTION PBMAIN

  IF TRIM$(COMMAND$) = "" THEN

    EXIT FUNCTION  ' No command-line params given, just quit

  ELSEIF INSTR(COMMAND$, "/Q") THEN

    ' Process the /Q option

  ELSEIF INSTR(COMMAND$, "/W") THEN

    ' Process the /W option

  END IF

END FUNCTION