Difference between revisions of "COMMAND$"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 1: Line 1:
The '''COMMAND$''' function returns the DOS commandline arguments passed when a program is run.
The '''COMMAND$''' [[STRING]] function returns the spaced [[DOS]] command line argument(s) passed when a program is run.






{{PageSyntax}} variable$ = COMMAND$
{{PageSyntax}}  
::: option$ = [[COMMAND$]][(count%)]




* The [[STRING|string]] return value is any parameter following the filename in a [[RUN]] or command line statement.
* Useful when the programmer wants to send specific program options to the command line for later use by the called program.
* Qbasic returns [[UCASE$|uppercase]] [[STRING]] parameters no matter what case they were sent originally.  
* The [[STRING]] return value is any '''unquoted or quoted spaced''' parameter(s) following the filename in a [[RUN]] or command line statement.
* '''QB64''' does not '''require or return all [[UCASE$|uppercase]]''' values so keep that fact in mind when checking parameters passed!
* '''QB64''' does not '''require or return all [[UCASE$|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 '''spaced''' second parameter passed at the command line.  This can be used on modern operating systems to successfully retrieve file names and arguments which contain spaces properly. (versions after May 20, 2015)  
 
* Use the [[_COMMANDCOUNT]] function to find the number of spaced parameters passed to a program via the command line. (versions after May 20, 2015) '''{{text|See ''Example 2''|green}}'''.
* 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 file names and arguments which contain spaces properly. (versions after May 20, 2015) See ''Example 2''.
* Reading the spaced command options in the '''COMMAND$(i)''' array in a loop can also be done and reading a COMMAND$ without parameters is also possible. (versions after May 20, 2015) '''{{text|See ''Example 3''|green}}'''.
* Use the [[_COMMANDCOUNT]] function to find the number of parameters passed to a program via the command line. (versions after May 20, 2015) See ''Example 2''.
* COMMAND$ was '''not available in QuickBasic versions below 4.0''' and returned [[UCASE$|uppercase]] [[STRING]] parameters no matter what case they were sent originally.
* COMMAND$ without parameters still works and reading the '''COMMAND$(i)''' array in a loop can also be used. (versions after May 20, 2015) See ''Example 3''.
* COMMAND$ was '''not available in QuickBasic versions below 4.0'''




Line 46: Line 45:
{{Cl|NEXT}}
{{Cl|NEXT}}
{{CodeEnd}}
{{CodeEnd}}
: ''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.
{{OutputStart}}-1
a data file
{{OutputEnd}}
: ''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 the quoted "a data file" option as separate entries for easier parsing and processing.




Line 58: Line 60:
count = count - 1 'save the number of parameters sent to this program when run
count = count - 1 'save the number of parameters sent to this program when run
{{CodeEnd}}
{{CodeEnd}}
:'''Note:''' When using this command [[DO]] loop read procedure, the commands sent must not be empty strings as the count will end!
:'''Note:''' When using this command [[DO]] loop read procedure, the spaced commands sent must not be empty strings as the count will end!





Revision as of 03:34, 27 May 2015

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


Syntax

option$ = COMMAND$[(count%)]


  • Useful when the programmer wants to send specific program options to the command line for later use by the called program.
  • The STRING return value is any unquoted or quoted spaced parameter(s) following the filename in a RUN or command line statement.
  • QB64 does not require or return all uppercase values so keep that fact in mind when checking parameters passed!
  • In QB64 only, COMMAND$ can work as an array to return specific elements passed to the command line. COMMAND$(2) would return the spaced second parameter passed at the command line. This can be used on modern operating systems to successfully retrieve file names and arguments which contain spaces properly. (versions after May 20, 2015)
  • Use the _COMMANDCOUNT function to find the number of spaced parameters passed to a program via the command line. (versions after May 20, 2015) See Example 2.
  • Reading the spaced command options in the COMMAND$(i) array in a loop can also be done and reading a COMMAND$ without parameters is also possible. (versions after May 20, 2015) See Example 3.
  • COMMAND$ was not available in QuickBasic versions below 4.0 and returned uppercase STRING parameters no matter what case they were sent originally.


Example 1: Compile both programs. ProgramA RUNs ProgramB with a parameter passed following the filename:

LOCATE 12, 36: PRINT "ProgramA" LOCATE 23, 25: PRINT "Press any key to run ProgramB" K$ = INPUT$(1) RUN "ProgramB FS" 'pass FS parameter to ProgramB in QB64 END

ProgramB checks for fullscreen parameter pass in QB64 and goes full screen.

LOCATE 12, 36: PRINT "ProgramB" parameter$ = UCASE$(COMMAND$) LOCATE 20, 33: PRINT "Parameter = " + parameter$ IF LEFT$(parameter$, 2) = "FS" THEN _FULLSCREEN 'parameter changes to full screen END

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.

count = _COMMANDCOUNT FOR c = 1 TO count PRINT COMMAND$(c) 'or process commands sent NEXT

-1 a data file

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 the quoted "a data file" option 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:

DO count = count + 1 cmd$ = COMMAND$(count) IF cmd$ = "" THEN EXIT DO 'read until an empty return PRINT cmd$ 'or process commands sent LOOP count = count - 1 'save the number of parameters sent to this program when run

Note: When using this command DO loop read procedure, the spaced commands sent must not be empty strings as the count will end!


See also:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page