Command Line Argument in Scala
The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings.
runtime. But it never accepts parameters from any other method in the program.
Syntax:
def main(args: Array[String])
For accessing our Scala command-line arguments using the args array, which is made available to us implicitly when we extend App. Here is an example.
Example 1: Print all given objects
// Scala Program on command line argument object CMDExample { // Main method def main(args: Array[String]) { println("Scala Command Line Argument Example"); // You pass any thing at runtime // that will be print on the console for(arg<-args) { println(arg); } } } |
To Compile and execute the above program on terminal follow below commands :
First save program CMDExample.scala then open CMD/Terminal and go on that directory where you save your scala program.
Compile: scalac CMDExample.scala
Execute: scala CMDExample Welcome To GeeksforGeeks!
Output:
Scala Comand Line Argument Example Welcome To GeeksforGeeks!

Example 2: Print some object which is given at runtime
// Scala Program on command line argument object CMDExample { // Main method def main(args: Array[String]) { println("Scala Command Line Argument Example"); // You pass any thing at runtime // that will be print on the console println(args(0)); println(args(2)); } } |
To Compile and execute the above program on terminal follow below commands :
Compile: scalac CMDExample.scala
Execute: scala CMDExample 1 Welcome To GeeksforGeeks! 2
Output:
Scala Command Line Argument Example 1 To GeeksforGeeks!

Note:If given index not present in array then you find this error

Recommended Posts:
- Argument vs Parameter in Java
- How to break line without using <br> tag in HTML / CSS ?
- How to insert line break before an element using CSS?
- How to make a vertical line using HTML ?
- How to remove line breaks from the string in PHP?
- How to put string in array, split by new line in PHP ?
- CSS | text-decoration-line Property
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala Map
- Scala | Either
- How to remove all line breaks from a string using JavaScript?
- How to prevent line breaks in the list of items using CSS?
- How to make horizontal line with words in the middle using CSS?
- How to create multi-line strings in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



