clikt icon indicating copy to clipboard operation
clikt copied to clipboard

Can't set context

Open benkeil opened this issue 6 months ago • 3 comments

class HelloWorld : CliktCommand() {
  init {
    context { readArgumentFile = null }
  }

  val foo by option("--foo", "-f").required()

  override fun run() {
    println("Hello, $foo")
  }
}

HelloWorld().context { readArgumentFile = null }.main(args)

None of the context overrides works.

./github.main.kts --foo @types/node
warning: argfile not found: /Users/foobar/development/github.com/project/types/node
Usage: hello-world [<options>]

Error: option --foo requires a value

benkeil avatar Aug 20 '25 10:08 benkeil

Hm, I can't reproduce this. If I run your exact code, I get Hello, @types/node. There's also a unit test for this. Are you on the latest version of Clikt?

ajalt avatar Aug 24 '25 16:08 ajalt

Yes I am using 5.0.3. I tested a little bit more but I couldn't figured it out...

We are using Kotlinscript.

#!/usr/bin/env kotlin
@file:DependsOn("com.github.ajalt.clikt:clikt-jvm:5.0.3")

class C : CliktCommand() {
  init {
    context { readArgumentFile = null }
  }

  val arg by option("--arg")

  override fun run() {
    println(arg)
  }
}

C().main(listOf("--arg", "@asd"))
./github.main.kts                                                                                                                                      
@asd
./github.main.kts --arg @asd
warning: argfile not found: /Users/user/development/github.com/org/repo/asd
@asd

The warning should not come at all, because we don't use args in the code, or?

benkeil avatar Sep 01 '25 06:09 benkeil

Does this only happen with kotlinscript? Can you try it with a regular project?

ajalt avatar Sep 02 '25 16:09 ajalt