@@ -59,7 +59,7 @@ object Settings {
59
59
description : String ,
60
60
default : T ,
61
61
helpArg : String = " " ,
62
- choices : Seq [T ] = Nil ,
62
+ choices : Option [ Seq [T ]] = None ,
63
63
prefix : String = " " ,
64
64
aliases : List [String ] = Nil ,
65
65
depends : List [(Setting [? ], Any )] = Nil ,
@@ -92,25 +92,10 @@ object Settings {
92
92
93
93
def legalChoices : String =
94
94
choices match {
95
- case xs if xs.isEmpty => " "
96
- case r : Range => s " ${r.head}.. ${r.last}"
97
- case xs : List [? ] => xs.toString
98
- }
99
-
100
- def isLegal (arg : Any ): Boolean =
101
- choices match {
102
- case xs if xs.isEmpty =>
103
- arg match {
104
- case _ : T => true
105
- case _ => false
106
- }
107
- case r : Range =>
108
- arg match {
109
- case x : Int => r.head <= x && x <= r.last
110
- case _ => false
111
- }
112
- case xs : List [? ] =>
113
- xs.contains(arg)
95
+ case Some (xs) if xs.isEmpty => " "
96
+ case Some (r : Range ) => s " ${r.head}.. ${r.last}"
97
+ case Some (xs) => xs.mkString(" , " )
98
+ case None => " "
114
99
}
115
100
116
101
def tryToSet (state : ArgsSummary ): ArgsSummary = {
@@ -132,6 +117,12 @@ object Settings {
132
117
ArgsSummary (sstate, args, errors :+ msg, warnings)
133
118
def missingArg =
134
119
fail(s " missing argument for option $name" , args)
120
+ def setString (argValue : String , args : List [String ]) =
121
+ choices match
122
+ case Some (xs) if ! xs.contains(argValue) =>
123
+ fail(s " $argValue is not a valid choice for $name" , args)
124
+ case _ =>
125
+ update(argValue, args)
135
126
def doSet (argRest : String ) = ((implicitly[ClassTag [T ]], args): @ unchecked) match {
136
127
case (BooleanTag , _) =>
137
128
update(true , args)
@@ -140,13 +131,11 @@ object Settings {
140
131
case (ListTag , _) =>
141
132
if (argRest.isEmpty) missingArg
142
133
else update((argRest split " ," ).toList, args)
143
- case (StringTag , _) if choices.nonEmpty && argRest.nonEmpty =>
144
- if (! choices.contains(argRest))
145
- fail(s " $arg is not a valid choice for $name" , args)
146
- else update(argRest, args)
134
+ case (StringTag , _) if argRest.nonEmpty =>
135
+ setString(argRest, args)
147
136
case (StringTag , arg2 :: args2) =>
148
137
if (arg2 startsWith " -" ) missingArg
149
- else update (arg2, args2)
138
+ else setString (arg2, args2)
150
139
case (OutputTag , arg :: args) =>
151
140
val path = Directory (arg)
152
141
val isJar = path.extension == " jar"
@@ -160,8 +149,10 @@ object Settings {
160
149
try {
161
150
val x = arg2.toInt
162
151
choices match {
163
- case r : Range if x < r.head || r.last < x =>
164
- fail(s " $arg2 is out of legal range $legalChoices for $name" , args2)
152
+ case Some (r : Range ) if x < r.head || r.last < x =>
153
+ fail(s " $arg2 is out of legal range ${r.head}.. ${r.last} for $name" , args2)
154
+ case Some (xs) if ! xs.contains(x) =>
155
+ fail(s " $arg2 is not a valid choice for $name" , args)
165
156
case _ =>
166
157
update(x, args2)
167
158
}
@@ -273,10 +264,13 @@ object Settings {
273
264
publish(Setting (name, descr, default, helpArg, aliases = aliases))
274
265
275
266
def ChoiceSetting (name : String , helpArg : String , descr : String , choices : List [String ], default : String ): Setting [String ] =
276
- publish(Setting (name, descr, default, helpArg, choices))
267
+ publish(Setting (name, descr, default, helpArg, Some (choices)))
268
+
269
+ def IntSetting (name : String , descr : String , default : Int ): Setting [Int ] =
270
+ publish(Setting (name, descr, default))
277
271
278
- def IntSetting (name : String , descr : String , default : Int , range : Seq [Int ] = Nil ): Setting [Int ] =
279
- publish(Setting (name, descr, default, choices = range ))
272
+ def IntChoiceSetting (name : String , descr : String , choices : Seq [Int ], default : Int ): Setting [Int ] =
273
+ publish(Setting (name, descr, default, choices = Some (choices) ))
280
274
281
275
def MultiStringSetting (name : String , helpArg : String , descr : String ): Setting [List [String ]] =
282
276
publish(Setting (name, descr, Nil , helpArg))
0 commit comments