diff options
author | aleksZubakov <aleks.zubakov@gmail.com> | 2018-08-11 16:59:31 +0300 |
---|---|---|
committer | aleksZubakov <aleks.zubakov@gmail.com> | 2018-08-11 16:59:31 +0300 |
commit | 696598260072fea2329db9140f9635b87033c478 (patch) | |
tree | b65ebaf4c8f2464309953d0293445c0fd88c4070 /runners | |
parent | 8d051eb3b32d7caf44b972fe03f2e0a5fcdd4490 (diff) | |
download | dokka-696598260072fea2329db9140f9635b87033c478.tar.gz dokka-696598260072fea2329db9140f9635b87033c478.tar.bz2 dokka-696598260072fea2329db9140f9635b87033c478.zip |
Cli parser refactoring
Diffstat (limited to 'runners')
-rw-r--r-- | runners/cli/src/main/kotlin/cli/DokkaArgumentsParser.kt | 160 | ||||
-rw-r--r-- | runners/cli/src/main/kotlin/cli/main.kt | 44 |
2 files changed, 78 insertions, 126 deletions
diff --git a/runners/cli/src/main/kotlin/cli/DokkaArgumentsParser.kt b/runners/cli/src/main/kotlin/cli/DokkaArgumentsParser.kt index dd4d6377..7b8f5fa0 100644 --- a/runners/cli/src/main/kotlin/cli/DokkaArgumentsParser.kt +++ b/runners/cli/src/main/kotlin/cli/DokkaArgumentsParser.kt @@ -2,15 +2,16 @@ package org.jetbrains.dokka import kotlinx.cli.* import kotlin.reflect.KProperty -class ParseContext(val cli: CommandLineInterface = CommandLineInterface("dokka")) { - private val map = mutableMapOf<KProperty<*>, (String) -> Unit>() + +class ParseContext(private val cli: CommandLineInterface = CommandLineInterface("dokka")) { + private val transformActions = mutableMapOf<KProperty<*>, (String) -> Unit>() private val flagActions = mutableMapOf<KProperty<*>, () -> Unit>() fun registerFlagAction( keys: List<String>, help: String, - invoke: () -> Unit, - property: KProperty<*> + property: KProperty<*>, + invoke: () -> Unit ) { if (property !in flagActions.keys) { cli.flagAction(keys, help) { @@ -21,32 +22,32 @@ class ParseContext(val cli: CommandLineInterface = CommandLineInterface("dokka") } - fun registerSingleAction( + fun registerSingleOption( keys: List<String>, help: String, - invoke: (String) -> Unit, - property: KProperty<*> + property: KProperty<*>, + invoke: (String) -> Unit ) { - if (property !in map.keys) { + if (property !in transformActions.keys) { cli.singleAction(keys, help) { - map[property]!!(it) + transformActions[property]!!(it) } } - map[property] = invoke + transformActions[property] = invoke } - fun registerRepeatableAction( + fun registerRepeatableOption( keys: List<String>, help: String, - invoke: (String) -> Unit, - property: KProperty<*> + property: KProperty<*>, + invoke: (String) -> Unit ) { - if (property !in map.keys) { + if (property !in transformActions.keys) { cli.repeatingAction(keys, help) { - map[property]!!(it) + transformActions[property]!!(it) } } - map[property] = invoke + transformActions[property] = invoke } fun parse(args: Array<String>) { @@ -99,7 +100,19 @@ fun CommandLineInterface.repeatingAction( ) + class DokkaArgumentsParser(val args: Array<String>, val parseContext: ParseContext) { + class OptionDelegate<T>( + var value: T, + private val action: (delegate: OptionDelegate<T>, property: KProperty<*>) -> Unit + ) { + operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value + operator fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<T> { + action(this, property) + return this + } + } + fun <T> parseInto(constructor: (parseContext: DokkaArgumentsParser) -> T): T { val res = constructor(this) parseContext.parse(args) @@ -110,43 +123,24 @@ class DokkaArgumentsParser(val args: Array<String>, val parseContext: ParseConte keys: List<String>, help: String, transform: (String) -> T - ): OptionDelegate<MutableList<T>> { - val list = mutableListOf<T>() - return object : OptionDelegate<MutableList<T>>(list) { - override fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<MutableList<T>> { - parseContext.registerRepeatableAction( - keys, - help, - { - list.add(transform(it)) - }, - property - - ) - return this - } + ) = OptionDelegate(mutableListOf<T>()) { delegate, property -> + parseContext.registerRepeatableOption(keys, help, property) { + delegate.value.add(transform(it)) } } + fun <T> repeatableOption( + keys: List<String>, + help: String + ) = repeatableOption(keys, help) { it as T } + fun <T> repeatableFlag( keys: List<String>, help: String, initElement: (ParseContext) -> T - ): OptionDelegate<MutableList<T>> { - val list = mutableListOf<T>() - return object : OptionDelegate<MutableList<T>>(list) { - override fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<MutableList<T>> { - parseContext.registerFlagAction( - keys, - help, - { - list.add(initElement(parseContext)) - }, - property - - ) - return this - } + ) = OptionDelegate(mutableListOf<T>()) { delegate, property -> + parseContext.registerFlagAction(keys, help, property) { + delegate.value.add(initElement(parseContext)) } } @@ -155,72 +149,32 @@ class DokkaArgumentsParser(val args: Array<String>, val parseContext: ParseConte help: String, initElement: (ParseContext) -> T, transform: () -> T - ): OptionDelegate<T> { - val element = initElement(parseContext) - return object : OptionDelegate<T>(element) { - override fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<T> { - parseContext.registerFlagAction( - keys, - help, - { - value = transform() - }, - property - ) - - return this - } - } - - } - - fun <T> singleOption( - keys: List<String>, - help: String, - transform: ((String) -> T)? = null, - initElement: (ParseContext) -> T - ): OptionDelegate<T> { - val element: T = initElement(parseContext) - return object : OptionDelegate<T>(element) { - - override fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<T> { - parseContext.registerSingleAction( - keys, - help, - { - val toAdd = if (transform != null) { - transform(it) - } else { - it as T - } - value = toAdd - }, - property - ) - - return this - } + ) = OptionDelegate(initElement(parseContext)) { delegate, property -> + parseContext.registerFlagAction(keys, help, property) { + delegate.value = transform() } } - fun singleBooleanFlag( + fun singleFlag( keys: List<String>, help: String ) = singleFlag(keys, help, { false }, { true }) - fun <T> defaultSingleOption( + fun <T> singleOption( keys: List<String>, help: String, defaultValue: T - ) = singleOption( - keys, - help, - { it as T }, - { defaultValue } - ) -} + ) = singleOption(keys, help, { it as T }, { defaultValue }) -abstract class OptionDelegate<T>(var value: T) { - operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value - abstract operator fun provideDelegate(thisRef: Any, property: KProperty<*>): OptionDelegate<T> + fun <T> singleOption( + keys: List<String>, + help: String, + transform: (String) -> T, + initElement: (ParseContext) -> T + ) = OptionDelegate(initElement(parseContext)) { delegate, property -> + parseContext.registerSingleOption(keys, help, property) { + val toAdd = transform(it) + delegate.value = toAdd + } + } } diff --git a/runners/cli/src/main/kotlin/cli/main.kt b/runners/cli/src/main/kotlin/cli/main.kt index 34d86257..0e17c193 100644 --- a/runners/cli/src/main/kotlin/cli/main.kt +++ b/runners/cli/src/main/kotlin/cli/main.kt @@ -8,22 +8,22 @@ import java.net.URL import java.net.URLClassLoader open class GlobalArguments(parser: DokkaArgumentsParser) : DokkaConfiguration { - override val outputDir: String by parser.defaultSingleOption( + override val outputDir: String by parser.singleOption( listOf("-output"), "Output directory path", "") - override val format: String by parser.defaultSingleOption( + override val format: String by parser.singleOption( listOf("-format"), "Output format (text, html, markdown, jekyll, kotlin-website)", "") - override val generateIndexPages: Boolean by parser.singleBooleanFlag( + override val generateIndexPages: Boolean by parser.singleFlag( listOf("-generateIndexPages"), "Generate index page" ) - override val cacheRoot: String? by parser.defaultSingleOption( + override val cacheRoot: String? by parser.singleOption( listOf("-cacheRoot"), "Path to cache folder, or 'default' to use ~/.cache/dokka, if not provided caching is disabled", null) @@ -31,7 +31,7 @@ open class GlobalArguments(parser: DokkaArgumentsParser) : DokkaConfiguration { override val impliedPlatforms: List<String> by parser.repeatableOption( listOf("-impliedPlatforms"), "List of implied platforms (comma-separated)" - ) { it } + ) override val passesConfigurations: List<Arguments> by parser.repeatableFlag( listOf("-pass"), @@ -42,7 +42,7 @@ open class GlobalArguments(parser: DokkaArgumentsParser) : DokkaConfiguration { } class Arguments(val parser: DokkaArgumentsParser) : DokkaConfiguration.PassConfiguration { - override val moduleName: String by parser.defaultSingleOption( + override val moduleName: String by parser.singleOption( listOf("-module"), "Name of the documentation module", "") @@ -50,7 +50,7 @@ class Arguments(val parser: DokkaArgumentsParser) : DokkaConfiguration.PassConfi override val classpath: List<String> by parser.repeatableOption( listOf("-classpath"), "Classpath for symbol resolution" - ) { it } + ) override val sourceRoots: List<DokkaConfiguration.SourceRoot> by parser.repeatableOption( listOf("-src"), @@ -60,30 +60,30 @@ class Arguments(val parser: DokkaArgumentsParser) : DokkaConfiguration.PassConfi override val samples: List<String> by parser.repeatableOption( listOf("-samples"), "Source root for samples" - ) { it } + ) override val includes: List<String> by parser.repeatableOption( listOf("-include"), "Markdown files to load (allows many paths separated by the system path separator)" - ) { it } + ) - override val includeNonPublic: Boolean by parser.singleBooleanFlag( + override val includeNonPublic: Boolean by parser.singleFlag( listOf("-includeNonPublic"), "Include non public") - override val includeRootPackage: Boolean by parser.singleBooleanFlag( + override val includeRootPackage: Boolean by parser.singleFlag( listOf("-includeRootPackage"), "Include non public") - override val reportUndocumented: Boolean by parser.singleBooleanFlag( + override val reportUndocumented: Boolean by parser.singleFlag( listOf("-reportUndocumented"), "Include non public") - override val skipEmptyPackages: Boolean by parser.singleBooleanFlag( + override val skipEmptyPackages: Boolean by parser.singleFlag( listOf("-skipEmptyPackages"), "Include non public") - override val skipDeprecated: Boolean by parser.singleBooleanFlag( + override val skipDeprecated: Boolean by parser.singleFlag( listOf("-skipDeprecated"), "Include non public") @@ -94,32 +94,31 @@ class Arguments(val parser: DokkaArgumentsParser) : DokkaConfiguration.PassConfi { 6 } ) - override val languageVersion: String? by parser.defaultSingleOption( + override val languageVersion: String? by parser.singleOption( listOf("-languageVersion"), "Language Version to pass to Kotlin Analysis", null) - override val apiVersion: String? by parser.defaultSingleOption( + override val apiVersion: String? by parser.singleOption( listOf("-apiVesion"), "Kotlin Api Version to pass to Kotlin Analysis", null ) - override val noStdlibLink: Boolean by parser.singleBooleanFlag( + override val noStdlibLink: Boolean by parser.singleFlag( listOf("-noStdlibLink"), "Disable documentation link to stdlib") - override val noJdkLink: Boolean by parser.singleBooleanFlag( + override val noJdkLink: Boolean by parser.singleFlag( listOf("-noJdkLink"), "Disable documentation link to stdlib") override val suppressedFiles: List<String> by parser.repeatableOption( listOf("-suppresedFiles"), - "", - { it } + "" ) - override val collectInheritedExtensionsFromLibraries: Boolean by parser.singleBooleanFlag( + override val collectInheritedExtensionsFromLibraries: Boolean by parser.singleFlag( listOf("-collectInheritedExtensionsFromLibraries"), "Search for applicable extensions in libraries") @@ -132,8 +131,7 @@ class Arguments(val parser: DokkaArgumentsParser) : DokkaConfiguration.PassConfi override val targets: List<String> by parser.repeatableOption( listOf("-targets"), - "Generation targets", - { it } + "Generation targets" ) override val sourceLinks: List<DokkaConfiguration.SourceLinkDefinition> |