Scala – Command Line Argument

  • date 29th December, 2020 |
  • by Prwatech |
  • 0 Comments

Command Line Argument in Scala

 

Command-line arguments allow Scala applications to accept input parameters from the terminal when executed. These arguments provide a way to customize the behavior of the program without modifying its source code. Scala provides access to command-line arguments through the args array within the main method of the application.

To access command-line arguments in Scala, you can define the main method with a parameter of type Array[String]:

object MyApp {
def main(args: Array[String]): Unit = {
// Access command-line arguments via the args array
println("Number of arguments: " + args.length)
args.foreach(arg => println("Argument: " + arg))
}
}

In this example, args will be an array containing ["arg1", "arg2", "arg3"].

When you run a Scala application from the command line, you can pass arguments after the application name:

scala MyApp.scala arg1 arg2 arg3 

Command-line arguments are commonly used to configure application settings, specify input files or directories, or control program behavior based on user input. Understanding how to handle command-line arguments in Scala enables developers to create more flexible and customizable applications that can be easily configured and adapted for different use cases.

Arguments which are pass by the generator to the main() method are termed as Command.

Note: But it never accepts parameters from any other method in the program

                 Run the following program for the reference:

OUTPUT:

 

Command Line Argument in Scala

Quick Support

image image