Compare commits

..

4 Commits

Author SHA1 Message Date
6be1957d82 update README 2025-12-13 20:24:01 +01:00
757011af85 small refactor 2025-12-13 20:23:34 +01:00
ebd698e611 fix workflow 2025-12-13 01:53:00 +01:00
810c2d4263 use common plugin 2025-12-13 01:47:38 +01:00
8 changed files with 43 additions and 77 deletions

View File

@@ -4,7 +4,7 @@ on:
- main
env:
GIT__TOKEN: ${{ secrets.TOKEN }}
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
jobs:
publish:

View File

@@ -1,16 +1,15 @@
# Kotlin CLI
# cli-kt
This library implements a lightweight framework for implementing terminal applications.
`cli-kt` is a Kotlin Multiplatform framework for implementing terminal applications.
## Features
## Targets
* Declare commands.
* Supports both named and wildcard commands.
* Declare and parse type-safe arguments.
* Multiplatform support
* JVM
* JavaScript
* Native (Linux)
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation

View File

@@ -1,11 +1,11 @@
import com.infendro.plugin.infendro
import com.infendro.plugin.token
group = "com.infendro"
version = "1.3.0"
repositories {
mavenCentral()
}
plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.multiplatform)
id("maven-publish")
}
@@ -16,33 +16,12 @@ kotlin {
nodejs()
}
linuxX64()
}
publishing {
repositories {
maven {
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
authentication.create("header", HttpHeaderAuthentication::class)
credentials(HttpHeaderCredentials::class) {
val token = secret("git.token") ?: throw GradleException()
name = "Authorization"
value = "token $token"
}
}
mavenCentral()
}
}
fun secret(
key: String,
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
publishing.repositories {
infendro(token)
}

View File

@@ -1,5 +1,7 @@
[versions]
kotlin = "2.2.10"
infendro = "1.0.0"
kotlin = "2.2.21"
[plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

View File

@@ -1 +1,6 @@
rootProject.name = "cli"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -1,6 +1,5 @@
package com.infendro.cli.command
import com.infendro.cli.command.Execution
import com.infendro.cli.argument.Argument
import com.infendro.cli.exception.build.*
import com.infendro.cli.exception.run.ArgumentCountException
@@ -24,8 +23,7 @@ class Command private constructor(
fun run(
args: Array<String>,
) {
val index = args
.withIndex()
val index = args.withIndex()
.find { (_, value) -> value.startsWith("--") }
?.index ?: args.size
@@ -89,8 +87,7 @@ class Command private constructor(
result += Execution.Arg(name, value)
}
arguments
.forEach { argument ->
arguments.forEach { argument ->
val count = result.count { it.name == argument.name }
if (count !in argument.min..argument.max)
throw ArgumentCountException(argument.name, argument.min, argument.max)
@@ -164,7 +161,7 @@ class Command private constructor(
}
}
class Key(
class Key internal constructor(
internal val index: Int,
)
}

View File

@@ -19,45 +19,29 @@ class Execution(
operator fun Command.Key.getValue(
thisRef: Any?,
property: KProperty<*>,
): String {
return commands[index]
.name
}
): String = commands[index].name
operator fun <T> Argument.Single<T>.getValue(
thisRef: Any?,
property: KProperty<*>,
): T {
return arguments
.single { it.name == name }
): T = arguments.single { it.name == name }
.let { parser.parse(it.value) }
}
operator fun <T> Argument.SingleOrElse<T>.getValue(
thisRef: Any?,
property: KProperty<*>,
): T {
return arguments
.singleOrNull { it.name == name }
?.let { parser.parse(it.value) }
?: other
}
): T = arguments.singleOrNull { it.name == name }
?.let { parser.parse(it.value) } ?: other
operator fun <T> Argument.SingleOrNull<T>.getValue(
thisRef: Any?,
property: KProperty<*>,
): T? {
return arguments
.singleOrNull { it.name == name }
): T? = arguments.singleOrNull { it.name == name }
?.let { parser.parse(it.value) }
}
operator fun <T> Argument.Multiple<T>.getValue(
thisRef: Any?,
property: KProperty<*>,
): List<T> {
return arguments
.filter { it.name == name }
): List<T> = arguments.filter { it.name == name }
.map { parser.parse(it.value) }
}
}