Skip to content

Putting @main in a mixin #145

Open
Open
@kincsescsaba

Description

@kincsescsaba

Discussed in #144

Originally posted by kincsescsaba August 7, 2024
Hey there,

I'm not sure if this is an issue or more like a feature request, but when I try to place the @main annotation of mainargs in a trait, that is mixed into the class that contains the main method, I get a No @main methods declared error.

I tried referencing the self type from ParserForMethods but that did not fix the issue.

Here's how the approach looks like:

abstract class CaskMainWithArgs extends cask.Main:
  self: Conf =>
  private var args_ : Seq[String] = uninitialized
  def args: Seq[String] = args_
  
  override def main(argsArray: Array[String]): Unit =
    if Option(args_).isEmpty then
      args_ = argsArray.toSeq
      ParserForMethods(self).runOrExit(argsArray)
      super.main(argsArray)

trait Conf:
  self: CaskMainWithArgs =>

  @main
  def runConfInit(@arg(name = "conf", short = 'c', doc = "Set a custom configuration file")
                  configFile: String) =
    println("Hello from runConfInit")

Any ideas on how this behavior could be implemented?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions