Skip to content

Commit

Permalink
Reformat according to offical code style
Browse files Browse the repository at this point in the history
  • Loading branch information
orangy committed Feb 26, 2019
1 parent 88deed2 commit fd284ee
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 191 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gradle/
build/
.idea
*.iml

1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.code.style=official
26 changes: 13 additions & 13 deletions src/main/kotlin/kotlinx/cli/CommandLineInterface.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package kotlinx.cli

open class CommandLineInterface(
val commandName: String,
private val usage: String? = null,
private val description: String? = null,
private val epilogue: String? = null,
addHelp: Boolean = true,
val defaultHelpPrinter: HelpPrinter? = SimpleHelpPrinter(24),
val printHelpByDefault: Boolean = true,
val argumentsAfterDoubleDashArePositional: Boolean = true,
val shortTagPrefix: String? = null,
val longTagPrefixes: List<String> = emptyList(),
val longTagValueDelimiter: String? = null
val commandName: String,
private val usage: String? = null,
private val description: String? = null,
private val epilogue: String? = null,
addHelp: Boolean = true,
val defaultHelpPrinter: HelpPrinter? = SimpleHelpPrinter(24),
val printHelpByDefault: Boolean = true,
val argumentsAfterDoubleDashArePositional: Boolean = true,
val shortTagPrefix: String? = null,
val longTagPrefixes: List<String> = emptyList(),
val longTagValueDelimiter: String? = null
) : CommandLineBuilder {
private val usageBuilder: StringBuilder? =
if (usage == null) StringBuilder("Usage: $commandName ") else null
if (usage == null) StringBuilder("Usage: $commandName ") else null
private val actualUsage get() = usageBuilder?.toString() ?: usage!!

private val helpEntries = ArrayList<HelpEntry>()
Expand Down Expand Up @@ -83,5 +83,5 @@ open class CommandLineInterface(
}

fun getFlagAction(flag: String): Action? =
flagActions[flag]
flagActions[flag]
}
12 changes: 6 additions & 6 deletions src/main/kotlin/kotlinx/cli/CommandLineParser.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kotlinx.cli

class CommandLineParser internal constructor(
private val cli: CommandLineInterface
private val cli: CommandLineInterface
) {
fun parse(args: Array<out String>) {
parse(args.asList())
Expand Down Expand Up @@ -86,14 +86,14 @@ class CommandLineParser internal constructor(
private fun checkEnoughPositionals(positionalArgument: PositionalArgument, actualCount: Int) {
if (actualCount < positionalArgument.minArgs) {
throw CommandLineException(
"Not enough positional arguments for ${positionalArgument.name}: " +
"$actualCount, expected at least ${positionalArgument.minArgs}"
"Not enough positional arguments for ${positionalArgument.name}: " +
"$actualCount, expected at least ${positionalArgument.minArgs}"
)
}
}

private fun tokenizeArgs(args: List<String>): List<String> =
args.flatMap { tokenizeArg(it) }
args.flatMap { tokenizeArg(it) }

private fun tokenizeArg(arg: String): List<String> {
if (cli.getFlagAction(arg) != null) return listOf(arg)
Expand Down Expand Up @@ -137,6 +137,6 @@ class CommandLineParser internal constructor(
}

private fun isShortTagPrefixed(arg: String) =
cli.shortTagPrefix != null && arg.startsWith(cli.shortTagPrefix) &&
cli.longTagPrefixes.none { arg.startsWith(it) }
cli.shortTagPrefix != null && arg.startsWith(cli.shortTagPrefix) &&
cli.longTagPrefixes.none { arg.startsWith(it) }
}
79 changes: 39 additions & 40 deletions src/main/kotlin/kotlinx/cli/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ open class SimpleEvent<T> : Event<T>, EventTrigger<T> {
override fun addListener(listener: Listener<T>) {
if (this.listener == null) {
this.listener = listener
}
else {
} else {
throw IllegalStateException("SimpleEvent supports single listener only")
}
}
Expand All @@ -39,84 +38,84 @@ open class SimpleEvent<T> : Event<T>, EventTrigger<T> {


fun CommandLineInterface.onArgument(name: String, help: String, minArgs: Int = 0, maxArgs: Int = 1): Event<String> =
SimpleEvent<String>().apply {
positionalAction(name, help, minArgs, maxArgs) { trigger(it) }
}
SimpleEvent<String>().apply {
positionalAction(name, help, minArgs, maxArgs) { trigger(it) }
}

fun CommandLineInterface.onRemainingArguments(name: String, help: String): Event<String> =
onArgument(name, help, minArgs = 0, maxArgs = Int.MAX_VALUE)
onArgument(name, help, minArgs = 0, maxArgs = Int.MAX_VALUE)

fun CommandLineInterface.onFlag(flag: String, help: String): Event<Nothing?> =
onFlag(listOf(flag), help)
onFlag(listOf(flag), help)

fun CommandLineInterface.onFlag(flags: List<String>, help: String): Event<Nothing?> =
SimpleEvent<Nothing?>().apply {
flagAction(flags, help) {
trigger(null)
}
SimpleEvent<Nothing?>().apply {
flagAction(flags, help) {
trigger(null)
}
}

fun CommandLineInterface.onFlagValue(flag: String, valueSyntax: String, help: String): Event<String> =
onFlagValue(listOf(flag), valueSyntax, help)
onFlagValue(listOf(flag), valueSyntax, help)

fun CommandLineInterface.onFlagValue(flags: List<String>, valueSyntax: String, help: String): Event<String> =
SimpleEvent<String>().apply {
flagValueAction(flags, valueSyntax, help) {
trigger(it)
}
SimpleEvent<String>().apply {
flagValueAction(flags, valueSyntax, help) {
trigger(it)
}
}

class MappedEvent<in T, R>(private val transformation: (T) -> R): SimpleEvent<R>(), Listener<T> {
class MappedEvent<in T, R>(private val transformation: (T) -> R) : SimpleEvent<R>(), Listener<T> {
override fun onEvent(value: T) {
trigger(transformation(value))
}
}

fun <T, R> Event<T>.map(transformation: (T) -> R): Event<R> =
MappedEvent(transformation).also {
this@map.addListener(it)
}
MappedEvent(transformation).also {
this@map.addListener(it)
}

fun <T> Event<T>.onEach(action: (T) -> Unit) =
apply { add(action) }
apply { add(action) }

fun <T> Event<T>.once(action: (T) -> Unit) =
apply {
add {
action(it)
stopParsing()
}
apply {
add {
action(it)
stopParsing()
}
}

class ArgumentStorage<T>(private var value: T): ArgumentValue<T> {
class ArgumentStorage<T>(private var value: T) : ArgumentValue<T> {
override fun getValue(thisRef: Any?, prop: Any?): T =
value
value

fun setValue(newValue: T) {
value = newValue
}
}

fun <T> Event<T>.store(initialValue: T): ArgumentValue<T> =
ArgumentStorage(initialValue).apply {
add { setValue(it) }
}
ArgumentStorage(initialValue).apply {
add { setValue(it) }
}

fun <T : Any> Event<T>.store(): ArgumentValue<T?> =
store(null)
store(null)

fun <T> Event<*>.storeConst(initialValue: T, storeValue: T): ArgumentValue<T> =
ArgumentStorage(initialValue).apply {
add { setValue(storeValue) }
}
ArgumentStorage(initialValue).apply {
add { setValue(storeValue) }
}

fun Event<*>.storeTrue() =
storeConst(false, true)
storeConst(false, true)

fun <T> Event<T>.addTo(list: MutableList<T>): ArgumentValue<List<T>> =
ArgumentStorage(list).apply {
add { list.add(it) }
}
ArgumentStorage(list).apply {
add { list.add(it) }
}

fun <T> Event<T>.addToList() =
addTo(ArrayList())
addTo(ArrayList())
48 changes: 26 additions & 22 deletions src/main/kotlin/kotlinx/cli/FlagArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package kotlinx.cli


abstract class FlagActionBase(
val flags: List<String>,
private val help: String
val flags: List<String>,
private val help: String
) : FlagAction, HelpEntry {

init {
Expand All @@ -19,15 +19,15 @@ abstract class FlagActionBase(


abstract class FlagArgumentBase<T>(
flags: List<String>,
help: String,
initialValue: T
flags: List<String>,
help: String,
initialValue: T
) : FlagActionBase(flags, help), ArgumentValue<T> {

protected var value = initialValue

override fun getValue(thisRef: Any?, prop: Any?): T =
value
value
}


Expand All @@ -42,7 +42,7 @@ fun <T : FlagActionBase> CommandLineBuilder.registerAction(action: T): T {


fun <T> CommandLineBuilder.registerArgument(argument: FlagArgumentBase<T>): ArgumentValue<T> =
registerAction(argument)
registerAction(argument)


fun CommandLineBuilder.flagAction(flags: List<String>, help: String, action: () -> Unit) {
Expand All @@ -60,35 +60,39 @@ fun CommandLineBuilder.flagAction(flag: String, help: String, action: () -> Unit


fun CommandLineBuilder.flagArgument(flag: String, help: String) =
flagArgument(flag, help, false, true)
flagArgument(flag, help, false, true)


fun <T> CommandLineBuilder.flagArgument(flags: List<String>, help: String, initialValue: T, flagValue: T) =
registerArgument(object : FlagArgumentBase<T>(flags, help, initialValue) {
override fun invoke() {
value = flagValue
}
})

registerArgument(object : FlagArgumentBase<T>(flags, help, initialValue) {
override fun invoke() {
value = flagValue
}
})


fun <T> CommandLineBuilder.flagArgument(flag: String, help: String, initialValue: T, flagValue: T) =
flagArgument(listOf(flag), help, initialValue, flagValue)
flagArgument(listOf(flag), help, initialValue, flagValue)


fun <T> CommandLineBuilder.foldFlagArguments(flags: List<String>, help: String, initialValue: T, fn: (T) -> T) =
registerArgument(object : FlagArgumentBase<T>(flags, help, initialValue) {
override fun invoke() {
value = fn(value)
}
})
registerArgument(object : FlagArgumentBase<T>(flags, help, initialValue) {
override fun invoke() {
value = fn(value)
}
})


fun <T> CommandLineBuilder.foldFlagArguments(flag: String, help: String, initialValue: T, fn: (T) -> T) =
foldFlagArguments(listOf(flag), help, initialValue, fn)
foldFlagArguments(listOf(flag), help, initialValue, fn)


fun CommandLineInterface.help(flags: List<String>, help: String, helpPrinter: HelpPrinter, exitAfterHelp: Boolean = true) {
fun CommandLineInterface.help(
flags: List<String>,
help: String,
helpPrinter: HelpPrinter,
exitAfterHelp: Boolean = true
) {
registerAction(object : FlagActionBase(flags, help) {
override fun invoke() {
this@help.printHelp(helpPrinter)
Expand Down
Loading

0 comments on commit fd284ee

Please sign in to comment.