COMMAND$ function
Purpose: COMMAND$ returns the command line tail, that is, a character string containing everything typed on the command line after the filename that started the program.
Syntax 1: RetStr$ = COMMAND$ Return Value:
Parameters:
|
Example 1:
DIM RetStr$ RetStr$ = COMMAND$ PRINT RetStr$
Syntax 2: RetStr$ = COMMAND$(ArgCount AS INTEGER) Return Value:
Parameters: |
Example 2:
DIM cmd$ DIM tailargs% = 0 cmd$ = COMMAND$ PRINT "Full command tail is: ", cmd$ cmd$ = COMMAND$(tailargs%) PRINT "The executable name is ", cmd$ DO tailargs++ cmd$ = COMMAND$(tailargs%) IF cmd$ = NUL$ THEN tailargs-- EXIT DO END IF PRINT "Argument", tailargs%, " is ", cmd$ LOOP PRINT "Total command tail arguments:", tailargs%
Result: Compiling the above code to snip.exe and running with the command line
snip one two three four five
the output is
Full command tail is: one two three four five The executable name is snip Argument 1 is one Argument 2 is two Argument 3 is three Argument 4 is four Argument 5 is five Total command tail arguments: 5
Example 3: ARGC contains the number of arguments on the command line of the executed program.
DIM cmd$ DIM i%, tailargs% cmd$ = COMMAND$ tailargs% = ARGC - 1 PRINT "Full command tail: ", cmd$ PRINT "The executable name is ", COMMAND$(0) FOR i% = 1 TO tailargs% cmd$ = COMMAND$(i) PRINT "Argument", i, " is ", cmd$ NEXT i% PRINT "Total command tail arguments:", tailargs%
Result: Compiling the above code to snip.exe and running with the command line
snip one two three four five
the output is
Full command tail: one two three four five The executable name is snip Argument 1 is one Argument 2 is two Argument 3 is three Argument 4 is four Argument 5 is five Total command tail arguments: 5
Example 4:
DIM i WHILE NOTNULL(COMMAND$(i)) PRINT COMMAND$(i) INCR i WEND PAUSE
Result: Compiling the above code to My_Example.exe and running with the command line
My_Example 000 111 222 333 444
the output is
My_Example 000 111 222 333 444 Press any key to continue . . .
Remarks:
☞ Using COMMAND$ directly,
as in this example
IF COMMAND$(1) = "" OR COMMAND(2) = "" THEN PRINT "Usage: DoNotDoThis File1 File2" END END IF
can lead to undefined behavior. Sometimes it may work but, at other times, it will cause problems.
Always try to use an intermediate variable string to retrieve contents of COMMAND$, as in this example
DIM RetStr1$ DIM RetStr2$ RetStr1$ = COMMAND$(1) RetStr2$ = COMMAND$(2) IF RetStr1$ = "" OR RetStr2$ = "" THEN PRINT "Usage: DoThis File1 File2" END END IF
Drag and Drop
COMMAND$ is drag and drop aware and can capture the path and name of a file that is drag and dropped or copied and pasted on to its executable.
The following example will show in a message box and print to a file the path\filename of any file dropped on it.
DIM Filename$ DIM A$ Filename$ = APPEXEPATH$ & "TestFile.txt" OPEN Filename$ FOR APPEND AS FP1 A$ = COMMAND$ MSGBOX A$ FPRINT FP1, A$ CLOSE FP1
Launch Detection
When the following example is launched from the Windows File Explorer or similar file manager, the program detects that the launch was not from a command line console and displays an information message box containing the name of the executable with its directory location and a message that the application is command line only. When run from a command line console the message box is not displayed.
IF CURSORX = 1 AND CURSORY = 1 AND COMMAND = "" THEN DIM TheMessage$, MsgRetVal% $FILL TheMessage$ "This application, " & CRLF$ APPEXENAME$ & CRLF$ " is COMMAND LINE ONLY!" & CRLF$ " It is located at " & CRLF$ APPEXEPATH$ & APPEXENAME$ & CRLF$ $FILL HIDE(CONWIN) MsgRetVal% = MSGBOX(TheMessage$, "COMMAND LINE ONLY!", MB_ICONASTERISK) END END IF PRINT "You DO know why you are looking at this !"
Remarks:
The above code is based on original source in the BCX translator
which was written to display the BCX version and build info when
the BCX executable is launched from the Windows File Explorer or
similar file manager.
BCX Console Sample Programs using the COMMAND$ function.
S06.bas, S44.bas, S47.bas, S48.bas, S64.bas, S65.bas, S67.bas, S68.bas, S71.bas, S73.bas, S74.bas, S77.bas, S96.bas, S98.bas, S103.bas, S105.bas, S141.bas,
ARGC and ARGV$[] identifiers
Purpose: ARGC and ARGV$[] identifiers are case insensitive analogues to the C language parameters in the function
INT main(INT argc, CHAR *argv[])
ARGC returns the full count of the command line, that is, the application invocation plus appended arguments. For example, the command line
CommandCount one two three four five
calling a compilation of this "CommandCount.bas" BCX code
PRINT ARGCwill output this
Result:
6
The string array ARGV$[] contains each of the items on the command line. Entering the command line
Commands one two three four five
calling a compilation of this "Commands.bas" BCX code
FOR INTEGER OO = 0 TO ARGC - 1 PRINT ARGV$[OO] NEXT
will output this
Result:
Commands one two three four five
Remarks:
☞ ARGC and
ARGV$[] are accessible only in Console mode and are
scope limited to 'C' main. To access command line arguments from
FUNCTION and SUB procedures
and, as well, GUI mode programs, use the COMMAND$
function.
☞ ARGC and
ARGV$[] are not available if coding using the BCX
$NOMAIN directive with a user defined MAIN()
function, such as,
$NOMAIN SUB MAIN () DIM RAW i i = 10 PRINT i END SUB
the 'C' language main() function emitted by
SUB MAIN ()
is
INT main(void)
☞ If coding with the BCX $NOMAIN directive, with a user defined MAIN() function, and a parameterized
INT main(INT argc, CHAR *argv[])translation is needed to access the application invocation and appended arguments, then do something like this.
$NOMAIN FUNCTION MAIN (ArgCount, ArgArray[] AS CHAR PTR) FOR INTEGER OO = 0 TO ArgCount - 1 PRINT ArgArray$[OO] NEXT END FUNCTION
Compiling the above as NoMainArgs.bas and invoking with the command line
NoMainArgs one two three four five
will output this
Result:
NoMainArgs one two three four five
For INT main() details please see the ISO/IEC 9899 C Language Standard section 5.1.2.2.1 Program startup.
BCX Console Sample Programs using the ARGC and ARGV$[] functions.