@@ -36,10 +36,13 @@ import annotation._
3636 * Note that main function overloading is not currently supported, i.e. you cannot define two main methods that have
3737 * the same name in the same project.
3838 *
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
4040 * will display some useful information about the main function. This help directly uses the ScalaDoc comment
4141 * 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`.
4346 *
4447 * Parameters may be given annotations to add functionalities to the main function:
4548 * - `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:
6972 private val argMarker = " --"
7073 private val shortArgMarker = " -"
7174
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+
7289 private val maxUsageLineLength = 120
7390
7491 /** 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:
90107 names.map(_ -> canonicalName)
91108 ).toMap
92109
110+ helpIsOverridden = namesToCanonicalName.exists((name, _) => name == helpArg)
111+ shortHelpIsOverridden = shortNamesToCanonicalName.exists((name, _) => name == shortHelpArg)
112+
93113 def getCanonicalArgName (arg : String ): Option [String ] =
94114 if arg.startsWith(argMarker) && arg.length > argMarker.length then
95115 namesToCanonicalName.get(arg.drop(argMarker.length))
@@ -305,7 +325,10 @@ final class main extends MainAnnotation:
305325 for (remainingArg <- positionalArgs) error(s " unused argument: $remainingArg" )
306326 for (invalidArg <- invalidByNameArgs) error(s " unknown argument name: $invalidArg" )
307327
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
309332 usage()
310333 println()
311334 explain()
0 commit comments