nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

Running from command line/script runner #2206

Closed ioquatix closed 9 years ago

ioquatix commented 9 years ago

Hi,

I've had this issue come in: https://github.com/ioquatix/script-runner/issues/20#issuecomment-75649211

Just wondering, is it possible to run a nim script using a hash bang line, e.g.

#!/usr/bin/env nim

Can you accept the script from stdin somehow for running unsaved files?

Sorry for the bother and thanks for your advice, assistance.

def- commented 9 years ago

Just wondering, is it possible to run a nim script using a hash bang line, e.g.

Yes: http://hookrace.net/blog/what-makes-nim-practical/#use-as-a-scripting-language

Can you accept the script from stdin somehow for running unsaved files?

I made a PR for this just yesterday: https://github.com/Araq/Nim/pull/2202

ioquatix commented 9 years ago

Is nimscript bundled by default?

def- commented 9 years ago

No.

ioquatix commented 9 years ago

What is the preferred implementation in script-runner?

1/ Bundle nimscript and use it. 2/ Execute something similar to what nimscript does.

The option of having nimscript included in nim makes life a lot easier since it can run the script outside the script-runner environment.

def- commented 9 years ago

I guess just running this would work: nim --verbosity:0 -r c $FILE

ioquatix commented 9 years ago

@def- I guess the big issue with that is people would have to type that into every script.. either that or it gets hard coded into script-runner in which case it might become a compatibility issue in the future if the flags change.

ioquatix commented 9 years ago

If no file is specified can you default to stdin? This is how almost every other script interpreter works.

Araq commented 9 years ago

Stdin support has been merged.

ioquatix commented 9 years ago

@Araq looking to implement this now, can you please confirm how to run this at the top of a script file, e.g.

#!/usr/bin/env nim --verbosity:0 -r c
reactormonk commented 9 years ago

I can't find a correct shebang line to make it work. I think it's a problem with the argument parsing and/or how the arguments are passed to the invoked compiler.

ioquatix commented 9 years ago

Okay, well, essentially, what we want is something that can run on the hash bang line, it looks like nimscript might have cracked that nut, but perhaps it's good if this functionality is available directly from nim - once you let me know I will integrate support into script-runner.

refi64 commented 9 years ago

@ioquatix That won't work: on most systems (including Linux), /usr/bin/env will not pass any command line arguments to the program. In your example, /usr/bin/env will be looking for an executable named nim --verbosity:0 -r -c.

reactormonk commented 9 years ago

The passing works just fine on arch. I get

Error: invalid command: '--verbosity:0 -r c'

When enclosing the arguments via ", I get

Error: arguments can only be given if the '--run' option is selected
ioquatix commented 9 years ago

@kirbyfan64 I have never seen the behaviour your proposing. In fact, I can write an entire program after /usr/bin/env with arguments if I like:

screen shot 2015-04-22 at 10 47 01 am

From the env man page:

SYNOPSIS
     env [-i] [name=value ...] [utility [argument ...]]

The only limitation is programs with = in their name as this looks like an environment option.

refi64 commented 9 years ago

@ioquatix What version of UNIX are you using? On Linux:

ryan@DevPC-LX:~/langtest$ ./x.rb
/usr/bin/env: ruby -e "puts ARGV.inspect" --: No such file or directory
ryan@DevPC-LX:~/langtest$ 
ioquatix commented 9 years ago

I'm on Mac OS X which is BSD derived.

ioquatix commented 9 years ago

On linux:

% /usr/bin/env ruby -e "puts ARGV.inspect" -- Apples Oranges Bananas
["Apples", "Oranges", "Bananas"]

Is it different when you run it in a file?

ioquatix commented 9 years ago

Weird, when it's in a file, it doesn't work correctly, on both Mac and Linux.

ioquatix commented 9 years ago

Interesting, as soon as you have quotes, it fails.

ioquatix commented 9 years ago

Perhaps bundling nimscript makes sense given the complexity of using env with arguments.

ioquatix commented 9 years ago

Just touching base, any progress here?

def- commented 9 years ago

I would recommend to use https://github.com/flaviut/nimrun

ioquatix commented 9 years ago

Okay right, so basically this isn't something we really need to add explicit support to script-runner then. Anyone can just add this to their path and use it. Thanks.