<commit_before>---
layout: doc-page
title: "Main Methods"
---

Scala 3 offers a new way to define programs that can be invoked from the command line:
A `@main` annotation on a method turns this method into an executable program.
Example:

```scala
@main def happyBirthday(age: Int, name: String, others: String*) =
   val suffix =
      age % 100 match
      case 11 | 12 | 13 => "th"
      case _ =>
         age % 10 match
         case 1 => "st"
         case 2 => "nd"
         case 3 => "rd"
         case _ => "th"
   val bldr = new StringBuilder(s"Happy $age$suffix birthday, $name")
   for other <- others do bldr.append(" and ").append(other)
   bldr.toString
```

This would generate a main program `happyBirthday` that could be called like this

```
> scala happyBirthday 23 <NAME>
Happy 23rd Birthday, Lisa and Peter!
```

A `@main` annotated method can be written either at the top-level or in a statically accessible object. The name of the program is in each case the name of the method, without any object prefixes. The `@main` method can have an arbitrary number of parameters.
For each parameter type there must be an instance of the `scala.util.CommandLineParser.FromString` type class
that is used to convert an argument string to the required parameter type.
The parameter list of a main method can end in a repeated parameter that then
takes all remaining arguments given on the command line.<commit_msg>Make output match the example program<commit_after>---
layout: doc-page
title: "Main Methods"
---

Scala 3 offers a new way to define programs that can be invoked from the command line:
A `@main` annotation on a method turns this method into an executable program.
Example:

```scala
@main def happyBirthday(age: Int, name: String, others: String*) =
   val suffix =
      age % 100 match
      case 11 | 12 | 13 => "th"
      case _ =>
         age % 10 match
         case 1 => "st"
         case 2 => "nd"
         case 3 => "rd"
         case _ => "th"
   val bldr = new StringBuilder(s"Happy $age$suffix birthday, $name")
   for other <- others do bldr.append(" and ").append(other)
   bldr.toString
```

This would generate a main program `happyBirthday` that could be called like this

```
> scala happyBirthday 23 <NAME>
Happy 23rd birthday, Lisa and Peter
```

A `@main` annotated method can be written either at the top-level or in a statically accessible object. The name of the program is in each case the name of the method, without any object prefixes. The `@main` method can have an arbitrary number of parameters.
For each parameter type there must be an instance of the `scala.util.CommandLineParser.FromString` type class
that is used to convert an argument string to the required parameter type.
The parameter list of a main method can end in a repeated parameter that then
takes all remaining arguments given on the command line.
{'fraction_non_alphanumeric': 0.07895652173913044, 'fraction_numerical': 0.013217391304347827, 'mean_word_length': 3.370820668693009, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4622935', 'n_tokens_mistral': 847, 'n_tokens_neox': 774, 'n_words': 414}