@@ -25,6 +25,9 @@ object Settings:
25
25
val OptionTag : ClassTag [Option [? ]] = ClassTag (classOf [Option [? ]])
26
26
val OutputTag : ClassTag [AbstractFile ] = ClassTag (classOf [AbstractFile ])
27
27
28
+ trait SettingCategory :
29
+ def prefixLetter : String
30
+
28
31
class SettingsState (initialValues : Seq [Any ], initialChanged : Set [Int ] = Set .empty):
29
32
private val values = ArrayBuffer (initialValues* )
30
33
private val changed : mutable.Set [Int ] = initialChanged.to(mutable.Set )
@@ -59,8 +62,14 @@ object Settings:
59
62
ArgsSummary (sstate, arguments.tail, errors, warnings :+ msg)
60
63
}
61
64
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
+
62
71
case class Setting [T : ClassTag ] private [Settings ] (
63
- category : String ,
72
+ category : SettingCategory ,
64
73
name : String ,
65
74
description : String ,
66
75
default : T ,
@@ -75,8 +84,10 @@ object Settings:
75
84
// kept only for -Ykind-projector option compatibility
76
85
legacyArgs : Boolean = false )(private [Settings ] val idx : Int ) {
77
86
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" )
80
91
assert(legacyArgs || ! choices.exists(_.contains(" " )), s " Empty string is not supported as a choice for setting $name" )
81
92
// Without the following assertion, it would be easy to mistakenly try to pass a file to a setting that ignores invalid args.
82
93
// Example: -opt Main.scala would be interpreted as -opt:Main.scala, and the source file would be ignored.
@@ -319,64 +330,58 @@ object Settings:
319
330
setting
320
331
}
321
332
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 =
329
334
assert(! name.startsWith(" -" ), s " Setting $name cannot start with - " )
330
- " -" + validateSetting( name)
335
+ " -" + name
331
336
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))
334
339
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))
337
342
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))
340
345
341
346
// 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 ))
344
349
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))
347
352
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))
350
355
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))
353
358
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)))
356
361
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))
359
364
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))
362
367
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))
365
370
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))
368
373
369
- def PrefixSetting (category : String , name : String , descr : String ): Setting [List [String ]] =
374
+ def PrefixSetting (category : SettingCategory , name : String , descr : String ): Setting [List [String ]] =
370
375
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)))
372
377
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))
375
380
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))
378
383
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)))
381
386
}
382
387
end Settings
0 commit comments