Skip to content

Document how to use Decline with Ammonite (specifically manually passing args) #270

Description

@mdedetrich

So I am trying to use decline within Ammonite and there doesn't seem to be a good uesr guide on how to do this. From the docs on Ammonite, i.e. https://ammonite.io/#ScriptArguments you can access the command line arguments by doing the following

@main
def entrypoint(args: String*) = {

}

Assuming you have a an app MyApp as follows (taken from example)

object MyApp extends CommandApp(
  name = "my-app",
  header = "This compiles to JavaScript!",
  main = {
    val loudOpt = Opts.flag("loud", "Do something noisy!").orFalse
    
    for (loud <- loudOpt) yield {
      if (loud) println("HELLO WORLD!")
      else println("hello world!")
    }
  }
)

You would do something like this

@main
def entrypoint(args: String*) = {
  MyApp.main(args.toArray)
}

The problem is that the main method in CommandApp is apparently deprecated, i.e.

  @deprecated(
    """
The CommandApp.main method is not intended to be called by user code.
For suggested usage, see: http://monovore.com/decline/usage.html#defining-an-application""",
    "0.3.0"
  )
  final def main(args: Array[String]): Unit =
    command.parse(PlatformApp.ambientArgs getOrElse args, sys.env) match {
      case Left(help) => System.err.println(help)
      case Right(_) => ()
    }

Ontop of this the command variable inside the CommandApp class is not publicly visible outside the class (its missing the val modifier in the constructor) so its not like you can manually do MyApp.command.parse (should this deprecated method main be removed in the future). So how exactly is one meant to manually parse args to a CommandApp without using the deprecated main method? For example https://ben.kirw.in/decline/scalajs.html#ambient-arguments details how to get the args on various platforms but never how to pass it into a CommandApp and the provided link at https://ben.kirw.in/decline/usage.html#defining-an-application details how you can manually use args when you have a Command but not when you have a CommandApp (this goes to the previous problem where the command inside the CommandApp is not publicly visible).

Conclusively I guess I also don't understand why the CommandApp.main method is deprecated, if you are using something like Ammonite it is by far the easiest way to make the repl/scripts work with an existing CommandApp (which is the ideal way to use decline). This would also be the case for any other kind of dynamic repl/interactive session where args are passed in a different way.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions