COMMAND$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:




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


Line 14: Line 14:


{{PageExamples}}
{{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}}
{{Cl|LOCATE}} 12, 36: {{Cl|PRINT}} "ProgramA"
{{Cl|LOCATE}} 12, 36: {{Cl|PRINT}} "ProgramA"
Line 24: Line 24:
{{Cl|SYSTEM}}
{{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}} 17, 36: {{Cl|PRINT}} "ProgramB"
{{Cl|LOCATE}} 17, 36: {{Cl|PRINT}} "ProgramB"
parameter$ = {{Cl|UCASE$}}({{Cl|COMMAND$}}) 'UCASE$ is needed in QB64 only, as QB4.5 will always return upper case
parameter$ = {{Cl|UCASE$}}({{Cl|COMMAND$}}) 'UCASE$ is needed in QB64 only, as QB4.5 will always return upper case
Line 31: Line 31:
{{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


{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 42: Line 42:




''Example 2:'' Program gets the number of parameters passed to the program, and then prints those parameters to the screen one at a time.  
''Example 2:'' Program gets the number of parameters passed to the program, and then prints those parameters to the screen one at a time.
{{CodeStart}}count = {{Cl|_COMMANDCOUNT}}
{{CodeStart}}count = {{Cl|_COMMANDCOUNT}}
{{Cl|FOR...NEXT|FOR}} c = 1 {{Cl|TO}} count
{{Cl|FOR...NEXT|FOR}} c = 1 {{Cl|TO}} count
Line 54: Line 54:




''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]]):  
''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 66: Line 66:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [https://qb64phoenix.com/forum/showthread.php?tid=1271 Featured in our "Keyword of the Day" series]
* [[SHELL]], [[RUN]]
* [[SHELL]], [[RUN]]
* [[UCASE$]], [[LCASE$]]
* [[UCASE$]], [[LCASE$]]

Latest revision as of 17:59, 25 May 2024

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:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link