Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

New CLI tool #3085

Merged
merged 12 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Started rewriting argParser
  • Loading branch information
Elena Lepilkina authored and Elena Lepilkina committed Jul 9, 2019
commit a816793b0fdbed6c900ee9ebdff0fd5831faf54f
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private fun makeDependencyAssigner(targets: List<String>, defFiles: List<File>)

private fun makeDependencyAssignerForTarget(target: String, defFiles: List<File>): SingleTargetDependencyAssigner {
val tool = prepareTool(target, KotlinPlatform.NATIVE)
val argParser = ArgParser(getCInteropArguments(), useDefaultHelpShortName = false)
val argParser = ArgParser(getCInteropArguments(), useDefaultHelpShortName = false,
prefixStyle = ArgParser.OPTION_PREFIX_STYLE.JVM)
argParser.parse(arrayOf<String>())
val libraries = defFiles.associateWith {
buildNativeLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ private fun findFilesByGlobs(roots: List<Path>, globs: List<String>): Map<Path,


private fun processCLib(args: Array<String>, additionalArgs: Map<String, Any> = mapOf()): Array<String>? {
val argParser = ArgParser(getCInteropArguments(), useDefaultHelpShortName = false)
val argParser = ArgParser(getCInteropArguments(), useDefaultHelpShortName = false,
prefixStyle = ArgParser.OPTION_PREFIX_STYLE.JVM)
if (!argParser.parse(args))
return null
val ktGenRoot = argParser.get<String>("generated")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ const val idlMathPackage = "kotlinx.interop.wasm.math"
const val idlDomPackage = "kotlinx.interop.wasm.dom"

fun processIdlLib(args: Array<String>, additionalArgs: Map<String, Any> = mapOf()): Array<String>? {
val argParser = ArgParser(getJSInteropArguments(), useDefaultHelpShortName = false)
val argParser = ArgParser(getJSInteropArguments(), useDefaultHelpShortName = false,
prefixStyle = ArgParser.OPTION_PREFIX_STYLE.JVM)
if (!argParser.parse(args))
return null
// TODO: Refactor me.
Expand Down
75 changes: 75 additions & 0 deletions kliopt/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
buildscript {
ext.rootBuildDirectory = file('..')

apply from: "$rootBuildDirectory/gradle/loadRootProperties.gradle"
apply from: "$rootBuildDirectory/gradle/kotlinGradlePlugin.gradle"

repositories {
maven {
url 'https://cache-redirector.jetbrains.com/jcenter'
}
maven {
url kotlinCompilerRepo
}
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

apply plugin: 'kotlin-multiplatform'

repositories {
maven {
url 'https://cache-redirector.jetbrains.com/jcenter'
}
maven {
url kotlinCompilerRepo
}
maven {
url buildKotlinCompilerRepo
}
}

kotlin {
sourceSets {
commonMain {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion"
}
kotlin.srcDir 'src/main/kotlin'

}
commonTest {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion"
implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion"
}
kotlin.srcDir 'src/tests'
}
nativeMain {
dependsOn commonMain
}
linuxMain { dependsOn nativeMain }
windowsMain { dependsOn nativeMain }
macosMain {dependsOn nativeMain }
jvm().compilations.main.defaultSourceSet {
dependencies {
implementation kotlin('stdlib-jdk8')
}
}
// JVM-specific tests and their dependencies:
jvm().compilations.test.defaultSourceSet {
dependencies {
implementation kotlin('test-junit')
}
}
}

targets {
fromPreset(presets.mingwX64, 'windows')
fromPreset(presets.linuxX64, 'linux')
fromPreset(presets.macosX64, 'macos')
}
}
2 changes: 2 additions & 0 deletions kliopt/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.jetbrains.kotlin.native.home=../dist
org.jetbrains.kotlin.native.jvmArgs=-Xmx6G
Empty file added kliopt/settings.gradle
Empty file.
62 changes: 62 additions & 0 deletions kliopt/src/main/kotlin/org/jetbrains/kliopt/ArgType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/

package org.jetbrains.kliopt

// Possible types of arguments.
// TODO Make extension points for new types? Not sealed?
sealed class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
// Type description for help messages.
abstract val description: kotlin.String
// Function to convert string argument value to its type.
// In case of error during convertion also provide help message.
abstract val convertion: (value: kotlin.String, name: kotlin.String, helpMessage: kotlin.String)->T

// Flags that can be only set/unset.
class Boolean : ArgType<kotlin.Boolean>(false) {
LepilkinaElena marked this conversation as resolved.
Show resolved Hide resolved
override val description: kotlin.String
get() = ""

override val convertion: (value: kotlin.String, name: kotlin.String, _: kotlin.String) -> kotlin.Boolean
get() = { value, _ , _ -> if (value == "false") false else true }
}

class String : ArgType<kotlin.String>(true) {
override val description: kotlin.String
get() = "{ String }"

override val convertion: (value: kotlin.String, name: kotlin.String, _: kotlin.String) -> kotlin.String
get() = { value, _, _ -> value }
}

class Int : ArgType<kotlin.Int>(true) {
override val description: kotlin.String
get() = "{ Int }"

override val convertion: (value: kotlin.String, name: kotlin.String, helpMessage: kotlin.String) -> kotlin.Int
get() = { value, name, helpMessage -> value.toIntOrNull()
?: error("Option $name is expected to be integer number. $value is provided.\n$helpMessage") }
}

class Double : ArgType<kotlin.Double>(true) {
override val description: kotlin.String
get() = "{ Double }"

override val convertion: (value: kotlin.String, name: kotlin.String,
helpMessage: kotlin.String) -> kotlin.Double
get() = { value, name, helpMessage -> value.toDoubleOrNull()
?: error("Option $name is expected to be double number. $value is provided.\n$helpMessage") }
}

class Choice(val values: List<kotlin.String>) : ArgType<kotlin.String>(true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't some characters be prohibited in values such as spaces and commas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I amn't sure why we should provide such limitations, it can be any string value. What problems do you see?

override val description: kotlin.String
get() = "{ Value should be one of $values }"

override val convertion: (value: kotlin.String, name: kotlin.String,
helpMessage: kotlin.String) -> kotlin.String
get() = { value, name, helpMessage -> if (value in values) value
else error("Option $name is expected to be one of $values. $value is provided.\n$helpMessage") }
}
}
Loading