Skip to content

Commit 3a566ff

Browse files
Add more thorough documentation for main
1 parent 92db848 commit 3a566ff

File tree

1 file changed

+90
-4
lines changed

1 file changed

+90
-4
lines changed

library/src/scala/main.scala

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,101 @@ import collection.mutable
1212
import annotation._
1313

1414
/**
15-
* An annotation that designates a main function.
16-
* @param maxLineLength the maximum number of characters to print on a single line when
17-
* displaying the help
15+
* The annotation that designates a main function.
16+
* Main functions are entry points for Scala programs. They can be called through a command line interface by using
17+
* the `scala` command, followed by their name and, optionally, their parameters.
18+
*
19+
* The parameters of a main function may have any type `T`, as long as there exists a
20+
* `given util.CommandLineParser.FromString[T]` in the scope. It will be used for parsing the string given as input
21+
* into the correct argument type.
22+
* These types already have parsers defined:
23+
* - String,
24+
* - Boolean,
25+
* - Byte, Short, Int, Long, Float, Double.
26+
*
27+
* The parameters of a main function may be passed either by position, or by name. Passing an argument positionaly
28+
* means that you give the arguments in the same order as the function's signature. Passing an argument by name means
29+
* that you give the argument right after giving its name. Considering the function
30+
* `@main def foo(i: Int, s: String)`, we may have arguments passed:
31+
* - by position: `scala foo 1 abc`,
32+
* - by name: `scala foo --i 1 --s abc` or `scala foo --s abc --i 1`.
33+
*
34+
* A mixture of both is also possible: `scala foo --s abc 1` is equivalent to all previous examples.
35+
*
36+
* Note that main function overloading is not currently supported, i.e. you cannot define two main methods that have
37+
* the same name in the same project.
38+
*
39+
* A special argument is used to display help regarding a main function: `--help`. If used as argument, the program
40+
* will display some useful information about the main function. This help directly uses the ScalaDoc comment
41+
* associated with the function, more precisely its description and the description of the parameters documented with
42+
* `@param`.
43+
*
44+
*
45+
* Parameters may be given annotations to add functionalities to the main function:
46+
* - `main.ShortName` adds a short name to a parameter. For example, if a parameter `node` has as short name `n`, it
47+
* may be addressed using either `--node` or `-n`,
48+
* - `main.Name` adds another name to a parameter. For example, if a parameter `node` has as alternative name
49+
* `otherNode`, it may be addressed using either `--node` or `--otherNode`.
50+
*
51+
* Here is an example of a main function with annotated parameters:
52+
* `@main def foo(@main.ShortName('x') number: Int, @main.Name("explanation") s: String)`. The following commands are
53+
* equivalent:
54+
* - `scala foo --number 1 --s abc`
55+
* - `scala foo -x 1 --s abc`
56+
* - `scala foo --number 1 --explanation abc`
57+
* - `scala foo -x 1 --explanation abc`
58+
*
59+
* @param maxLineLength the maximum number of characters to print on a single line when displaying the help
1860
*/
1961
final class main(maxLineLength: Int) extends MainAnnotation:
2062
import main._
2163
import MainAnnotation._
2264

23-
/** An annotation that designates a main function. */
65+
/**
66+
* The annotation that designates a main function.
67+
* Main functions are entry points for Scala programs. They can be called through a command line interface by using
68+
* the `scala` command, followed by their name and, optionally, their parameters.
69+
*
70+
* The parameters of a main function may have any type `T`, as long as there exists a
71+
* `given util.CommandLineParser.FromString[T]` in the scope. It will be used for parsing the string given as input
72+
* into the correct argument type.
73+
* These types already have parsers defined:
74+
* - String,
75+
* - Boolean,
76+
* - Byte, Short, Int, Long, Float, Double.
77+
*
78+
* The parameters of a main function may be passed either by position, or by name. Passing an argument positionaly
79+
* means that you give the arguments in the same order as the function's signature. Passing an argument by name means
80+
* that you give the argument right after giving its name. Considering the function
81+
* `@main def foo(i: Int, s: String)`, we may have arguments passed:
82+
* - by position: `scala foo 1 abc`,
83+
* - by name: `scala foo --i 1 --s abc` or `scala foo --s abc --i 1`.
84+
*
85+
* A mixture of both is also possible: `scala foo --s abc 1` is equivalent to all previous examples.
86+
*
87+
* Note that main function overloading is not currently supported, i.e. you cannot define two main methods that have
88+
* the same name in the same project.
89+
*
90+
* A special argument is used to display help regarding a main function: `--help`. If used as argument, the program
91+
* will display some useful information about the main function. This help directly uses the ScalaDoc comment
92+
* associated with the function, more precisely its description and the description of the parameters documented with
93+
* `@param`.
94+
*
95+
*
96+
* Parameters may be given annotations to add functionalities to the main function:
97+
* - `main.ShortName` adds a short name to a parameter. For example, if a parameter `node` has as short name `n`, it
98+
* may be addressed using either `--node` or `-n`,
99+
* - `main.Name` adds another name to a parameter. For example, if a parameter `node` has as alternative name
100+
* `otherNode`, it may be addressed using either `--node` or `--otherNode`.
101+
*
102+
* Here is an example of a main function with annotated parameters:
103+
* `@main def foo(@main.ShortName('x') number: Int, @main.Name("explanation") s: String)`. The following commands are
104+
* equivalent:
105+
* - `scala foo --number 1 --s abc`
106+
* - `scala foo -x 1 --s abc`
107+
* - `scala foo --number 1 --explanation abc`
108+
* - `scala foo -x 1 --explanation abc`
109+
*/
24110
def this() = this(120)
25111

26112
override type ArgumentParser[T] = util.CommandLineParser.FromString[T]

0 commit comments

Comments
 (0)