Using Command Line Arguments

The Arguments object in the standard library allows you to work with Command Line Arguments from inside a Small Basic program.  The Arguments object includes a Count property that gives the total number of arguments passed to the program, and a GetArgument(index) operation that allows you to access each of these arguments using their index.

The following program prints out all the prime numbers less than the number passed at the command line.

 If (Arguments.Count = 1) Then
  maxNumber = Arguments.GetArgument(1)
  
  If (maxNumber < 2) Then
    TextWindow.WriteLine("No Primes less than 2.")
  EndIf
  
  For i = 2 To maxNumber
    CheckPrime()
    If (isPrime = "True") Then
      TextWindow.WriteLine(i)
    EndIf
  EndFor
Else
  TextWindow.WriteLine("Usage: prime.exe ")
EndIf

Sub CheckPrime
  isPrime = "True"
  For j = 2 To Math.SquareRoot(i)
    If (Math.Remainder(i, j) = 0) Then
      isPrime = "False"
    EndIf
  EndFor
EndSub

In this program, Small Basic tries to coerce any passed argument as a number.  If you try passing a non-number, the program ends up with “0” and will print out “No Primes less than 2.”