Skip to content

Conversation

@keynmol
Copy link

@keynmol keynmol commented Apr 12, 2025

Hello 👋

Glad I remembered about this library. I'm building a CLI with Scala Native and thought I'd give it a go. It works, that's great!

This PR bumps SN to 0.5, and simplifies the tests to not use Cats Effect (which is not yet published for 0.5).

Please let me know if you are willing to release a new version with these changes, and if not, I can fork the project. Thanks again for your work.


I've been using it generically along with my project decline-derive to add completions to any command:

case class PrintCompletions(value: String)

given withCompletions[T](using
    t: CommandApplication[T]
): CommandApplication[T | PrintCompletions] =
  new:
    val newSubcommands =
      val bash =
        Command("bash", "output autocompletion script for bash")(
          Opts(
            PrintCompletions(Completion.bashCompletion(command))
          )
        )

      val zsh = Command("zsh", "output autocompletion script for zsh")(
        Opts(
          PrintCompletions(Completion.zshBashcompatCompletion(command))
        )
      )

      val completion = Command(
        "completion",
        "output autocompletion scripts for common shells"
      ) {
        Opts.subcommands(bash, zsh)
      }

      t.subcommands :+ completion
    end newSubcommands

    override def command: Command[T | PrintCompletions] =
      Command[T | PrintCompletions](t.command.name, t.command.header)(
        Opts.subcommands(newSubcommands.head, newSubcommands.tail*)
      )
    override def subcommands: List[Command[T | PrintCompletions]] =
      newSubcommands

And then at usage site:

        CommandApplication.parseOrExit[CLI | PrintCompletions](args) match
          case PrintCompletions(value) => print(value)
          case cli: CLI.New            => commandNew(context, cli)

It works great!

lazy val core = crossProject(JVMPlatform, NativePlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("."))
.in(file("core"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting weird compilation errors because of implicit root project being created, but without dependencies

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

Successfully merging this pull request may close these issues.

1 participant