View Full Version : Passing Arguments To Tbasic Program
peralta_mike
02-11-2011, 19:49
How do I pass arguments to a thinbasic program?
Specifically how do I access the argument values that
I pass to a thinbasic program?
For example I want to pass the two arguments 3.14 and 1.717
to the thinbasic program myprog.bas as follows:
thinbasic myprog.bas 3.14 1.717
1. Is passing arguments even possible in this way?
2. How do I access these argument values within myprog.bas
Thanks for any help.
Petr Schreiber
02-11-2011, 20:47
Interesting question,
I think lot of people would like to do this ... and it is possible :)
This functionality is handled by OS module, by functions:
OS_GetCommand
OS_GetCommands
OS_CommandPresent
OS_CommandsGetSep
OS_CommandsSetSep
You can read more about them in the help file + see the sample code thinBasic\SampleScripts\OS\OS_SampleCommandLine.tBasic.
Here more elemental example to handle your task:
Uses "OS" ' -- For command line handling
Uses "Console"
Long i
' -- When launched from thinAir, the first param is @1 and the second name of the script
' -- When launched independently, the first param is name of the script
For i = 1 To OS_GetCommands
PrintL "Parameter #"+Format$(i), OS_GetCommand(i)
PrintL
Next
' -- Because of this behavior, the parameters we need are the prev-to-last and last one
Single param1 = OS_GetCommand(OS_GetCommands-1)
Single param2 = OS_GetCommand(OS_GetCommands)
MsgBox 0, "Number #1 = " + Format$(param1) + $CRLF +
"Number #2 = " + Format$(param2)
WaitKey
To test how it works, simply enter this in ThinAir, and then click Script/Command line and enter the two numbers separated by space.
Petr
peralta_mike
03-11-2011, 00:12
Thanks Petr.
That works.
:dance1: :drink: :dance1:
peralta_mike
03-11-2011, 17:24
I was trying to pass the arguments 1e-9 and 10e-9
but the program splits 1e-9 into 1e and -9. :unguee:
Can I control the separator characters?
My command line is:
thinbasic vt.bas mosdata1.txt 1e-9 10e-9
================
Answer: To use spaces as the separator character use the following program line
OS_CommandsSetSep(" ")
before using the functions OS_GetCommands and OS_GetCommand()
( I include this post in case others run into this problem. )
:dance1:
Petr Schreiber
03-11-2011, 19:12
Thanks for sharing the solution!
Petr