Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Apr 2, 2023
1 parent 41e7f4b commit d0f337a
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,17 @@ abstract class CliktCommand(
* called.
*/
open fun allHelpParams(): List<ParameterHelp> {
return _options.mapNotNull { it.parameterHelp(currentContext) } +
_arguments.mapNotNull { it.parameterHelp(currentContext) } +
_groups.mapNotNull { it.parameterHelp(currentContext) } +
_subcommands.mapNotNull {
when {
it.hidden -> null
else -> ParameterHelp.Subcommand(it.commandName, it.shortHelp(), it.helpTags)
}
return listOf(
_options.mapNotNull { it.parameterHelp(currentContext) },
_arguments.mapNotNull { it.parameterHelp(currentContext) },
_groups.mapNotNull { it.parameterHelp(currentContext) },
_subcommands.mapNotNull {
when {
it.hidden -> null
else -> ParameterHelp.Subcommand(it.commandName, it.shortHelp(), it.helpTags)
}
}
).flatten()
}

private fun getCommandNameWithParents(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ interface OptionDelegate<T> :
val value: T

/** Implementations must call [ParameterHolder.registerOption] */
operator fun provideDelegate(
override operator fun provideDelegate(
thisRef: ParameterHolder,
prop: KProperty<*>,
property: KProperty<*>,
): ReadOnlyProperty<ParameterHolder, T>

override fun getValue(thisRef: ParameterHolder, property: KProperty<*>): T = value
Expand Down Expand Up @@ -181,19 +181,16 @@ internal fun Option.getFinalValue(
} ?: FinalValue.Parsed(emptyList())
}

// This is a pretty ugly hack: option groups need to enforce their contraints, including on options
// This is a pretty ugly hack: option groups need to enforce their constraints, including on options
// from envvars/value sources, but not including default values. Unfortunately, we don't know
// whether an option's value is from a default or envvar. So we do some ugly casts and read the
// final value again to check for values from other sources.
internal fun Option.hasEnvvarOrSourcedValue(
context: Context,
invocations: List<OptionParser.Invocation>,
invocations: List<Invocation>,
): Boolean {
val envvar = when (this) {
is OptionWithValues<*, *, *> -> envvar
is FlagOption<*> -> envvar
else -> null
}

val envvar = (this as? OptionWithValues<*, *, *>)?.envvar
val final = this.getFinalValue(context, invocations, envvar)
return final !is FinalValue.Parsed
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.ajalt.clikt.parsers

import com.github.ajalt.clikt.core.*
import com.github.ajalt.clikt.internal.finalizeOptions
import com.github.ajalt.clikt.mpp.readFileIfExists
import com.github.ajalt.clikt.parameters.arguments.Argument
import com.github.ajalt.clikt.parameters.options.Option
import com.github.ajalt.clikt.parameters.options.splitOptionPrefix
Expand Down Expand Up @@ -360,7 +359,7 @@ internal object Parser {
name,
optionsByName.filterNot { it.value.hidden }.keys.toList()
)
return OptParseResult(1, err = Err(NoSuchOption(name, possibilities).also { it.context = context }, index))
return OptParseResult(1, err = Err(NoSuchOption(name, possibilities), index))
}

return parseOptValues(
Expand Down Expand Up @@ -446,7 +445,7 @@ internal object Parser {
prefix == "-" && "-$tok" in optionsByName -> listOf("-$tok")
else -> emptyList()
}
return OptParseResult(1, err = Err(NoSuchOption(name, possibilities).also { it.context = context }, index))
return OptParseResult(1, err = Err(NoSuchOption(name, possibilities), index))
}
if (option.nvalues.last > 0) {
val value = if (i < tok.lastIndex) tok.drop(i + 1) else null
Expand Down Expand Up @@ -492,8 +491,8 @@ internal object Parser {

if (consumed > remaining) {
val e = when (remaining) {
0 -> MissingArgument(argument).also { it.context = context }
else -> IncorrectArgumentValueCount(argument).also { it.context = context }
0 -> MissingArgument(argument)
else -> IncorrectArgumentValueCount(argument)
}
return ArgsParseResult(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.testing.TestCommand
import com.github.ajalt.clikt.testing.formattedMessage
import com.github.ajalt.clikt.testing.parse
import com.github.ajalt.clikt.testing.skipDueToKT43490
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.data.blocking.forAll
import io.kotest.data.row
Expand Down Expand Up @@ -128,7 +127,6 @@ class CliktCommandTest {
@Test
@JsName("command_usage")
fun `command usage`() {
if (skipDueToKT43490) return
class Parent : TestCommand(called = false) {
val arg by argument()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.testing.TestCommand
import com.github.ajalt.clikt.testing.parse
import com.github.ajalt.clikt.testing.skipDueToKT43490
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
Expand Down Expand Up @@ -90,7 +89,6 @@ class ContextTest {
@Test
@JsName("default_help_option_names")
fun `default help option names`() {
if (skipDueToKT43490) return
class C : TestCommand()

shouldThrow<PrintHelpMessage> { C().parse("--help") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,157 +608,6 @@ class CliktHelpFormatterTest {
""".trimMargin()
}

@Test
@Suppress("unused")
@JsName("integration_test")
fun `integration test`() {
class G : OptionGroup("My Group", help = "this is my group") {
val groupFoo by option(help = "foo for group").required()
val bar by option("-g", "--group-bar", help = "bar for group")
}

class G2 : OptionGroup("Another group") {
val groupBaz by option(help = "this group doesn't have help").required()
}

class C : NoOpCliktCommand(
name = "program",
help = """
This is a program.
This is the prolog.
""",
epilog = "This is the epilog"
) {
val g by G().cooccurring()
val g2 by G2().cooccurring()
val ex by mutuallyExclusiveOptions(
option("--ex-foo", help = "exclusive foo"),
option("--ex-bar", help = "exclusive bar")
).help(
name = "Exclusive",
help = "These options are exclusive"
)
val ex2 by mutuallyExclusiveOptions(
option("--ex-baz", help = "exclusive baz"),
option("--ex-qux", help = "exclusive qux"),
option("--ex-quz", help = "exclusive quz"),
name = "Exclusive without help"
)
val foo by option(help = "foo option help").int().required()
val bar by option("-b", "--bar", help = "bar option help", metavar = "META").default("optdef")
val baz by option(help = "baz option help").flag("--no-baz")
val good by option().flag("--bad", default = true, defaultForHelp = "good").help("good option help")
val feature by option().switch("--one" to 1, "--two" to 2).default(0, defaultForHelp = "zero")
.help("feature switch")
val hidden by option(help = "hidden", hidden = true)
val multiOpt by option(help = "multiple").multiple(required = true)
val arg by argument()
val multi by argument().multiple(required = true)
val defArg by argument(help = "has default").default("def")

init {
context {
helpFormatter = CliktHelpFormatter(
showDefaultValues = true,
showRequiredTag = true,
requiredOptionMarker = "*"
)
}

eagerOption("--eager", "-e", help = "this is an eager option with a group", groupName = "My Group") {}
eagerOption("--eager2", "-E", help = "this is an eager option") {}
}
}

class Sub : NoOpCliktCommand(helpTags = mapOf("deprecated" to "")) {
override val commandHelp: String = """
a subcommand
with extra help
"""
}

class Sub2 : NoOpCliktCommand(help = "another command")

val c = C()
.versionOption("1.0")
.subcommands(Sub(), Sub2())
.context { helpFormatter = CliktHelpFormatter() }

c.getFormattedHelp() shouldBe """
|Usage: program [OPTIONS] ARG MULTI... [DEFARG] COMMAND [ARGS]...
|
| This is a program.
|
| This is the prolog.
|
|My Group:
|
| this is my group
|
|* --group-foo TEXT foo for group (required)
| -g, --group-bar TEXT bar for group
| -e, --eager this is an eager option with a group
|
|Another group:
|* --group-baz TEXT this group doesn't have help (required)
|
|Exclusive:
|
| These options are exclusive
|
| --ex-foo TEXT exclusive foo
| --ex-bar TEXT exclusive bar
|
|Exclusive without help:
| --ex-baz TEXT exclusive baz
| --ex-qux TEXT exclusive qux
| --ex-quz TEXT exclusive quz
|
|Options:
|* --foo INT foo option help (required)
| -b, --bar META bar option help (default: optdef)
| --baz / --no-baz baz option help
| --good / --bad good option help (default: good)
| --one, --two feature switch (default: zero)
|* --multi-opt TEXT multiple (required)
| -E, --eager2 this is an eager option
| --version Show the version and exit
| -h, --help Show this message and exit
|
|Arguments:
| ARG
| MULTI
| DEFARG has default (default: def)
|
|Commands:
| sub a subcommand (deprecated)
| sub2 another command
|
|This is the epilog
""".trimMargin()
}

@Test
@JsName("required_option_marker")
fun `required option marker`() {
val f = CliktHelpFormatter(width = 54, requiredOptionMarker = "*")
f.formatHelp(
"", "", l(
opt(l("--aa", "-a"), "INT", "aa option help"),
opt(l("--bb", "-b"), "INT", "bb option help", tags = mapOf(HelpFormatter.Tags.REQUIRED to ""))
), programName = "prog"
) shouldBe
"""
|Usage: prog [OPTIONS]
|
|Options:
| -a, --aa INT aa option help
|* -b, --bb INT bb option help
""".trimMargin()
}

@Test
@JsName("required_option_tag")
fun `required option tag`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.testing.TestCommand
import com.github.ajalt.clikt.testing.formattedMessage
import com.github.ajalt.clikt.testing.parse
import com.github.ajalt.clikt.testing.skipDueToKT43490
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.data.blocking.forAll
import io.kotest.data.row
Expand All @@ -25,7 +24,6 @@ class ArgumentTest {
@Test
@JsName("one_required_argument")
fun `one required argument`() {
if (skipDueToKT43490) return
class C : TestCommand(called = false) {
val foo by argument()
}
Expand Down Expand Up @@ -114,7 +112,6 @@ class ArgumentTest {
@Test
@JsName("one_argument_nvalues_2")
fun `one argument nvalues=2`() {
if (skipDueToKT43490) return
class C : TestCommand() {
val x by argument().pair()
override fun run_() {
Expand Down Expand Up @@ -161,7 +158,6 @@ class ArgumentTest {
@Test
@JsName("misused_arguments_with_nvalues_2")
fun `misused arguments with nvalues=2`() {
if (skipDueToKT43490) return
class C : TestCommand() {
val x by argument().pair()
}
Expand All @@ -176,7 +172,6 @@ class ArgumentTest {
@Test
@JsName("misused_arguments_with_nvalues_3")
fun `misused arguments with nvalues=3`() {
if (skipDueToKT43490) return
class C : TestCommand() {
val x by argument().triple()
}
Expand Down Expand Up @@ -242,7 +237,6 @@ class ArgumentTest {
@Test
@JsName("one_required_argument_nvalues_minus_1_empty_argv")
fun `one required argument nvalues=-1 empty argv`() {
if (skipDueToKT43490) return
class C : TestCommand() {
val x by argument().multiple(required = true)
}
Expand Down Expand Up @@ -271,7 +265,6 @@ class ArgumentTest {
@Test
@JsName("two_arguments_nvalues_minus_1_1_empty_argv")
fun `two arguments nvalues=-1_1 empty argv`() {
if (skipDueToKT43490) return
class C : TestCommand(called = false) {
val foo by argument().multiple()
val bar by argument()
Expand Down Expand Up @@ -303,7 +296,6 @@ class ArgumentTest {
@Test
@JsName("two_arguments_nvalues_1_minus_1_empty_argv")
fun `two arguments nvalues=1_-1 empty argv`() {
if (skipDueToKT43490) return
class C : TestCommand(called = false) {
val foo by argument()
val bar by argument().multiple()
Expand Down Expand Up @@ -333,7 +325,6 @@ class ArgumentTest {
@Test
@JsName("argument_validator_non_minus_null")
fun `argument validator non-null`() {
if (skipDueToKT43490) return
var called = false

class C : TestCommand() {
Expand Down Expand Up @@ -435,7 +426,6 @@ class ArgumentTest {
@Test
@JsName("convert_catches_exceptions")
fun `convert catches exceptions`() {
if (skipDueToKT43490) return
class C : TestCommand() {
val x by argument().convert {
when (it) {
Expand Down Expand Up @@ -491,7 +481,6 @@ class ArgumentTest {
@Test
@JsName("punctuation_in_arg_prefix_unix_style_error")
fun `punctuation in arg prefix unix style error`() {
if (skipDueToKT43490) return
class C : TestCommand(called = false) {
val x by argument()
}
Expand Down Expand Up @@ -524,7 +513,6 @@ class ArgumentTest {
@Test
@JsName("punctuation_in_arg_prefix_windows_style_error")
fun `punctuation in arg prefix windows style error`() {
if (skipDueToKT43490) return
class C : TestCommand(called = false) {
init {
context { helpOptionNames = setOf("/help") }
Expand Down

0 comments on commit d0f337a

Please sign in to comment.