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 - main
env: env:
GIT__TOKEN: ${{ secrets.TOKEN }} INFENDRO__TOKEN: ${{ secrets.TOKEN }}
jobs: jobs:
publish: 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. | Target | Status |
* Supports both named and wildcard commands. |------------------|---------------|
* Declare and parse type-safe arguments. | JVM | Supported |
* Multiplatform support | JavaScript | Supported |
* JVM | Native (Linux) | Supported |
* JavaScript | Native (Windows) | Not Supported |
* Native (Linux)
## Installation ## Installation

View File

@@ -1,11 +1,11 @@
import com.infendro.plugin.infendro
import com.infendro.plugin.token
group = "com.infendro" group = "com.infendro"
version = "1.3.0" version = "1.3.0"
repositories {
mavenCentral()
}
plugins { plugins {
alias(libs.plugins.infendro)
alias(libs.plugins.multiplatform) alias(libs.plugins.multiplatform)
id("maven-publish") id("maven-publish")
} }
@@ -16,33 +16,12 @@ kotlin {
nodejs() nodejs()
} }
linuxX64() linuxX64()
}
publishing {
repositories { repositories {
maven { mavenCentral()
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"
}
}
} }
} }
fun secret( publishing.repositories {
key: String, infendro(token)
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
} }

View File

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

View File

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

View File

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