Difference between revisions of "COMMAND$"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>SMcNeill
m (-)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The '''COMMAND$''' [[STRING]] function returns the spaced [[DOS]] command line argument(s) passed when a program is run.
The '''COMMAND$''' function returns the command line argument(s) passed when a program is run.
 




{{PageSyntax}}  
{{PageSyntax}}  
::: option$ = [[COMMAND$]][(count%)]
: {{Parameter|commandLine$}} = [[COMMAND$]][(count%)]




* Useful when the programmer wants to send specific program options to the command line for later use by the called program.
{{PageDescription}}
* The [[STRING]] return value is any '''unquoted or quoted spaced''' parameter(s) following the filename in a [[RUN]] or command line statement.
* The [[STRING]] return value is anything typed after a program's executable file name in command line (or using the [[RUN]] statement).
* '''QB64''' does not '''require or return all [[UCASE$|uppercase]]''' values so keep that fact in mind when checking parameters passed!
* Unlike QuickBASIC, '''QB64''' does not return all [[UCASE$|uppercase]] values so keep that in mind when checking parameters.
* 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)
* In '''QB64''', COMMAND$ works as an array to return specific elements passed to the command line. COMMAND$(2) would return the second parameter passed at 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.
* 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}}'''.
* Use the [[_COMMANDCOUNT]] function to find the number of parameters passed to a program via the command line. See ''Example 2'' below.
* 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}}'''.
* COMMAND$ was '''not available in QuickBasic versions below 4.0''' and returned [[UCASE$|uppercase]] [[STRING]] parameters no matter what case they were sent originally.  




{{PageExamples}}
''Example 1:'' Compile both programs. ProgramA [[RUN]]s ProgramB with a parameter passed following the filename:  
''Example 1:'' Compile both programs. ProgramA [[RUN]]s ProgramB with a parameter passed following the filename:  
{{CodeStart}}
{{CodeStart}}
Line 22: Line 20:
{{Cl|LOCATE}} 23, 25: {{Cl|PRINT}} "Press any key to run ProgramB"
{{Cl|LOCATE}} 23, 25: {{Cl|PRINT}} "Press any key to run ProgramB"
K$ = {{Cl|INPUT$}}(1)
K$ = {{Cl|INPUT$}}(1)
{{Cl|RUN}} "ProgramB FS"  'pass FS parameter to ProgramB in QB64
{{Cl|RUN}} "ProgramB FS"  'pass FS parameter to ProgramB in QB64 or QB4.5


{{Cl|END}}
{{Cl|SYSTEM}}
{{CodeEnd}}
{{CodeEnd}}
: ''ProgramB'' checks for fullscreen parameter pass in QB64 and goes full screen.  
: ''ProgramB'' checks for fullscreen parameter pass in QB64 and goes full screen.  
{{CodeStart}} '' ''
{{CodeStart}} '' ''
{{Cl|LOCATE}} 12, 36: {{Cl|PRINT}} "ProgramB"
{{Cl|LOCATE}} 17, 36: {{Cl|PRINT}} "ProgramB"
parameter$ = {{Cl|UCASE$}}({{Cl|COMMAND$}})
parameter$ = {{Cl|UCASE$}}({{Cl|COMMAND$}}) 'UCASE$ is needed in QB64 only, as QB4.5 will always return upper case
{{Cl|LOCATE}} 20, 33: {{Cl|PRINT}} "Parameter = " + parameter$
{{Cl|LOCATE}} 20, 33: {{Cl|PRINT}} "Parameter = " + parameter$
{{Cl|IF...THEN|IF}} {{Cl|LEFT$}}(parameter$, 2) = "FS" {{Cl|THEN}} {{Cl|_FULLSCREEN}} 'parameter changes to full screen
{{Cl|IF...THEN|IF}} {{Cl|LEFT$}}(parameter$, 2) = "FS" {{Cl|THEN}} {{Cl|_FULLSCREEN}} 'parameter changes to full screen
Line 35: Line 33:
{{Cl|END}} '' ''
{{Cl|END}} '' ''
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}                             Parameter = FS.EXE
{{OutputStart}}
                                    ProgramB
 
 
 
                                Parameter = FS.EXE
{{OutputEnd}}
{{OutputEnd}}


Line 51: Line 54:




''Example 3:'' As part of the command array upgrade, you can also just read the array to see how many commands were sent:  
''Example 3:'' As part of the command array syntax, you can also just read the array to see how many commands were sent (or simply check [[_COMMANDCOUNT]]):  
{{CodeStart}}DO
{{CodeStart}}DO
     count = count + 1
     count = count + 1
Line 60: Line 63:
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 spaced commands sent must not be empty strings as the count will end!




''See also:''
{{PageSeeAlso}}
* [[SHELL]], [[RUN]]
* [[SHELL]], [[RUN]]
* [[UCASE$]], [[LCASE$]]
* [[UCASE$]], [[LCASE$]]

Latest revision as of 16:36, 19 September 2017

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


Syntax

commandLine$ = COMMAND$[(count%)]


Description

  • The STRING return value is anything typed after a program's executable file name in command line (or using the RUN statement).
  • Unlike QuickBASIC, QB64 does not return all uppercase values so keep that in mind when checking parameters.
  • In QB64, COMMAND$ works as an array to return specific elements passed to the command line. COMMAND$(2) would return the second parameter passed at 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.
  • Use the _COMMANDCOUNT function to find the number of parameters passed to a program via the command line. See Example 2 below.


Examples

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 or QB4.5 SYSTEM

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

LOCATE 17, 36: PRINT "ProgramB" parameter$ = UCASE$(COMMAND$) 'UCASE$ is needed in QB64 only, as QB4.5 will always return upper case LOCATE 20, 33: PRINT "Parameter = " + parameter$ IF LEFT$(parameter$, 2) = "FS" THEN _FULLSCREEN 'parameter changes to full screen END

ProgramB 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 syntax, you can also just read the array to see how many commands were sent (or simply check _COMMANDCOUNT):

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


See also



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