1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-06-29 11:40:38 +00:00
QB64-PE/internal/help/COMMAND$.txt
SteveMcNeill 33adc04fc4 Add temp folder to repo. It's necessary as well!
Just more initial setting on... nothing much to see here.
2022-04-28 13:39:56 -04:00

75 lines
3.1 KiB
Plaintext

The '''COMMAND$''' function returns the command line argument(s) passed when a program is run.
{{PageSyntax}}
: {{Parameter|commandLine$}} = [[COMMAND$]][(count%)]
{{PageDescription}}
* 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 [[UCASE$|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.
{{PageExamples}}
''Example 1:'' Compile both programs. ProgramA [[RUN]]s ProgramB with a parameter passed following the filename:
{{CodeStart}}
{{Cl|LOCATE}} 12, 36: {{Cl|PRINT}} "ProgramA"
{{Cl|LOCATE}} 23, 25: {{Cl|PRINT}} "Press any key to run ProgramB"
K$ = {{Cl|INPUT$}}(1)
{{Cl|RUN}} "ProgramB FS" 'pass FS parameter to ProgramB in QB64 or QB4.5
{{Cl|SYSTEM}}
{{CodeEnd}}
: ''ProgramB'' checks for fullscreen parameter pass in QB64 and goes full screen.
{{CodeStart}} '' ''
{{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
{{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|END}} '' ''
{{CodeEnd}}
{{OutputStart}}
ProgramB
Parameter = FS.EXE
{{OutputEnd}}
''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}}
{{Cl|FOR...NEXT|FOR}} c = 1 {{Cl|TO}} count
{{Cl|PRINT}} {{Cl|COMMAND$}}(c) 'or process commands sent
{{Cl|NEXT}}
{{CodeEnd}}
{{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.
''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
count = count + 1
cmd$ = {{Cl|COMMAND$}}(count)
{{Cl|IF...THEN|IF}} cmd$ = "" {{Cl|THEN}} {{Cl|EXIT DO}} 'read until an empty return
{{Cl|PRINT}} cmd$ 'or process commands sent
{{Cl|LOOP}} '' ''
count = count - 1 'save the number of parameters sent to this program when run
{{CodeEnd}}
{{PageSeeAlso}}
* [[SHELL]], [[RUN]]
* [[UCASE$]], [[LCASE$]]
* [[_COMMANDCOUNT]]
{{PageNavigation}}
<