@@ -88,9 +88,7 @@ class ArgParser(
8888 val parser = parsers[arg.type]
8989 ? : throw ParserNotRegistered (" No parsers registered for `${arg.type} `" )
9090 val (argument, original) = getNextArgument(arg.greedy)
91- val result = if (argument.isEmpty()) {
92- Optional .empty()
93- } else {
91+ val result = argument.takeIf { it.isNotEmpty() }?.let {
9492 try {
9593 parser.parse(ctx, argument)
9694 } catch (e: Throwable ) {
@@ -101,9 +99,9 @@ class ArgParser(
10199 val canSubstitute = arg.isTentative || arg.isNullable || (arg.optional && argument.isEmpty())
102100 val (rangeCheck, rangeMessage) = checkRange(arg, result)
103101
104- if (! result.isPresent || ! rangeCheck) {
102+ if (result == null || ! rangeCheck) {
105103 if (! canSubstitute) { // canSubstitute -> Whether we can pass null or the default value.
106- val cause = if (result.isPresent && rangeCheck) null else IllegalArgumentException (rangeMessage )
104+ val cause = rangeMessage?. let (:: IllegalArgumentException )
107105 // This should throw if the result is not present, and one of the following is not true:
108106 // - The arg is marked tentative (isTentative)
109107 // - The arg can use null (isNullable)
@@ -118,12 +116,11 @@ class ArgParser(
118116 }
119117 }
120118
121- return result.takeIf { rangeCheck }?.orElse( null )
119+ return result.takeIf { rangeCheck }
122120 }
123121
124- private fun checkRange (arg : Argument , parsed : Optional < out Any ?> ): Pair <Boolean , String ?> {
122+ private fun < T : Any ?> checkRange (arg : Argument , res : T ): Pair <Boolean , String ?> {
125123 arg.range ? : return true to null
126- val res = parsed.orElse(null )
127124
128125 if (res !is Number && res !is String ) {
129126 return false to null
@@ -138,21 +135,21 @@ class ArgParser(
138135 return when (double.size) {
139136 1 -> (dbl >= double[0 ]) to " `${arg.name} ` must be at least ${double[0 ]} or bigger."
140137 2 -> (dbl >= double[0 ] && dbl <= double[1 ]) to " `${arg.name} ` must be within range ${double.joinToString(" -" )} ."
141- else -> false to " < Invalid range for `${arg.name} :double`> "
138+ else -> false to " Invalid double range for `${arg.name} ` "
142139 }
143140 } else if (long.isNotEmpty() && res is Number ) {
144141 val lng = res.toLong()
145142 return when (long.size) {
146143 1 -> (lng >= long[0 ]) to " `${arg.name} ` must be at least ${long[0 ]} or bigger."
147144 2 -> (lng >= long[0 ] && lng <= long[1 ]) to " `${arg.name} ` must be within range ${long.joinToString(" -" )} ."
148- else -> false to " < Invalid range for `${arg.name} :long`> "
145+ else -> false to " Invalid long range for `${arg.name} ` "
149146 }
150147 } else if (string.isNotEmpty() && res is String ) {
151148 val lth = res.length
152149 return when (string.size) {
153150 1 -> (lth >= string[0 ]) to " `${arg.name} ` must be at least ${string[0 ]} character${TextUtils .plural(string[0 ])} or longer."
154151 2 -> (lth >= string[0 ] && lth <= string[1 ]) to " `${arg.name} ` must be within the range of ${string.joinToString(" -" )} characters."
155- else -> false to " < Invalid range for `${arg.name} :string`> "
152+ else -> false to " Invalid string range for `${arg.name} ` "
156153 }
157154 }
158155
0 commit comments