Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable CommandParser #57

Open
larsrh opened this issue Jun 9, 2017 · 2 comments
Open

Customizable CommandParser #57

larsrh opened this issue Jun 9, 2017 · 2 comments

Comments

@larsrh
Copy link
Contributor

larsrh commented Jun 9, 2017

The current ways to construct a CommandParser are fairly limited. It would be cool to have something like this:

CommandParser.fromRecord(
  "build" ->> BuildCommand ::
  "check" ->> CheckCommand ::
  HNil
)

I'm a little fuzzy on the details, though. Ideally, this should support common and command-specific options, e.g.

cli-app --config /home/lars/.local/cli-app.conf build --session HOL

Maybe something like this?

CommandParser.fromRecord[Common] { common =>
  "build" ->> BuildCommand(common) ::
  "check" ->> CheckCommand(common) ::
  HNil
}

... where the return type of *Command is some Parser[T].

@larsrh
Copy link
Contributor Author

larsrh commented Jun 9, 2017

I just realized one could possibly build that on top of the existing primitives.

case class Common(x: Int = 3, y: String = "bla")
case class Command[T](@Recurse common: Common, @Recurse specific: T)

def parseAux[T, Aux](args: List[String])(implicit aux: Parser.Aux[T, Aux]) =
  CaseApp.parse[Command[T]](args)                                                                                                                                   

// implicit not found when trying to define this directly, needs `parseAux`
def parseCmd[T : Parser](args: List[String]) = parseAux(args)(Parser[T])                                                                   

case class Cmd1(lol: String = "foo")
parseCmd[Cmd1](List("--lol", "fooo", "-x", "3"))

And then build some implicits recursing over a record, constructing these composite parsers.

@alexarchambault
Copy link
Owner

I've been using a workaround for that too, in coursier: https://github.com/coursier/coursier/blob/b4dd43e03c478fb1d72fe49999751f9cf6e8e6be/cli/src/main/scala-2.11/coursier/cli/Coursier.scala.

I'd like to have something along the lines of your first example work too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants