diff --git a/readme.md b/readme.md index 5d7bd63..269c8b3 100644 --- a/readme.md +++ b/readme.md @@ -49,17 +49,18 @@ fun deleteItems(vararg ids: Int) = println("Deleting ${ids.joinToString()}") ``` ``` -$ myProgram +$ myProgram --help Available commands: -runServer(host: String = ..., port: Int = ...): Unit -migrate(version: Int): Unit -dump(to: File): Unit -deleteItems(ids: Int...): Unit +runServer [--host ] [--port ] +migrate --version +dump --to +deleteItems --ids $ myProgram runServer --help runServer --host (optional) --port (optional) + $ myProgram runServer Running the server at 0.0.0.0 on port 8080 diff --git a/src/main/kotlin/cli.kt b/src/main/kotlin/cli.kt index 2c32b1a..d9d2cd1 100644 --- a/src/main/kotlin/cli.kt +++ b/src/main/kotlin/cli.kt @@ -179,12 +179,16 @@ public annotation class Description(val description: String) public annotation class Documentation(val documentation: String) private fun KParameter.toHumanString(): String { - return if(this.isVararg) this.name + ": " + this.varargType().toHumanString() + "..." - else this.name + ": " + this.type.toHumanString() + if(isOptional) " = ..." else "" +// return if(this.isVararg) this.name + ": " + this.varargType().toHumanString() + "..." +// else this.name + ": " + this.type.toHumanString() + if(isOptional) " = ..." else "" + return (if(this.isVararg) "--" + this.name + " <" + this.varargType().toHumanString() + "...>" + else "--" + this.name + " <" + this.type.toHumanString() + ">").let { + if(isOptional) "[$it]" else it + } } private fun KType.toHumanString(): String = this.jvmErasure.simpleName + if(isMarkedNullable) "?" else "" private fun KFunction<*>.toHumanString(): String { - val prefix = "${name}(${valueParameters.joinToString { it.toHumanString() }}): ${returnType.toHumanString()}" + val prefix = "${name} ${valueParameters.joinToString(" ") { it.toHumanString() }}" return findAnnotation()?.let { "$prefix - ${it.description}" } ?: prefix } @@ -329,7 +333,11 @@ private fun interactiveMode( } if(input == "exit" || input == "quit") return val parts = input.cliSplit().toTypedArray() - cli(parts, available = available, useInteractive = false) + try { + cli(parts, available = available, useInteractive = false) + } catch(e: Exception) { + e.printStackTrace() + } } } diff --git a/src/test/kotlin/ReadmeTest.kt b/src/test/kotlin/ReadmeTest.kt index 7d3952e..3aa263a 100644 --- a/src/test/kotlin/ReadmeTest.kt +++ b/src/test/kotlin/ReadmeTest.kt @@ -8,6 +8,7 @@ class ReadmeTest { @Test fun documentations() { emitForReadme("") + emitForReadme("--help") emitForReadme("runServer --help") emitForReadme("runServer") emitForReadme("runServer 127.0.0.0 8080")