Skip to content

Commit

Permalink
Pagination attempts (#5)
Browse files Browse the repository at this point in the history
* Adds pagination example
  • Loading branch information
jlengrand authored Nov 6, 2020
1 parent 0251d28 commit b8000a8
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 9 deletions.
Binary file modified .gradle/6.3/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/6.3/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/6.3/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/6.3/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,37 @@ You can find :
* [SwaCLIBasic](src/main/kotlin/nl/lengrand/swacli/SwaCLIBasic.kt) Not doing anything but showing how picoCLI comes with batteries included
* [SwaCLIOptions](src/main/kotlin/nl/lengrand/swacli/SwaCLIOptions.kt) A small demonstration on how to use (mutually exclusive) options and arguments
* [SwaCLISubCommands](src/main/kotlin/nl/lengrand/swacli/SwaCLISubCommands.kt) A small demonstration on how to use subcommands
* [SwaCLIPaginate](src/main/kotlin/nl/lengrand/swacli/SwaCLIPaginate.kt) A small demonstration on how to use subcommands and paginate the results. Default command in the sw compilable
* [SwaCLIProgrammatic](src/main/kotlin/nl/lengrand/swacli/SwaCLIProgrammatic.kt) An example that uses the programmatic API instead of the annotation system.
* [SwaCLISubCommandsTest](src/test/kotlin/nl/lengrand/swacli/SwaCLISubCommandsTest.kt) An example on how to test commandline configurations


All examples are in Kotlin and provide the same rough capabilities (get some facts about characters or planets in Star Wars)

## Running
## Compiling / Running

```
$ ./gradlew customFatJar
$ java -cp build/libs/all-in-one-jar-1.0-SNAPSHOT.jar nl.lengrand.swacli.SwaCLI
# to keep color and paginate the results
$ java -Dpicocli.ansi=true -cp build/libs/all-in-one-jar-1.0-SNAPSHOT.jar nl.lengrand.swacli.SwaCLIOptions -p | less -R
# to create the native image
$ cd build/libs;native-image --static -jar all-in-one-jar-1.0-SNAPSHOT.jar sw
$ java -cp build/libs/all-in-one-jar-1.0-SNAPSHOT.jar nl.lengrand.swacli.SwaCLIPaginate
```

or if you're on Mac give a shot to the latest [release](https://github.com/jlengrand/swacli/releases).

## Creating the native image

[You'll need to have GraalVM and the native image extension setup on your system :)](https://www.graalvm.org/reference-manual/native-image/)

```
$ cd build/libs;native-image --static -jar all-in-one-jar-1.0-SNAPSHOT.jar sw --enable-http --enable-https
# remove static if you're on Mac :)
$ cd build/libs;native-image -jar all-in-one-jar-1.0-SNAPSHOT.jar sw
```

## Why

This is an example CLI that I created as illustration for my [JFall 2020 talk](https://jfall.nl/sessions/an-introduction-to-creating-cli-applications-using-picocli/)

You can check out the slides [here](https://github.com/jlengrand/picocli-jfall-bitesize-2020).

## Source

* [The repository can be found here](https://github.com/jlengrand/swacli)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ kapt {

task customFatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'nl.lengrand.swacli.SwaCLISubCommands'
attributes 'Main-Class': 'nl.lengrand.swacli.SwaCLIPaginate'
}
baseName = 'all-in-one-jar'
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/nl/lengrand/swacli/PrettyPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ______ __ __ ______ ______ __ __
\/_____/ \/_/ \/_/ \/_/\/_/ \/_____/ \/_____/ \/_/
"""

class PrettyPrinter(val spec : CommandLine.Model.CommandSpec ) {
class PrettyPrinter(val spec : CommandLine.Model.CommandSpec) {
fun <T : Data> print(response: Response<T>){

spec.commandLine().out.println("""
Expand Down
55 changes: 55 additions & 0 deletions src/main/kotlin/nl/lengrand/swacli/SwaCLIPaginate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nl.lengrand.swacli

import picocli.CommandLine
import picocli.CommandLine.*
import picocli.CommandLine.Model.*
import java.io.FileWriter
import java.io.PrintWriter
import java.nio.file.Files
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(
name = "sw",
version = ["0.2"],
mixinStandardHelpOptions = true,
description = [asciiArt, "@|bold,yellow \uD83E\uDE90 A Star Wars CLI built on top of https://swapi.dev/ \uD83E\uDE90 |@"],
subcommands = [PlanetsCommand::class, PeopleCommand::class, HelpCommand::class]
)
class SwaCLIPaginate : Callable<Int> {

@Spec
lateinit var spec: CommandSpec

private fun executionStrategy(parseResult: ParseResult): Int {

if (!parseResult.hasSubcommand())
return RunLast().execute(parseResult)

val file = Files.createTempFile("pico", ".tmp").toFile()
this.spec.commandLine().out = PrintWriter(FileWriter(file), true)

val result = RunLast().execute(parseResult)

val processBuilder = ProcessBuilder("less", file.absolutePath).inheritIO()
val process = processBuilder.start()
process.waitFor()

return result
}

override fun call(): Int {
spec.commandLine().usage(System.out)
return 0
}

companion object{
@JvmStatic
fun main(args: Array<String>){
val app = SwaCLIPaginate()
exitProcess(CommandLine(app)
.setExecutionStrategy(app::executionStrategy)
.execute(*args))
}
}
}

0 comments on commit b8000a8

Please sign in to comment.