@@ -688,6 +688,12 @@ class Range(NumericOption):
688688 range_start = 0
689689 range_end = 1
690690
691+ _RANDOM_OPTS = [
692+ "random" , "random-low" , "random-middle" , "random-high" ,
693+ "random-range-low-<min>-<max>" , "random-range-middle-<min>-<max>" ,
694+ "random-range-high-<min>-<max>" , "random-range-<min>-<max>" ,
695+ ]
696+
691697 def __init__ (self , value : int ):
692698 if value < self .range_start :
693699 raise Exception (f"{ value } is lower than minimum { self .range_start } for option { self .__class__ .__name__ } " )
@@ -713,9 +719,26 @@ def from_text(cls, text: str) -> Range:
713719 # these are the conditions where "true" and "false" make sense
714720 if text == "true" :
715721 return cls .from_any (cls .default )
716- else : # "false"
717- return cls (0 )
718- return cls (int (text ))
722+ # "false"
723+ return cls (0 )
724+
725+ try :
726+ num = int (text )
727+ except ValueError :
728+ # text is not a number
729+ # Handle conditionally acceptable values here rather than in the f-string
730+ default = ""
731+ truefalse = ""
732+ if hasattr (cls , "default" ):
733+ default = ", default"
734+ if cls .range_start == 0 and cls .default != 0 :
735+ truefalse = ", \" true\" , \" false\" "
736+ raise Exception (f"Invalid range value { text !r} . Acceptable values are: "
737+ f"<int>{ default } , high, low{ truefalse } , "
738+ f"{ ', ' .join (cls ._RANDOM_OPTS )} ." )
739+
740+ return cls (num )
741+
719742
720743 @classmethod
721744 def weighted_range (cls , text ) -> Range :
@@ -731,9 +754,7 @@ def weighted_range(cls, text) -> Range:
731754 return cls (random .randint (cls .range_start , cls .range_end ))
732755 else :
733756 raise Exception (f"random text \" { text } \" did not resolve to a recognized pattern. "
734- f"Acceptable values are: random, random-high, random-middle, random-low, "
735- f"random-range-low-<min>-<max>, random-range-middle-<min>-<max>, "
736- f"random-range-high-<min>-<max>, or random-range-<min>-<max>." )
757+ f"Acceptable values are: { ', ' .join (cls ._RANDOM_OPTS )} ." )
737758
738759 @classmethod
739760 def custom_range (cls , text ) -> Range :
0 commit comments