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

Add support for running snippets #1166

Merged
merged 1 commit into from
Jul 13, 2022
Merged

Conversation

Gedochao
Copy link
Contributor

@Gedochao Gedochao commented Jul 11, 2022

Fixes #1150

Additionally, this allows to run Scala and Java snippets in a similar fashion as -e allows for scripts.
Context:

  • -e/--script-snippet/--execute-script allows for running code as a .sc script; it's similar to piping code via stdin to _.sc
    • this is meant to be the equivalent of scala -e
$ scala-cli -e 'println("Hello")'
Hello
  • --scala-snippet/--execute-scala allows for running raw .scala code; it's similar to piping code via stdin to _.scala
$ scala-cli --scala-snippet '@main def run() = println("Hello")' 
Hello
  • --java-snippet/--execute-java allows for running raw .java code; it's similar to piping code via stdin to _.java
$ scala-cli --java-snippet 'public class Main { public static void main(String[] args) { System.out.println("Hello"); } }'
Hello

@Gedochao Gedochao changed the title Add support for running expressions Add support for running snippets Jul 11, 2022
@Gedochao Gedochao marked this pull request as ready for review July 12, 2022 09:12
Copy link
Contributor

@alexarchambault alexarchambault left a comment

Choose a reason for hiding this comment

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

Looks great!

Two remarks:

  • it seems the --script-snippet option and all can only be specified a single time each. Is that intended? (to be inline with scala -e maybe?) Changing their types to List[String] would allow them to be specified multiple times, which could be handy.
  • I think it might be possible to preserve the order of these arguments, and add them to the other inputs in the right order too (relative to the other inputs), by using caseapp.core.Indexed, like scriptSnippet: List[Indexed[String]]. We get the other inputs via caseapp.RemainingArgs, which also offers the indices of individual arguments. That way, both --*-snippet options and other inputs would be taken into account in the right order, when computing the default main class for example. (Beware that I didn't use Indexed much, I hope it works fine…)

These points can be addressed or discussed in later PRs if needed, IMO.

website/docs/guides/snippets.md Outdated Show resolved Hide resolved
@Gedochao
Copy link
Contributor Author

Gedochao commented Jul 12, 2022

  • it seems the --script-snippet option and all can only be specified a single time each. Is that intended? (to be inline with scala -e maybe?) Changing their types to List[String] would allow them to be specified multiple times, which could be handy.

Initially I intended to have it as close to scala's -e as possible, but later it became clear that it should at the very least support whatever we have for piped inputs. Supporting a list of snippets passed my mind, but I wanted to get some feedback first. I'll add it in a separate PR.

  • I think it might be possible to preserve the order of these arguments, and add them to the other inputs in the right order too (relative to the other inputs), by using caseapp.core.Indexed, like scriptSnippet: List[Indexed[String]]. We get the other inputs via caseapp.RemainingArgs, which also offers the indices of individual arguments. That way, both --*-snippet options and other inputs would be taken into account in the right order, when computing the default main class for example. (Beware that I didn't use Indexed much, I hope it works fine…)

Hm... the potential benefits of doing this are minimal, if I understand correctly... But I'll try it in a separate PR

@Gedochao Gedochao merged commit 7305250 into VirtusLab:main Jul 13, 2022
@Gedochao Gedochao deleted the run-expression branch July 13, 2022 06:23
@Gedochao Gedochao added the enhancement New feature or request label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scala-cli is missing expression evaluation
2 participants