-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starting to impl options and flags with new ArgumentConsumer
- Loading branch information
Showing
10 changed files
with
152 additions
and
104 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/ArgumentProcessor.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,43 @@ | ||
package com.mattmx.ktgui.commands.declarative.arg | ||
|
||
import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder | ||
import com.mattmx.ktgui.commands.declarative.arg.impl.* | ||
|
||
class ArgumentProcessor( | ||
val command: DeclarativeCommandBuilder, | ||
val args: List<String> | ||
) { | ||
var pointer = 0 | ||
// Should be added to the command context | ||
val optionsAndFlags = hashMapOf<String, String>() | ||
|
||
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 && command.optionsSyntax.match(it) }) { | ||
val optionOrPointerId = command.optionsSyntax.removePrefixFrom(current()!!) | ||
|
||
if (command.permittedFlags.any { it.chatName() == optionOrPointerId }) { | ||
optionsAndFlags[optionOrPointerId] = true.toString() | ||
pointer++ | ||
} else if (command.permittedOptions.any { it.name() == optionOrPointerId }) { | ||
|
||
// TODO this should read the argument type args (does that make sense?) | ||
// e.g '--test "hello world"' -> hello world | ||
|
||
val value = peek(1) ?: continue | ||
optionsAndFlags[optionOrPointerId] = value | ||
pointer += 2 | ||
} else break | ||
} | ||
|
||
return current() | ||
} | ||
|
||
fun reset() { | ||
this.pointer = 0 | ||
this.optionsAndFlags.clear() | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/argTests.kt
This file was deleted.
Oops, something went wrong.
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
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
10 changes: 6 additions & 4 deletions
10
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
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) { | ||
typeName: String | ||
) : Argument<T>(name, typeName) { | ||
|
||
fun chatName() = name().replace("_", "-") | ||
|
||
|
||
} |
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
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
Oops, something went wrong.