Skip to content

Support enum or union types in @main case class #200

@seveneves

Description

@seveneves

Mainargs already support case class derivation via ParserForClass which allows for argument sharing

@main def main1(
  args1: Arguments,
  args1: OtherArguments,
): Unit = ???

@main def main2(
  args1: Arguments,
): Unit = ???

@main case class Arguments(foo: String, bar: Int)
object Arguments:
  given parser: ParserForClass[Arguments] = ParserForClass[Arguments]

@main case class OtherArguments(zap: String, cat: Int)
object OtherArguments:
  given parser: ParserForClass[OtherArguments] = ParserForClass[OtherArguments]

This approach allows for a simple composition of different argument groups. Each @main def can have its own set of
arguments.

However, if one wants to have a way of execution of subcommands or different execution branch, it is not possible to do with this approach. Currently, one needs to introduce multiple @main def methods to achieve subcommand functionality.

If enum or union data types were supported, it would allow for a more elegant solution to subcommands. Each
subcommand could be represented as a case class within an enum or union, and the main method could then take an instance
of this enum or union type. This would enable the execution of different subcommands based on the provided arguments.

This should help solving issue #57.

If you think this is possible to achieve in current codebase, with some guidance, I can work on a PR to support this in Scala 3 (and 2 if there is interest or feature set should be consistent across different scala versions).

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