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

Scripting: access to command line arguments #157

Closed
SethTisue opened this issue Sep 28, 2021 · 14 comments
Closed

Scripting: access to command line arguments #157

SethTisue opened this issue Sep 28, 2021 · 14 comments
Assignees
Labels
bug Something isn't working

Comments

@SethTisue
Copy link
Contributor

SethTisue commented Sep 28, 2021

the docs say that a hello world script is desugared to:

object hello {
  val message = "Hello from Scala script"
  println(message)

  def main(args: Array[String]): Unit = ()
}

this leaves me unable to reference args

I don't see any information about this on the doc page I linked above

(context: I was hoping I could replace the combination of https://github.com/scala/community-build/blob/2.13.x/advance and https://github.com/scala/community-build/blob/2.13.x/scripts/Advance.scala with a single .sc file, but this is blocking me)

SethTisue added a commit to SethTisue/community-build that referenced this issue Sep 28, 2021
@lrytz
Copy link

lrytz commented Sep 28, 2021

Not sure about .sc files, but it works with main methods like:

➜ cat A.scala
@main def f(s: String) = println(s"Hello $s")
➜ scala-cli A.scala -- World
Hello World

@SethTisue
Copy link
Contributor Author

fwiw, Ammonite also requires defining a @main method if you want access to args, https://ammonite.io/#ScriptArguments

@lrytz reports that defining a @main only works if the extension on the file is .scala as opposed to .sc. sorting that out seems like a design question.

my expectation that an explicit main method shouldn't be necessary comes from three places:

  • typical scripting languages like bash, Python, Perl

  • Scala 2's own script running support, which gives direct access to args:

% echo 'args.foreach(println)' >! S.scala; scala S.scala foo bar
foo
bar
  • sbt's ScriptMain support, which is what I was using before for my scripts (since it allows adding dependencies, unlike Scala's own scripting support)

@SethTisue
Copy link
Contributor Author

SethTisue commented Sep 29, 2021

and it turns out I'm not actually able to use .scala + @main as a workaround, because then dependencies don't work

I have:

#!/usr/bin/env scala-cli --scala-version 3.1.0-RC2                                                                      

import $dep.`org.scala-lang.modules:scala-parallel-collections_3:1.0.3`
import scala.collection.parallel.CollectionConverters._  // for .par     

but I get:

[error] ./advance.scala:9:8: value parallel is not a member of collection
[error]        ^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] ./advance.scala:30:12: Stable identifier required, but <error value parallel is not a member of collection>#CollectionConverters.scala.
[error]   sys
[error] .process.None found
[error]            ^^^^^
Error compiling project (Scala 3.1.0-RC2, JVM)
Compilation failed

tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 1, 2021
tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 4, 2021
Now it is poosible to run

```
echo "println(args)" > a.sc && scala-cli run ./a.sc -- foo bar
```

closes VirtusLab#157
tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 4, 2021
Now it is poosible to run

```
echo "println(args)" > a.sc && scala-cli run ./a.sc -- foo bar
```

closes VirtusLab#157
tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 4, 2021
Now it is poosible to run

```
echo "println(args)" > a.sc && scala-cli run ./a.sc -- foo bar
```

closes VirtusLab#157
tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 4, 2021
Now it is poosible to run

```
echo "println(args)" > a.sc && scala-cli run ./a.sc -- foo bar
```

closes VirtusLab#157
tpasternak added a commit to tpasternak/scala-cli that referenced this issue Oct 4, 2021
Now it is poosible to run

```
echo "println(args)" > a.sc && scala-cli run ./a.sc -- foo bar
```

closes VirtusLab#157
@SethTisue
Copy link
Contributor Author

@tpasternak I'm having trouble combining this with also passing flags to scala-cli

for example, suppose I want to pass --scala-version 3.1.0

I tried:

#!/usr/bin/env scala-cli --scala-version 3.1.0                                                                          

println(args.size)
println(args.headOption)

and it aaaaaaalmost works right. I can do:

% ./foo.sc    
0
None

and I can do:

% ./foo.sc -- a b c
3
Some(a)

but note that I had to supply the extra -- to make it work. If don't supply that, the result is:

% ./foo.sc a b c   
a: not found
b: not found
c: not found

happy to open a new ticket on this if you'd prefer.

@tpasternak
Copy link
Contributor

Thank you @SethTisue , you're right, I'm reopening the issue

@tpasternak tpasternak reopened this Oct 21, 2021
@romanowski romanowski added the bug Something isn't working label Oct 25, 2021
@romanowski
Copy link
Member

@SethTisue We are thinking about how to improve the situation and to be honest I cannot see how we can get rid of -- from she-bangs without some strange heuristics that would cause harm in other areas (mainly because it is quite hard to detect reliably if the application is running from she-bang or using scala-cli directly).

Any ideas on how to improve the situation? I do not see any other option than just updating documentation.

@SethTisue
Copy link
Contributor Author

I haven't thought about this deeply, but I'll throw a couple of questions out there anyway:

would it resolve the ambiguity if we required script authors to include -- in their shebang lines, like this?

#!/usr/bin/env scala-cli --scala-version 3.1.0 --

to make it unambiguous that any arguments passed to the script itself would be interpreted as arguments to the script rather than as arguments to scala-cli itself.

because it is quite hard to detect reliably if the application is running from she-bang or using scala-cli directly

would it help to add a top-level command, called shebang or script or something?

#!/usr/bin/env scala-cli script

@tpasternak
Copy link
Contributor

would it resolve the ambiguity if we required script authors to include -- in their shebang lines, like this?

#!/usr/bin/env scala-cli --scala-version 3.1.0 --

This is not so easy. If you execute ./script.sc arg1 arg2, the result command will be /usr/bin/env scala-cli --scala-version 3.1.0 -- ./script.sc arg1 arg2. This means the ./script.sc will be passed down to the program. Of course we could treat the first argument after the -- specially, but then it would break non-shebang executions. Basically bash and python have it for free, because they accept interpreter arguments before the inputs and program arguments after the inputs. In scala-cli we support multiple inputs (should we?), so we cannot drop -- and take whatever comes after the input file.

would it help to add a top-level command, called shebang or script or something?

#!/usr/bin/env scala-cli script

I did something like this here #410. It's quite dirty, as cheats the caseapp library, but it works

@SethTisue
Copy link
Contributor Author

I did something like this here #410

offhand, looks fine to me. it's still fairly early days for this project, so I'll be content with anything that gets me unstuck, and that PR looks like it will.

@tpasternak
Copy link
Contributor

scala-cli shebang command added in #410

It will be available in the upcoming release

@tpasternak
Copy link
Contributor

Ok, scala-cli 0.0.9 is out. Your case will work if you use scala-cli shebang. Closing this now, please reopen again if there are any problems still

@SethTisue
Copy link
Contributor Author

tried it out but was blocked by #451

@SethTisue
Copy link
Contributor Author

SethTisue commented Dec 7, 2021

(#451 remains an unreproducible mystery, but:)

yay, this is now working for me for the community build scripts: scala/community-build#1518

👏

@tpasternak
Copy link
Contributor

Thank you @SethTisue !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants