# Mainargs: a small, convenient, dependency-free library for command-line argument parsing in Scala

**URL:** <https://contributors.scala-lang.org/t/mainargs-a-small-convenient-dependency-free-library-for-command-line-argument-parsing-in-scala/4661>\
**Category:** Tooling\
**Created:** [November 26, 2020, 6:05am UTC](https://contributors.scala-lang.org/t/mainargs-a-small-convenient-dependency-free-library-for-command-line-argument-parsing-in-scala/4661 "2020-11-26T06:05:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [November 26, 2020, 6:05am UTC](https://contributors.scala-lang.org/t/mainargs-a-small-convenient-dependency-free-library-for-command-line-argument-parsing-in-scala/4661/1 "2020-11-26T06:05:38Z")

</div>

Mainargs is a small, convenient, dependency-free library for command-line argument parsing in Scala:

- [https://github.com/lihaoyi/mainargs](https://github.com/lihaoyi/mainargs)

To get started using MainArgs, all you need is a single method annotated with `@main`:

```scala
package testhello
import mainargs.{main, arg, ParserForMethods, Flag}

object Main{
  @main
  def run(@arg(short = 'f', doc = "String to print repeatedly")
          foo: String,
          @arg(name = "my-num", doc = "How many times to print string")
          myNum: Int = 2,
          @arg(doc = "Example flag, can be passed without any value to become true")
          bool: Flag) = {
    println(foo * myNum + " " + bool.value)
  }
  def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}

```

MainArgs is the outcome of unifying Ammonite Scala Script’s user-defined `@main` method implementation, with Ammonite’s own argument parser, with Mill’s user-defined `T.command` implementation, and Mill’s own argument parser. MainArgs is thus flexible enough support a wide range of use cases, while providing a convenient and boilerplate-free experience that scales down to the smallest programs.

As MainArgs is heavily used in the latest versions of Ammonite and Mill, I expect that it is flexible enough to handle a wide range of use cases, and is solid enough for serious real-world usage. Feel free to make use of this in your own projects!

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [November 26, 2020, 6:23am UTC](https://contributors.scala-lang.org/t/mainargs-a-small-convenient-dependency-free-library-for-command-line-argument-parsing-in-scala/4661/2 "2020-11-26T06:23:53Z")

</div>

Very nice!  
What is the future of it in Scala-3 considering its own `@main` annotation.

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [November 26, 2020, 12:37pm UTC](https://contributors.scala-lang.org/t/mainargs-a-small-convenient-dependency-free-library-for-command-line-argument-parsing-in-scala/4661/3 "2020-11-26T12:37:50Z")

</div>

We will hopefully be able to merge the two. For the moment Scala-3’s `@main` only covers a small subset of what Mainargs can do. But the plan is to extend that.
