I have my_func.coffee file
first = (test) ->
console.log 'first' + test
second = (test) ->
console.log 'second' + test
How can I call ‘second’ method from command line ?
It would be nice to have something like:
coffee my_func.coffee -e "second('test')"
You are probably mixing up a couple of things here. Coffeescript is the compiler, that transforms coffeescript to javascript. The eval option to coffeescript tells it to read source code to be compiled from the command line, not to execute it.
To execute the code, you need to run it in some kind of javascript environment. Node.js is a popular choice. So first, create your coffeescript module, call it
MyMod.coffelike this:There is some extra code to be able to export that code as a javascript module. Compile this using
coffee -c MyMod.coffee.Then you can execute it from the command line with for instance:
and you should get this:
Updated:
Seems you can run the coffeescript directly. If you remove the compiled
MyMod.jsfile so you only have the MyMod.coffee lying around, you can do:to get the same output. That assumes coffee-script module is installed of course.
Update:
You can also run the
coffeeexecutable directly: