/
SwaCLISubCommands.kt
59 lines (49 loc) · 1.67 KB
/
SwaCLISubCommands.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package nl.lengrand.swacli
import picocli.CommandLine
import picocli.CommandLine.*
import picocli.CommandLine.Model.*
import java.util.concurrent.Callable
import kotlin.system.exitProcess
@Command(
name = "sw",
version = ["0.1"],
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 SwaCLISubCommands : Callable<Int> {
@Spec
lateinit var spec: CommandSpec
override fun call(): Int {
spec.commandLine().usage(System.out)
return 0
}
companion object{
@JvmStatic
fun main(args: Array<String>){
exitProcess(CommandLine(SwaCLISubCommands()).execute(*args))
}
}
}
@Command(name = "planets", description = ["Search for planets"])
class PlanetsCommand : Callable<Int> {
@Spec
lateinit var spec: CommandSpec
@Parameters(index = "0", arity = "0..1", description = ["Search query for the request. (Example : Tatooine)"])
private var searchQuery : String? = null
override fun call(): Int {
PrettyPrinter(spec).print(SwApi.getPlanets(searchQuery))
return 0
}
}
@Command(name = "people", description = ["Search for people"])
class PeopleCommand : Callable<Int> {
@Spec
lateinit var spec: CommandSpec
@Parameters(index = "0", arity = "0..1", description = ["Search query for the request. (Example : Anakin)"])
private var searchQuery : String? = null
override fun call(): Int {
PrettyPrinter(spec).print(SwApi.getPeople(searchQuery))
return 0
}
}