Crystal: [RFC] Allow only one target file in the command line

Created on 7 Apr 2016  路  6Comments  路  Source: crystal-lang/crystal

Right now this is possible:

# make a program out of foo.cr and bar.cr, compile it and run it
crystal foo.cr bar.cr

As well as this:

# make a program out of foo.cr and bar.cr, and compile it
crystal build foo.cr bar.cr

And of course replacing build with other commands (for example run).

I don't remember why we have this behaviour. I believe it was to be able to run several spec files:

crystal spec/file1.cr spec/file2.cr

However, now we have a spec command that allows that, and even allows specifying an optional target line for each file:

crystal spec spec/file1.cr spec/file2.cr:10 spec/file3.cr:20

What if we only allow multiple files in that spec command but no in other commands? This will let us do things like:

# compile and run foo.cr with ARGV = ["1", "2", "3"]
crystal foo.cr 1 2 3

Right now one has to do:

crystal foo.cr -- 1 2 3

which isn't terrible, but the usual shebang line doesn't work (we have #546 as an issue). With this change it will work.

Pros:

  • Less typing to send ARGV arguments (no need to type and remember to use --). This mostly affects people new to the language.
  • Shebang works

Cons:

  • Can't compile multiple files at once. But is anyone using this? I'm not. For an app the usual thing to do is crystal src/main.cr or similar. In any case the workaround is to require multiple files in a single file.
draft

Most helpful comment

We shouldn't change the behavior of -- which is quite standard: pass arguments to the underlying process. That would only add some confusion.

I'd like to avoid a rack/testunit like hack that needs an intermediary ruby file to load many source files at once. I also need to pass arguments (like --verbose, --parallel=4 or -n /regex/ to match only some tests). I like that Crystal allows that by default :-)

Maybe have crystal run keep accepting many files? But crystal file.cr only accept one? I believe they're already handled differently in source code.

All 6 comments

I personally don't use it, find it very confusing that there are multiple possible entry points and the order they load might affect main.

In my case, for every program/executable that I invoke or compile, each have their own entry point (src/cli/server.cr).

I think the Pro list is interesting, but would love to hear folks that are using the other approach.

I'm using this feature. All the time. Hint: minitest. Check the Makefile of all my projects :D

@ysbaddaden I see, but minitest is also a test framework so it's similar to crystal spec. In fact, if you run crystal spec it will basically do something like crystal run spec/**/*_spec.cr, so you could use that. The only problem is that minitest uses test while spec uses spec. We could change crystal spec to do the equivalent of crystal run spec/**/*_spec.cr if it finds a test directory, and also add crystal test to do the same, though it might be maybe weird to add such commands for a test library (but I think test and spec are the most common words for this, so maybe it's not that bad).

I'm thinking... what if we invert the meaning of --? Arguments after -- are more files to be compiled. In this way the common case of passing arguments, and the shebang, are handled well by default. And for minitest you'd use --.

We shouldn't change the behavior of -- which is quite standard: pass arguments to the underlying process. That would only add some confusion.

I'd like to avoid a rack/testunit like hack that needs an intermediary ruby file to load many source files at once. I also need to pass arguments (like --verbose, --parallel=4 or -n /regex/ to match only some tests). I like that Crystal allows that by default :-)

Maybe have crystal run keep accepting many files? But crystal file.cr only accept one? I believe they're already handled differently in source code.

I like this:

Maybe have crystal run keep accepting many files?

Shebangs keeps being #!/usr/bin/env/crystal but works, and using crystal run you can compile and run multiple files (as well as when doing crystal build and other commands).

Personally I never pass more than one file.
Getting shebang to work naturally is prio one, so the run takes many, implicit only one sounds gooood :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

jhass picture jhass  路  3Comments

lgphp picture lgphp  路  3Comments

nabeelomer picture nabeelomer  路  3Comments

ArthurZ picture ArthurZ  路  3Comments