@@ -13,9 +13,17 @@ trait CliCommand:
13
13
14
14
type ConcreteSettings <: CommonScalaSettings with Settings .SettingGroup
15
15
16
+ def versionMsg : String
17
+
18
+ def ifErrorsMsg : String
19
+
16
20
/** The name of the command */
17
21
def cmdName : String
18
22
23
+ def isHelpFlag (using settings : ConcreteSettings )(using SettingsState ): Boolean
24
+
25
+ def helpMsg (using settings : ConcreteSettings )(using SettingsState , Context ): String
26
+
19
27
private def explainAdvanced = """
20
28
|-- Notes on option parsing --
21
29
|Boolean settings are always false unless set.
@@ -31,14 +39,6 @@ trait CliCommand:
31
39
| already are in phase X + 1.
32
40
"""
33
41
34
- def shortUsage : String = s " Usage: $cmdName <options> <source files> "
35
-
36
- def versionMsg : String = s " Scala $versionString -- $copyrightString"
37
-
38
- def ifErrorsMsg : String = " -help gives more information"
39
-
40
- def shouldStopWithInfo (using settings : ConcreteSettings )(using SettingsState ): Boolean
41
-
42
42
/** Distill arguments into summary detailing settings, errors and files to main */
43
43
def distill (args : Array [String ], sg : Settings .SettingGroup , ss : SettingsState ): ArgsSummary =
44
44
/**
@@ -64,11 +64,8 @@ trait CliCommand:
64
64
65
65
sg.processArguments(expandedArguments, ss, processAll = true )
66
66
67
-
68
- def infoMessage (using settings : ConcreteSettings )(using SettingsState )(using Context ): String
69
-
70
67
/** Creates a help message for a subset of options based on cond */
71
- def availableOptionsMsg (cond : Setting [? ] => Boolean )(using settings : ConcreteSettings )(using SettingsState ): String =
68
+ protected def availableOptionsMsg (cond : Setting [? ] => Boolean )(using settings : ConcreteSettings )(using SettingsState ): String =
72
69
val ss = (settings.allSettings filter cond).toList sortBy (_.name)
73
70
val width = (ss map (_.name.length)).max
74
71
def format (s : String ) = (" %-" + width + " s" ) format s
@@ -90,8 +87,9 @@ trait CliCommand:
90
87
91
88
ss.map(helpStr).mkString(" " , " \n " , s " \n ${format(" @<file>" )} A text file containing compiler arguments (options and source files). \n " )
92
89
90
+ protected def shortUsage : String = s " Usage: $cmdName <options> <source files> "
93
91
94
- def createUsageMsg (label : String , shouldExplain : Boolean , cond : Setting [? ] => Boolean )(using settings : ConcreteSettings )(using SettingsState ): String =
92
+ protected def createUsageMsg (label : String , shouldExplain : Boolean , cond : Setting [? ] => Boolean )(using settings : ConcreteSettings )(using SettingsState ): String =
95
93
val prefix = List (
96
94
Some (shortUsage),
97
95
Some (explainAdvanced) filter (_ => shouldExplain),
@@ -100,42 +98,50 @@ trait CliCommand:
100
98
101
99
prefix + " \n " + availableOptionsMsg(cond)
102
100
103
- def isStandard (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean = ! isAdvanced(s) && ! isPrivate(s)
104
- def isAdvanced (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean = s.name.startsWith(" -X" ) && s.name != " -X"
105
- def isPrivate (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean = s.name.startsWith(" -Y" ) && s.name != " -Y"
101
+ protected def isStandard (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean =
102
+ ! isAdvanced(s) && ! isPrivate(s)
103
+ protected def isAdvanced (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean =
104
+ s.name.startsWith(" -X" ) && s.name != " -X"
105
+ protected def isPrivate (s : Setting [? ])(using settings : ConcreteSettings )(using SettingsState ): Boolean =
106
+ s.name.startsWith(" -Y" ) && s.name != " -Y"
106
107
107
108
/** Messages explaining usage and options */
108
- def usageMessage (using settings : ConcreteSettings )(using SettingsState ) = createUsageMsg(" where possible standard" , shouldExplain = false , isStandard)
109
- def xusageMessage (using settings : ConcreteSettings )(using SettingsState ) = createUsageMsg(" Possible advanced" , shouldExplain = true , isAdvanced)
110
- def yusageMessage (using settings : ConcreteSettings )(using SettingsState ) = createUsageMsg(" Possible private" , shouldExplain = true , isPrivate)
111
-
112
- def phasesMessage : String =
109
+ protected def usageMessage (using settings : ConcreteSettings )(using SettingsState ) =
110
+ createUsageMsg(" where possible standard" , shouldExplain = false , isStandard)
111
+ protected def xusageMessage (using settings : ConcreteSettings )(using SettingsState ) =
112
+ createUsageMsg(" Possible advanced" , shouldExplain = true , isAdvanced)
113
+ protected def yusageMessage (using settings : ConcreteSettings )(using SettingsState ) =
114
+ createUsageMsg(" Possible private" , shouldExplain = true , isPrivate)
115
+
116
+ protected def phasesMessage : String =
113
117
(new Compiler ()).phases.map {
114
118
case List (single) => single.phaseName
115
119
case more => more.map(_.phaseName).mkString(" {" , " , " , " }" )
116
120
}.mkString(" \n " )
117
121
118
122
/** Provide usage feedback on argument summary, assuming that all settings
119
123
* are already applied in context.
120
- * @return The list of files passed as arguments.
124
+ * @return Either Some list of files passed as arguments or None if further processing should be interrupted .
121
125
*/
122
- def checkUsage (summary : ArgsSummary , sourcesRequired : Boolean )(using settings : ConcreteSettings )(using SettingsState )( using Context ): List [String ] =
126
+ def checkUsage (summary : ArgsSummary , sourcesRequired : Boolean )(using settings : ConcreteSettings )(using SettingsState , Context ): Option [ List [String ] ] =
123
127
// Print all warnings encountered during arguments parsing
124
128
summary.warnings.foreach(report.warning(_))
125
129
126
130
if summary.errors.nonEmpty then
127
131
summary.errors foreach (report.error(_))
128
132
report.echo(ifErrorsMsg)
129
- Nil
133
+ None
130
134
else if settings.version.value then
131
135
report.echo(versionMsg)
132
- Nil
133
- else if shouldStopWithInfo then
134
- report.echo(infoMessage)
135
- Nil
136
+ None
137
+ else if isHelpFlag then
138
+ report.echo(helpMsg)
139
+ None
140
+ else if (sourcesRequired && summary.arguments.isEmpty)
141
+ report.echo(usageMessage)
142
+ None
136
143
else
137
- if (sourcesRequired && summary.arguments.isEmpty) report.echo(usageMessage)
138
- summary.arguments
144
+ Some (summary.arguments)
139
145
140
146
extension [T ](setting : Setting [T ])
141
147
protected def value (using ss : SettingsState ): T = setting.valueIn(ss)
0 commit comments