COMMAND$

From QB64 Phoenix Edition Wiki
Revision as of 23:34, 20 June 2025 by RhoSigma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The COMMAND$ function returns the command line argument(s) passed when a program is run.


Syntax

argument$ = COMMAND$[(count%)]


Description

  • The STRING return value is anything typed after a program's executable file name in command line or by using the RUN or SHELL statements.
  • Unlike QuickBASIC, QB64 does not return all uppercase values, so keep that in mind when checking parameters.
  • Since QB64 v1.1, COMMAND$ also works like an array to return specific elements passed at the command line. Ex. COMMAND$(2) would return the second parameter passed.
  • COMMAND$(0) will always return the name of the program usually inclusive a full path. The passed arguments start at index 1.
    • Use the _COMMANDCOUNT function to find the number of parameters passed to a program via the command line.
  • Arguments can contain spaces if they are passed inside quotation marks. This can be used to properly retrieve file names and arguments which contain spaces. Note that the quotation marks are usually removed by the underlying console/terminal when parsing the command.


Availability

  • The optional count% argument was added in conjunction with the new _COMMANDCOUNT function in QB64 v1.1.


Examples

Example
The code below gets the number of parameters passed to our program from the command line with _COMMANDCOUNT.
'COMMAND$ index 0 is always the program name itself
'usually (but not necessarily) inclusive its full path
PRINT COMMAND$(0)
PRINT
'indexes 1 to _COMMANDCOUNT are the seperate arguments
'passed to the program in the same order as passed,
'quoted text counts as one argument, but quotes are usually
'already removed by the underlying console/terminal
numArgs& = _COMMANDCOUNT
FOR arg& = 1 TO numArgs&
    PRINT COMMAND$(arg&)
NEXT arg&
'COMMAND$ without an index returns all given arguments
'concatenated into one string (separated by a single space)
'in the same order as passed, due to the quote removal by
'the console/terminal the string gets ambiguous, also it
'doesn't include the program name itself (index 0), hence
'it is always better to use the COMMAND$(index) syntax
PRINT
PRINT COMMAND$
Explanation
 If we start MyProg.exe from the command window with
MyProg -l "loadfile.txt" -s "savefile.txt", the _COMMANDCOUNT would
be 4 and -l, loadfile.txt, -s and savefile.txt were the arguments passed
to the program, which we could then read separately with COMMAND$(n).


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link