Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsvedin committed Sep 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents d0a3eae + bc84a59 commit a662327
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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 <String>] [--port <Int>]
migrate --version <Int>
dump --to <File>
deleteItems --ids <Int...>
$ myProgram runServer --help
runServer
--host <String> (optional)
--port <Int> (optional)
$ myProgram runServer
Running the server at 0.0.0.0 on port 8080
16 changes: 12 additions & 4 deletions src/main/kotlin/cli.kt
Original file line number Diff line number Diff line change
@@ -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<Description>()?.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()
}
}
}

1 change: 1 addition & 0 deletions src/test/kotlin/ReadmeTest.kt
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ class ReadmeTest {
@Test
fun documentations() {
emitForReadme("")
emitForReadme("--help")
emitForReadme("runServer --help")
emitForReadme("runServer")
emitForReadme("runServer 127.0.0.0 8080")

0 comments on commit a662327

Please sign in to comment.