cargo-script-mvs icon indicating copy to clipboard operation
cargo-script-mvs copied to clipboard

Research how other tools handle scripts in the top-level command

Open epage opened this issue 2 years ago • 6 comments

Say we allow cargo in the #!, what inspiration can we take?

epage avatar Apr 19 '23 01:04 epage

dub: https://github.com/dlang/dub/blob/master/source/dub/commandline.d#L374,L432

  • Checks for - in the subcommand
  • Checks if subcommand ends with .d
  • Checks if subcommand exists
  • Check if <subcommand >.d exists (making the extension optional)

Key points

  • Specifying the extension when calling cargo is optional
  • Gives higher precedence to scripts than commands dub run could execute a script called run.d which would get cyclical)

Implementation note: dub just invokes argv[0] run ... to execute the script

epage avatar Apr 19 '23 01:04 epage

Did a first pass with Groovy but the code base was too enterprise-y for me to find: https://github.com/apache/groovy/

epage avatar Apr 19 '23 01:04 epage

If I'm understanding stack correctly, they fallback to running the subcommand as a script if the subcommand is not recognized.

See https://github.com/commercialhaskell/stack/blob/master/src/main/Stack/CLI.hs#L111,L117

epage avatar Apr 19 '23 01:04 epage

Didn't quite find it for elixir but my guess is its in or around https://github.com/elixir-lang/elixir/blob/59bf2247b4e0b9821ed5d56f91bc51bb2a7a5747/lib/elixir/lib/kernel/cli.ex

epage avatar Apr 19 '23 01:04 epage

(Hi. I came here via TWIR and the blog. I hope this is the right place to put this.)

I think the right answer is to require an option to say "this is a script invocation". That makes things unambiguous.

Prior art:

  • To write a script for jq, use the -f/--from-file option. (#!/usr/bin/jq -f) The default is to treat the argument as jq filter text (ie, source code in the jq language, rather than a filename). awk has similar behaviour.
  • To run a file containing Emacs Lisp as a script, use emacs --script. So #!/usr/bin/emacs -script. (The default behaviour is of course to run emacs as an interactive text editor and treat the argument as a file to edit.)
  • Debian's debian/rules is a Makefile. make's default behaviour is to treat the arguments as targets and look for a Makefile in the cwd (which is analogous to cargo's default behaviour). So, debian/rules starts #! /usr/bin/make -f.
  • To run a file containing a PostgreSQL script, use psql -f, since the arguments are the database name etc.
  • If you want to write a #! script for execution by gdb, you must write #!/usr/bin/gdb -x or similar. (By default the argument is the program to debug.)

Conflating the cargo subcommand namespace with the cargo script namespace is highly undesriable and I bet it would cause trouble. I think this should be sorted out before the feature is stabilised.

The precise option doesn't matter very much, but there should be a one-letter alias since #! lines sometimes have annoying length limits.

ijackson avatar May 15 '24 09:05 ijackson

Note that this was linked to as past research and this Issue has no bearing on the feature at this time. All development has shifted to RFCs and the Cargo repo.

epage avatar May 15 '24 12:05 epage