Skip to content

Commit

Permalink
Make use of MainArgs in example/basic/ (#2409)
Browse files Browse the repository at this point in the history
We don't need to use it in all the examples, but I added it to a few.
That means that someone following the examples will at least get some
hints about how to do command line argument parsing, which is a common
requirement when writing small CLI tools and utilities.

Not strictly Mill/build-config related, but definitely handy for what we
expect people to want to do with Mill
  • Loading branch information
lihaoyi authored Apr 4, 2023
1 parent 27e7973 commit ab05091
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 40 deletions.
22 changes: 16 additions & 6 deletions example/basic/1-hello-world/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import mill._, scalalib._

object foo extends RootModule with ScalaModule {
def scalaVersion = "2.13.2"
def ivyDeps = Agg(ivy"com.lihaoyi::scalatags:0.8.2")
def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags:0.8.2",
ivy"com.lihaoyi::mainargs:0.4.0"
)
}

// This is a basic Mill build for a single `ScalaModule`, with a single
Expand All @@ -11,6 +14,10 @@ object foo extends RootModule with ScalaModule {
// directly perform operations `./mill compile` or `./mill run` without needing
// to prefix it as `foo.compile` or `foo.run`.
//
// This example project uses two third-party dependencies - MainArgs for CLI
// argument parsing, Scalatags for HTML generation - and uses them to wrap a
// given input string in HTML templates with proper escaping.
//
// You can run `assembly` to generate a standalone executable jar, which then
// can be run from the command line or deployed to be run elsewhere.

Expand All @@ -20,15 +27,18 @@ object foo extends RootModule with ScalaModule {
compiling 1 Scala source
> ./mill run
Foo.value: <h1>hello</h1>
error: Missing argument: --text <str>
> ./mill run --text hello
<h1>hello</h1>
> ./mill show assembly
out/assembly.dest/out.jar
> java -jar ./out/assembly.dest/out.jar
Foo.value: <h1>hello</h1>
> java -jar ./out/assembly.dest/out.jar --text hello
<h1>hello</h1>
> ./out/assembly.dest/out.jar # mac/linux
Foo.value: <h1>hello</h1>
> ./out/assembly.dest/out.jar --text hello # mac/linux
<h1>hello</h1>
*/
10 changes: 7 additions & 3 deletions example/basic/1-hello-world/src/Foo.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package foo
import scalatags.Text.all._
import mainargs.{main, ParserForMethods}
object Foo {
val value = h1("hello")
def main(args: Array[String]): Unit = {
println("Foo.value: " + Foo.value)
@main
def main(text: String) = {
val value = h1(text)
println(value)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
9 changes: 5 additions & 4 deletions example/basic/3-custom-tasks/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ object foo extends RootModule with ScalaModule {

def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags:0.8.2",
ivy"com.lihaoyi::mainargs:0.4.0",
ivy"com.lihaoyi::os-lib:0.9.1",
)

Expand Down Expand Up @@ -62,8 +63,8 @@ object foo extends RootModule with ScalaModule {

/* Example Usage
> ./mill run
Foo.value: <h1>hello</h1>
MyDeps.value: List((com.lihaoyi,scalatags,0.8.2), (com.lihaoyi,os-lib,0.9.1))
my.line.count: 10
> ./mill run --text hello
value: <h1>hello</h1>
MyDeps.value: List((com.lihaoyi,scalatags,0.8.2), (com.lihaoyi,mainargs,0.4.0), (com.lihaoyi,os-lib,0.9.1))
my.line.count: 14
*/
10 changes: 7 additions & 3 deletions example/basic/3-custom-tasks/src/Foo.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package foo
import scalatags.Text.all._
import mainargs.{main, ParserForMethods}
object Foo {
val value = h1("hello")
def main(args: Array[String]): Unit = {
println("Foo.value: " + Foo.value)
@main
def main(text: String): Unit = {
val value = h1(text)
println("value: " + value)
println("MyDeps.value: " + MyDeps.value)
println("my.line.count: " + sys.props("my.line.count"))
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
11 changes: 7 additions & 4 deletions example/basic/4-multi-module/bar/src/Bar.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package bar
import scalatags.Text.all._
import mainargs.{main, ParserForMethods}
object Bar {
val value = p("world")

def main(args: Array[String]): Unit = {
println("Bar.value: " + bar.Bar.value)
@main
def main(text: String): Unit = {
val value = p("world")
println("Bar.value: " + value)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
9 changes: 6 additions & 3 deletions example/basic/4-multi-module/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import mill._, scalalib._

trait MyModule extends ScalaModule{
def scalaVersion = "2.13.2"
def ivyDeps = Agg(ivy"com.lihaoyi::scalatags:0.8.2")
def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags:0.8.2",
ivy"com.lihaoyi::mainargs:0.4.0"
)
}

object foo extends MyModule {
Expand All @@ -27,11 +30,11 @@ object bar extends MyModule
foo.run
bar.run
> ./mill foo.run
> ./mill foo.run --foo-text hello --bar-text world
Foo.value: <h1>hello</h1>
Bar.value: <p>world</p>
> ./mill bar.run
> ./mill bar.run --text world
Bar.value: <p>world</p>
*/
9 changes: 7 additions & 2 deletions example/basic/4-multi-module/foo/src/Foo.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package foo
import scalatags.Text.all._
import mainargs.{main, ParserForMethods, arg}
object Foo {
val value = h1("hello")

def main(args: Array[String]): Unit = {
@main
def main(@arg(name = "foo-text") fooText: String,
@arg(name = "bar-text") barText: String): Unit = {
println("Foo.value: " + Foo.value)
println("Bar.value: " + bar.Bar.value)
bar.Bar.main(barText)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
10 changes: 6 additions & 4 deletions example/basic/5-nested-modules/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import mill._, scalalib._

trait MyModule extends ScalaModule{
def scalaVersion = "2.13.2"
def ivyDeps = Agg(ivy"com.lihaoyi::scalatags:0.8.2")
def ivyDeps = Agg(
ivy"com.lihaoyi::scalatags:0.8.2",
ivy"com.lihaoyi::mainargs:0.4.0"
)
}

object wrapper extends Module{
Expand Down Expand Up @@ -32,13 +35,12 @@ wrapper.foo.run
wrapper.bar.run
qux.run
> ./mill qux.run
> ./mill qux.run --foo-text hello --bar-text world --qux-text today
Foo.value: <h1>hello</h1>
Bar.value: <p>world</p>
Qux.value: <p>today</p>
> ./mill wrapper.foo.run
> ./mill wrapper.foo.run --text hello
Foo.value: <h1>hello</h1>
Bar.value: <p>world</p>
*/
18 changes: 13 additions & 5 deletions example/basic/5-nested-modules/qux/src/Qux.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package qux
import scalatags.Text.all._
import mainargs.{main, ParserForMethods, arg}
object Qux {
val value = p("today")

def main(args: Array[String]): Unit = {
println("Foo.value: " + foo.Foo.value)
println("Bar.value: " + bar.Bar.value)
println("Qux.value: " + qux.Qux.value)

@main
def main(@arg(name="foo-text") fooText: String,
@arg(name="bar-text") barText: String,
@arg(name="qux-text") quxText: String): Unit = {
foo.Foo.main(fooText)
bar.Bar.main(barText)

val value = p(quxText)
println("Qux.value: " + value)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
10 changes: 9 additions & 1 deletion example/basic/5-nested-modules/wrapper/bar/src/Bar.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package bar
import scalatags.Text.all._
import mainargs.{main, ParserForMethods}
object Bar {
val value = p("world")

@main
def main(text: String): Unit = {
val value = p(text)
println("Bar.value: " + value)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}
12 changes: 7 additions & 5 deletions example/basic/5-nested-modules/wrapper/foo/src/Foo.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package foo
import scalatags.Text.all._
import mainargs.{main, ParserForMethods}
object Foo {
val value = h1("hello")

def main(args: Array[String]): Unit = {
println("Foo.value: " + Foo.value)
println("Bar.value: " + bar.Bar.value)
@main
def main(text: String): Unit = {
val value = h1(text)
println("Foo.value: " + value)
}

def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
}

0 comments on commit ab05091

Please sign in to comment.