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
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
Elena Lepilkina authored and Elena Lepilkina committed Jul 9, 2019
commit 1c677761b84fac296ed4aafa70bb22ce8f8c86ad
16 changes: 15 additions & 1 deletion kliopt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,25 @@ kotlin {
implementation kotlin('test-junit')
}
}

jvm().compilations.all {
kotlinOptions {
freeCompilerArgs = ["-Xuse-experimental=org.jetbrains.kliopt.ExperimentalCli"]
suppressWarnings = true
}
}
}

targets {
//fromPreset(presets.mingwX64, 'windows')
//fromPreset(presets.linuxX64, 'linux')
fromPreset(presets.macosX64, 'macos')
fromPreset(presets.macosX64, 'macos') {
compilations.all {
kotlinOptions {
freeCompilerArgs = ["-Xuse-experimental=org.jetbrains.kliopt.ExperimentalCli"]
suppressWarnings = true
}
}
}
}
}
38 changes: 32 additions & 6 deletions kliopt/src/main/kotlin/org/jetbrains/kliopt/ArgType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@

package org.jetbrains.kliopt

// Possible types of arguments.
open abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
// Type description for help messages.
/**
* Possible types of arguments.
*
* Inheritors describe type of argument value. New types can be added by user.
* In case of options type can have parameter or not.
*/
abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
/**
* Text description of type for helpMessage.
*/
abstract val description: kotlin.String
// Function to convert string argument value to its type.
// In case of error during convertion also provide help message.

/**
* Function to convert string argument value to its type.
* In case of error during convertion also provides help message.
*
* @param value value
*/
abstract val convertion: (value: kotlin.String, name: kotlin.String, helpMessage: kotlin.String)->T

// Flags that can be only set/unset.
/**
* Argument type for flags that can be only set/unset.
*/
object Boolean : ArgType<kotlin.Boolean>(false) {
override val description: kotlin.String
get() = ""
Expand All @@ -22,6 +36,9 @@ open abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
get() = { value, _ , _ -> if (value == "false") false else true }
}

/**
* Argument type for string values.
*/
object String : ArgType<kotlin.String>(true) {
override val description: kotlin.String
get() = "{ String }"
Expand All @@ -30,6 +47,9 @@ open abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
get() = { value, _, _ -> value }
}

/**
* Argument type for integer values.
*/
object Int : ArgType<kotlin.Int>(true) {
override val description: kotlin.String
get() = "{ Int }"
Expand All @@ -39,6 +59,9 @@ open abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
?: error("Option $name is expected to be integer number. $value is provided.\n$helpMessage") }
}

/**
* Argument type for double values.
*/
object Double : ArgType<kotlin.Double>(true) {
override val description: kotlin.String
get() = "{ Double }"
Expand All @@ -49,6 +72,9 @@ open abstract class ArgType<T : Any>(val hasParameter: kotlin.Boolean) {
?: error("Option $name is expected to be double number. $value is provided.\n$helpMessage") }
}

/**
* Type for arguments that have limited set of possible values.
*/
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 }"
Expand Down
36 changes: 36 additions & 0 deletions kliopt/src/main/kotlin/org/jetbrains/kliopt/ExperimentalCli.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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

import kotlin.annotation.AnnotationTarget.*

/**
* This annotation marks the experimental API for working with command line arguments.
*
* > Beware using the annotated API especially if you're developing a library, since your library might become binary incompatible
* with the future versions of the CLI library.
*
* Any usage of a declaration annotated with `@ExperimentalCli` must be accepted either by
* annotating that usage with the [UseExperimental] annotation, e.g. `@UseExperimental(ExperimentalCli::class)`,
* or by using the compiler argument `-Xuse-experimental=org.jetbrains.kliopt.ExperimentalCli`.
*/
@Experimental(level = Experimental.Level.ERROR)
@Retention(AnnotationRetention.BINARY)
@Target(
CLASS,
ANNOTATION_CLASS,
PROPERTY,
FIELD,
LOCAL_VARIABLE,
VALUE_PARAMETER,
CONSTRUCTOR,
FUNCTION,
PROPERTY_GETTER,
PROPERTY_SETTER,
TYPEALIAS
)
@SinceKotlin("1.3")
public annotation class ExperimentalCli
Loading