@@ -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,9 @@ 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+ assert(name.startsWith(s " - ${category.prefixLetter}" ), s " Setting $name does not start with category - $category" )
8090 assert(legacyArgs || ! choices.exists(_.contains(" " )), s " Empty string is not supported as a choice for setting $name" )
8191 // Without the following assertion, it would be easy to mistakenly try to pass a file to a setting that ignores invalid args.
8292 // Example: -opt Main.scala would be interpreted as -opt:Main.scala, and the source file would be ignored.
@@ -319,64 +329,58 @@ object Settings:
319329 setting
320330 }
321331
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 =
332+ def prependName (name : String ): String =
329333 assert(! name.startsWith(" -" ), s " Setting $name cannot start with - " )
330- " -" + validateSetting( name)
334+ " -" + name
331335
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) ))
336+ def BooleanSetting (category : SettingCategory , name : String , descr : String , initialValue : Boolean = false , aliases : List [String ] = Nil ): Setting [Boolean ] =
337+ publish(Setting (category, prependName (name), descr, initialValue, aliases = aliases))
334338
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) ))
339+ def StringSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
340+ publish(Setting (category, prependName (name), descr, default, helpArg, aliases = aliases))
337341
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) ))
342+ def ChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
343+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
340344
341345 // 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 ))
346+ def LegacyChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : String , aliases : List [String ] = Nil ): Setting [String ] =
347+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases, legacyArgs = true ))
344348
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) ))
349+ def MultiChoiceSetting (category : SettingCategory , name : String , helpArg : String , descr : String , choices : List [String ], default : List [String ], aliases : List [String ] = Nil ): Setting [List [String ]] =
350+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
347351
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) ))
352+ 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 ]]] =
353+ publish(Setting (category, prependName (name), descr, default, helpArg, Some (choices), aliases = aliases))
350354
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) ))
355+ def IntSetting (category : SettingCategory , name : String , descr : String , default : Int , aliases : List [String ] = Nil ): Setting [Int ] =
356+ publish(Setting (category, prependName (name), descr, default, aliases = aliases))
353357
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)))
358+ def IntChoiceSetting (category : SettingCategory , name : String , descr : String , choices : Seq [Int ], default : Int ): Setting [Int ] =
359+ publish(Setting (category, prependName (name), descr, default, choices = Some (choices)))
356360
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) ))
361+ def MultiStringSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : List [String ] = Nil , aliases : List [String ] = Nil ): Setting [List [String ]] =
362+ publish(Setting (category, prependName (name), descr, default, helpArg, aliases = aliases))
359363
360- def OutputSetting (category : String , name : String , helpArg : String , descr : String , default : AbstractFile ): Setting [AbstractFile ] =
361- publish(Setting (category, validateAndPrependName (name), descr, default, helpArg))
364+ def OutputSetting (category : SettingCategory , name : String , helpArg : String , descr : String , default : AbstractFile ): Setting [AbstractFile ] =
365+ publish(Setting (category, prependName (name), descr, default, helpArg))
362366
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) ))
367+ def PathSetting (category : SettingCategory , name : String , descr : String , default : String , aliases : List [String ] = Nil ): Setting [String ] =
368+ publish(Setting (category, prependName (name), descr, default, aliases = aliases))
365369
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) ))
370+ def PhasesSetting (category : SettingCategory , name : String , descr : String , default : String = " " , aliases : List [String ] = Nil ): Setting [List [String ]] =
371+ publish(Setting (category, prependName (name), descr, if (default.isEmpty) Nil else List (default), aliases = aliases))
368372
369- def PrefixSetting (category : String , name : String , descr : String ): Setting [List [String ]] =
373+ def PrefixSetting (category : SettingCategory , name : String , descr : String ): Setting [List [String ]] =
370374 val prefix = name.takeWhile(_ != '<' )
371- publish(Setting (category, " -" + name, descr, Nil , prefix = Some (validateSetting( prefix) )))
375+ publish(Setting (category, " -" + name, descr, Nil , prefix = Some (prefix)))
372376
373- def VersionSetting (category : String , name : String , descr : String , default : ScalaVersion = NoScalaVersion ): Setting [ScalaVersion ] =
374- publish(Setting (category, validateAndPrependName (name), descr, default))
377+ def VersionSetting (category : SettingCategory , name : String , descr : String , default : ScalaVersion = NoScalaVersion ): Setting [ScalaVersion ] =
378+ publish(Setting (category, prependName (name), descr, default))
375379
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) ))
380+ def OptionSetting [T : ClassTag ](category : SettingCategory , name : String , descr : String , aliases : List [String ] = Nil ): Setting [Option [T ]] =
381+ publish(Setting (category, prependName (name), descr, None , propertyClass = Some (summon[ClassTag [T ]].runtimeClass), aliases = aliases))
378382
379- def DeprecatedSetting (category : String , name : String , descr : String , deprecationMsg : String ): Setting [Boolean ] =
380- publish(Setting (category, validateAndPrependName (name), descr, false , deprecationMsg = Some (deprecationMsg)))
383+ def DeprecatedSetting (category : SettingCategory , name : String , descr : String , deprecationMsg : String ): Setting [Boolean ] =
384+ publish(Setting (category, prependName (name), descr, false , deprecationMsg = Some (deprecationMsg)))
381385 }
382386end Settings
0 commit comments