@@ -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,9 @@ 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
+ assert(name.startsWith(s " - ${category.prefixLetter}" ), s " Setting $name does not start with category - $category" )
80
90
assert(legacyArgs || ! choices.exists(_.contains(" " )), s " Empty string is not supported as a choice for setting $name" )
81
91
// Without the following assertion, it would be easy to mistakenly try to pass a file to a setting that ignores invalid args.
82
92
// Example: -opt Main.scala would be interpreted as -opt:Main.scala, and the source file would be ignored.
@@ -319,64 +329,58 @@ object Settings:
319
329
setting
320
330
}
321
331
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 =
329
333
assert(! name.startsWith(" -" ), s " Setting $name cannot start with - " )
330
- " -" + validateSetting( name)
334
+ " -" + name
331
335
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))
334
338
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))
337
341
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))
340
344
341
345
// 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 ))
344
348
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))
347
351
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))
350
354
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))
353
357
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)))
356
360
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))
359
363
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))
362
366
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))
365
369
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))
368
372
369
- def PrefixSetting (category : String , name : String , descr : String ): Setting [List [String ]] =
373
+ def PrefixSetting (category : SettingCategory , name : String , descr : String ): Setting [List [String ]] =
370
374
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)))
372
376
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))
375
379
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))
378
382
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)))
381
385
}
382
386
end Settings
0 commit comments