@@ -36,10 +36,13 @@ import annotation._
36
36
* Note that main function overloading is not currently supported, i.e. you cannot define two main methods that have
37
37
* the same name in the same project.
38
38
*
39
- * A special argument is used to display help regarding a main function: `--help`. If used as argument, the program
39
+ * Special arguments are used to display help regarding a main function: `--help` and `-h `. If used as argument, the program
40
40
* will display some useful information about the main function. This help directly uses the ScalaDoc comment
41
41
* associated with the function, more precisely its description and the description of the parameters documented with
42
- * `@param`.
42
+ * `@param`. Note that if a parameter is named `help` or `h`, or if one of the parameters has as alias one of those names,
43
+ * the help displaying will be disabled for that argument.
44
+ * For example, for `@main def foo(help: Boolean)`, `scala foo -h` will display the help, but `scala foo --help` will fail,
45
+ * as it will expect a Boolean value after `--help`.
43
46
*
44
47
* Parameters may be given annotations to add functionalities to the main function:
45
48
* - `main.Alias` adds other names to a parameter. For example, if a parameter `node` has as aliases
@@ -69,6 +72,20 @@ final class main extends MainAnnotation:
69
72
private val argMarker = " --"
70
73
private val shortArgMarker = " -"
71
74
75
+ /**
76
+ * The name of the special argument to display the method's help.
77
+ * If one of the method's parameters is called the same, will be ignored.
78
+ */
79
+ private val helpArg = " help"
80
+ private var helpIsOverridden = false
81
+
82
+ /**
83
+ * The short name of the special argument to display the method's help.
84
+ * If one of the method's parameters uses the same short name, will be ignored.
85
+ */
86
+ private val shortHelpArg = 'h'
87
+ private var shortHelpIsOverridden = false
88
+
72
89
private val maxUsageLineLength = 120
73
90
74
91
/** A map from argument canonical name (the name of the parameter in the method definition) to parameter informations */
@@ -90,6 +107,9 @@ final class main extends MainAnnotation:
90
107
names.map(_ -> canonicalName)
91
108
).toMap
92
109
110
+ helpIsOverridden = namesToCanonicalName.exists((name, _) => name == helpArg)
111
+ shortHelpIsOverridden = shortNamesToCanonicalName.exists((name, _) => name == shortHelpArg)
112
+
93
113
def getCanonicalArgName (arg : String ): Option [String ] =
94
114
if arg.startsWith(argMarker) && arg.length > argMarker.length then
95
115
namesToCanonicalName.get(arg.drop(argMarker.length))
@@ -305,7 +325,10 @@ final class main extends MainAnnotation:
305
325
for (remainingArg <- positionalArgs) error(s " unused argument: $remainingArg" )
306
326
for (invalidArg <- invalidByNameArgs) error(s " unknown argument name: $invalidArg" )
307
327
308
- if args.contains(s " ${argMarker}help " ) then
328
+ val displayHelp =
329
+ (! helpIsOverridden && args.contains(getNameWithMarker(helpArg))) || (! shortHelpIsOverridden && args.contains(getNameWithMarker(shortHelpArg)))
330
+
331
+ if displayHelp then
309
332
usage()
310
333
println()
311
334
explain()
0 commit comments