locked
Using Arguments.GetArguments RRS feed

  • Question

  • In the original version of SmallBasic it was not possible to pass command line arguments to a program.  However, now it appears to be possible with the Arguments.GetArguments feature.  I am curious as to how to use this exactly.  I know that I have to supply an argument index, but is this index a line inside my code at the beginning of the file?  Where are the arguments coming from and how do I reference them in the index?
    Monday, August 24, 2009 4:42 PM

Answers

  • Here is a simple Paper, Rock, Scissors example for how to use arguments,
    'PRS
    'the user enters an item (paper, rock, scissors, 1, 2, 3)
    Comp = Math.GetRandomNumber(3) 'check to see if the user gave an arguement If (Program.ArgumentCount = 1) Then 'play paper, rock, scissors User = Program.GetArgument(1) User = Text.ConvertToUpperCase(User) 'convert user to number, use StartsWith to avoid spelling errors If (Text.StartsWith(User, "P") Or User = 1) Then User = 1 ElseIf (Text.StartsWith(User, "R") Or User = 2) Then User = 2 ElseIf (Text.StartsWith(User, "S") Or User = 3) Then User = 3 EndIf 'decide winner and announce If (User = 3 And Comp = 1) Then TextWindow.WriteLine("Scissors cut paper, You win!") ElseIf (User = 1 And Comp = 3) Then TextWindow.WriteLine("Scissors cut paper, Computer wins!") ElseIf (User = 2 And Comp = 3) Then TextWindow.WriteLine("Rock beats scissors, You win!") ElseIf (User = 3 And Comp = 2) Then TextWindow.WriteLine("Rock beats scissors, Computer wins!") ElseIf (User = 1 And Comp = 2) Then TextWindow.WriteLine("Paper covers rock, You win!") ElseIf (User = 2 And Comp = 1) Then TextWindow.WriteLine("Paper covers rock, Computer wins!") EndIf Else TextWindow.WriteLine("Please enter one argument after the program name in a command prompt to play.") EndIf

    Save this as "PRS"

    At the command prompt type - path + exe <space> argument(s)
    ex.
      C:\Documents and Settings\Owner\My Documents\SmallBasic>prs paper

    Output-
    Scissors cut paper, Computer wins!
    Press any key to continue...
    • Marked as answer by EricTheGeek Monday, August 24, 2009 7:48 PM
    Monday, August 24, 2009 6:22 PM

All replies

  • SmallBasic programs are compiled into exe file. If you run such file with an arguments - you can access these arguments with Arguments.GetArguments. It works perfectly.
    Grzesio
    Monday, August 24, 2009 4:47 PM
  • Here is a simple Paper, Rock, Scissors example for how to use arguments,
    'PRS
    'the user enters an item (paper, rock, scissors, 1, 2, 3)
    Comp = Math.GetRandomNumber(3) 'check to see if the user gave an arguement If (Program.ArgumentCount = 1) Then 'play paper, rock, scissors User = Program.GetArgument(1) User = Text.ConvertToUpperCase(User) 'convert user to number, use StartsWith to avoid spelling errors If (Text.StartsWith(User, "P") Or User = 1) Then User = 1 ElseIf (Text.StartsWith(User, "R") Or User = 2) Then User = 2 ElseIf (Text.StartsWith(User, "S") Or User = 3) Then User = 3 EndIf 'decide winner and announce If (User = 3 And Comp = 1) Then TextWindow.WriteLine("Scissors cut paper, You win!") ElseIf (User = 1 And Comp = 3) Then TextWindow.WriteLine("Scissors cut paper, Computer wins!") ElseIf (User = 2 And Comp = 3) Then TextWindow.WriteLine("Rock beats scissors, You win!") ElseIf (User = 3 And Comp = 2) Then TextWindow.WriteLine("Rock beats scissors, Computer wins!") ElseIf (User = 1 And Comp = 2) Then TextWindow.WriteLine("Paper covers rock, You win!") ElseIf (User = 2 And Comp = 1) Then TextWindow.WriteLine("Paper covers rock, Computer wins!") EndIf Else TextWindow.WriteLine("Please enter one argument after the program name in a command prompt to play.") EndIf

    Save this as "PRS"

    At the command prompt type - path + exe <space> argument(s)
    ex.
      C:\Documents and Settings\Owner\My Documents\SmallBasic>prs paper

    Output-
    Scissors cut paper, Computer wins!
    Press any key to continue...
    • Marked as answer by EricTheGeek Monday, August 24, 2009 7:48 PM
    Monday, August 24, 2009 6:22 PM