-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl new ArgumentProcessor to process arguments correctly
- Loading branch information
Showing
5 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.mattmx.ktgui.commands.declarative.arg | ||
|
||
import com.mattmx.ktgui.commands.declarative.arg.impl.* | ||
import kotlin.math.min | ||
|
||
class ArgumentProcessor( | ||
val args: List<String> | ||
) { | ||
var pointer = 0 | ||
val values = hashMapOf<String, String>() | ||
|
||
// Should be in the [DeclarativeCommandBuilder] | ||
val permittedFlags = arrayListOf<FlagArgument>() | ||
val options = OptionSyntax() | ||
|
||
fun peek(i: Int) = args.getOrNull(pointer + i) | ||
fun current() = peek(0) | ||
fun next(): String? { | ||
// Check if it is a flag | ||
pointer++ | ||
while (current().let { it != null && options.match(it) }) { | ||
val optionOrPointerId = options.removePrefixFrom(current()!!) | ||
|
||
// TODO check if expected id is expected and if it is a flag or option | ||
if (permittedFlags.any { it.chatName() == optionOrPointerId }) { | ||
values[optionOrPointerId] = true.toString() | ||
} else { | ||
// TODO this should read the argument type args (does that make sense?) | ||
// e.g '--test "hello world"' -> hello world | ||
val value = peek(1) ?: continue | ||
values[optionOrPointerId] = value | ||
pointer++ | ||
} | ||
|
||
pointer++ | ||
} | ||
|
||
return current() | ||
} | ||
|
||
fun takeOne(argId: String) { | ||
values[argId] = next() ?: return | ||
} | ||
|
||
fun takeUntilNot(argId: String, block: String.() -> Boolean) { | ||
var current = next() | ||
val list = arrayListOf<String>() | ||
|
||
if (current != null) { | ||
list.add(current) | ||
} | ||
|
||
while (current != null && block(current)) { | ||
current = next() | ||
if (current != null) { | ||
list.add(current) | ||
} | ||
} | ||
values[argId] = list.joinToString(" ") | ||
} | ||
|
||
fun takeRemaining(argId: String) { | ||
takeUntilNot(argId) { pointer < args.size } | ||
} | ||
} | ||
|
||
fun main() { | ||
val ping by flag() | ||
val option by optionArgument<String>() | ||
|
||
val args = "msg MattMX foo bar --ping --option 'hello world'".split(" ") | ||
val processor = ArgumentProcessor(args) | ||
|
||
processor.permittedFlags.add(ping) | ||
|
||
processor.takeOne("username") | ||
processor.takeRemaining("msg") | ||
|
||
println(processor.values) | ||
} |
12 changes: 12 additions & 0 deletions
12
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/impl/FlagArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mattmx.ktgui.commands.declarative.arg.impl | ||
|
||
import com.mattmx.ktgui.commands.declarative.arg.Argument | ||
import com.mattmx.ktgui.commands.declarative.arg.consumer.SingleArgumentConsumer | ||
|
||
class FlagArgument( | ||
name: String | ||
) : Argument<String>(name, "boolean", SingleArgumentConsumer()) { | ||
|
||
fun chatName() = name().replace("_", "-") | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/impl/OptionArgument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mattmx.ktgui.commands.declarative.arg.impl | ||
|
||
import com.mattmx.ktgui.commands.declarative.arg.Argument | ||
import com.mattmx.ktgui.commands.declarative.arg.consumer.ArgumentConsumer | ||
|
||
class OptionArgument<T : Any>( | ||
name: String, | ||
typeName: String, | ||
consumer: ArgumentConsumer | ||
) : Argument<T>(name, typeName, consumer) { | ||
} |
11 changes: 11 additions & 0 deletions
11
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/impl/OptionSyntax.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mattmx.ktgui.commands.declarative.arg.impl | ||
|
||
data class OptionSyntax( | ||
val prefix: String = "--" | ||
) { | ||
fun regex() = "$prefix.+".toRegex() | ||
|
||
fun match(str: String) = str.matches(regex()) | ||
|
||
fun removePrefixFrom(str: String) = str.replaceFirst(prefix, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters