@@ -25,6 +25,9 @@ object Settings:
2525 val OptionTag : ClassTag [Option [? ]] = ClassTag (classOf [Option [? ]])
2626 val OutputTag : ClassTag [AbstractFile ] = ClassTag (classOf [AbstractFile ])
2727
28+ trait SettingCategory :
29+ def prefixLetter : String
30+
2831 class SettingsState (initialValues : Seq [Any ], initialChanged : Set [Int ] = Set .empty):
2932 private val values = ArrayBuffer (initialValues* )
3033 private val changed : mutable.Set [Int ] = initialChanged.to(mutable.Set )
@@ -59,8 +62,14 @@ object Settings:
5962 ArgsSummary (sstate, arguments.tail, errors, warnings :+ msg)
6063 }
6164
65+ @ unshared
66+ val settingCharacters = " [a-zA-Z0-9_\\ -]*" .r
67+ def validateSettingString (name : String ): Unit =
68+ assert(settingCharacters.matches(name), s " Setting string $name contains invalid characters " )
69+
70+
6271 case class Setting [T : ClassTag ] private [Settings ] (
63- category : String ,
72+ category : SettingCategory ,
6473 name : String ,
6574 description : String ,
6675 default : T ,
@@ -75,8 +84,10 @@ object Settings:
7584 // kept only for -Ykind-projector option compatibility
7685 legacyArgs : Boolean = false )(private [Settings ] val idx : Int ) {
7786
78-
79- assert(name.startsWith(s " - $category" ), s " Setting $name does not start with category - $category" )
87+ validateSettingString(prefix.getOrElse(name))
88+ aliases.foreach(validateSettingString)
89+ prefix.foreach(validateSettingString)
90+ assert(name.startsWith(s " - ${category.prefixLetter}" ), s " Setting $name does not start with category - $category" )
8091 assert(legacyArgs || ! choices.exists(_.contains(" " )), s " Empty string is not supported as a choice for setting $name" )
8192 // Without the following assertion, it would be easy to mistakenly try to pass a file to a setting that ignores invalid args.
8293 // Example: -opt Main.scala would be interpreted as -opt:Main.scala, and the source file would be ignored.
@@ -319,64 +330,58 @@ object Settings:
319330 setting
320331 }
321332
322- @ unshared
323- val settingCharacters = " [a-zA-Z0-9_\\ -]*" .r
324- def validateSetting (setting : String ): String =
325- assert(settingCharacters.matches(setting), s " Setting $setting contains invalid characters " )
326- setting
327-
328- def validateAndPrependName (name : String ): String =
333+ def prependName (name : String ): String =
329334 assert(! name.startsWith(" -" ), s " Setting $name cannot start with - " )
330- " -" + validateSetting( name)
335+ " -" + name
331336
332- def BooleanSetting (category : String , name : String , descr : String , initialValue : Boolean = false , aliases : List [String ] = Nil ): Setting [Boolean ] =
333- publish(Setting (category, validateAndPrependName (name), descr, initialValue, aliases = aliases.map(validateSetting) ))
337+ def BooleanSetting (category : SettingCategory , name : String , descr : String , initialValue : Boolean = false , aliases : List [String ] = Nil ): Setting [Boolean ] =
338+ publish(Setting (category, prependName (name), descr, initialValue, aliases = aliases))
334339
335- def StringSetting (category : String , name : String , helpArg : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
336- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, aliases = aliases.map(validateSetting) ))
340+ def StringSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
341+ publish(Setting (category, prependName (name), descr, default, helpArg, aliases = aliases))
337342
338- def ChoiceSetting (category : String , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
339- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, Some (choices), aliases = aliases.map(validateSetting) ))
343+ def ChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
344+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
340345
341346 // Allows only args after :, but supports empty string as a choice. Used for -Ykind-projector
342- def LegacyChoiceSetting (category : String , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
343- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, Some (choices), aliases = aliases.map(validateSetting) , legacyArgs = true ))
347+ def LegacyChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
348+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases, legacyArgs = true ))
344349
345- def MultiChoiceSetting (category : String , name : String , helpArg : String , descr : String , choices : List [String ], default : List [String ], aliases : List [String ] = Nil ): Setting [List [String ]] =
346- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, Some (choices), aliases = aliases.map(validateSetting) ))
350+ def MultiChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : List [String ], aliases : List [String ] = Nil ): Setting [List [String ]] =
351+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
347352
348- def MultiChoiceHelpSetting (category : String , name : String , helpArg : String , descr : String , choices : List [ChoiceWithHelp [String ]], default : List [ChoiceWithHelp [String ]], aliases : List [String ] = Nil ): Setting [List [ChoiceWithHelp [String ]]] =
349- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, Some (choices), aliases = aliases.map(validateSetting) ))
353+ def MultiChoiceHelpSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [ChoiceWithHelp [String ]], default : List [ChoiceWithHelp [String ]], aliases : List [String ] = Nil ): Setting [List [ChoiceWithHelp [String ]]] =
354+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
350355
351- def IntSetting (category : String , name : String , descr : String , default : Int , aliases : List [String ] = Nil ): Setting [Int ] =
352- publish(Setting (category, validateAndPrependName (name), descr, default, aliases = aliases.map(validateSetting) ))
356+ def IntSetting (category : SettingCategory , name : String , descr : String , default : Int , aliases : List [String ] = Nil ): Setting [Int ] =
357+ publish(Setting (category, prependName (name), descr, default, aliases = aliases))
353358
354- def IntChoiceSetting (category : String , name : String , descr : String , choices : Seq [Int ], default : Int ): Setting [Int ] =
355- publish(Setting (category, validateAndPrependName (name), descr, default, choices = Some (choices)))
359+ def IntChoiceSetting (category : SettingCategory , name : String , descr : String , choices : Seq [Int ], default : Int ): Setting [Int ] =
360+ publish(Setting (category, prependName (name), descr, default, choices = Some (choices)))
356361
357- def MultiStringSetting (category : String , name : String , helpArg : String , descr : String , default : List [String ] = Nil , aliases : List [String ] = Nil ): Setting [List [String ]] =
358- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg, aliases = aliases.map(validateSetting) ))
362+ def MultiStringSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : List [String ] = Nil , aliases : List [String ] = Nil ): Setting [List [String ]] =
363+ publish(Setting (category, prependName (name), descr, default, helpArg, aliases = aliases))
359364
360- def OutputSetting (category : String , name : String , helpArg : String , descr : String , default : AbstractFile ): Setting [AbstractFile ] =
361- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg))
365+ def OutputSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : AbstractFile ): Setting [AbstractFile ] =
366+ publish(Setting (category, prependName (name), descr, default, helpArg))
362367
363- def PathSetting (category : String , name : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
364- publish(Setting (category, validateAndPrependName (name), descr, default, aliases = aliases.map(validateSetting) ))
368+ def PathSetting (category : SettingCategory , name : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
369+ publish(Setting (category, prependName (name), descr, default, aliases = aliases))
365370
366- def PhasesSetting (category : String , name : String , descr : String , default : String = " " , aliases : List [String ] = Nil ): Setting [List [String ]] =
367- publish(Setting (category, validateAndPrependName (name), descr, if (default.isEmpty) Nil else List (default), aliases = aliases.map(validateSetting) ))
371+ def PhasesSetting (category : SettingCategory , name : String , descr : String , default : String = " " , aliases : List [String ] = Nil ): Setting [List [String ]] =
372+ publish(Setting (category, prependName (name), descr, if (default.isEmpty) Nil else List (default), aliases = aliases))
368373
369- def PrefixSetting (category : String , name : String , descr : String ): Setting [List [String ]] =
374+ def PrefixSetting (category : SettingCategory , name : String , descr : String ): Setting [List [String ]] =
370375 val prefix = name.takeWhile(_ != '<' )
371- publish(Setting (category, " -" + name, descr, Nil , prefix = Some (validateSetting( prefix) )))
376+ publish(Setting (category, " -" + name, descr, Nil , prefix = Some (prefix)))
372377
373- def VersionSetting (category : String , name : String , descr : String , default : ScalaVersion = NoScalaVersion ): Setting [ScalaVersion ] =
374- publish(Setting (category, validateAndPrependName (name), descr, default))
378+ def VersionSetting (category : SettingCategory , name : String , descr : String , default : ScalaVersion = NoScalaVersion ): Setting [ScalaVersion ] =
379+ publish(Setting (category, prependName (name), descr, default))
375380
376- def OptionSetting [T : ClassTag ](category : String , name : String , descr : String , aliases : List [String ] = Nil ): Setting [Option [T ]] =
377- publish(Setting (category, validateAndPrependName (name), descr, None , propertyClass = Some (summon[ClassTag [T ]].runtimeClass), aliases = aliases.map(validateSetting) ))
381+ def OptionSetting [T : ClassTag ](category : SettingCategory , name : String , descr : String , aliases : List [String ] = Nil ): Setting [Option [T ]] =
382+ publish(Setting (category, prependName (name), descr, None , propertyClass = Some (summon[ClassTag [T ]].runtimeClass), aliases = aliases))
378383
379- def DeprecatedSetting (category : String , name : String , descr : String , deprecationMsg : String ): Setting [Boolean ] =
380- publish(Setting (category, validateAndPrependName (name), descr, false , deprecationMsg = Some (deprecationMsg)))
384+ def DeprecatedSetting (category : SettingCategory , name : String , descr : String , deprecationMsg : String ): Setting [Boolean ] =
385+ publish(Setting (category, prependName (name), descr, false , deprecationMsg = Some (deprecationMsg)))
381386 }
382387end Settings
0 commit comments