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).
Mainargs already support case class derivation via
ParserForClasswhich allows for argument sharingThis approach allows for a simple composition of different argument groups. Each
@main defcan have its own set ofarguments.
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 defmethods to achieve subcommand functionality.If
enumoruniondata types were supported, it would allow for a more elegant solution to subcommands. Eachsubcommand 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).