Coffeescript 如何从命令行将变量传递到脚本中?

Coffeescript 如何从命令行将变量传递到脚本中?,coffeescript,Coffeescript,假设我在命令行运行一个脚本: coffee ./scripts/doSomeStuff.coffee 其中doSomeStuff.coffee看起来像: numberOfTimes = ??? doStuff = (times) -> while times > 0 console.log('doing stuff') --times doStuff(numberOfTimes) 如何通过命令行传递执行任务的次数--eval似乎是显而易见的选择,

假设我在命令行运行一个脚本:

coffee ./scripts/doSomeStuff.coffee
其中doSomeStuff.coffee看起来像:

numberOfTimes = ??? 

doStuff = (times) -> 
  while times > 0 
    console.log('doing stuff') 
    --times 

doStuff(numberOfTimes)
如何通过命令行传递执行任务的次数
--eval
似乎是显而易见的选择,但添加
--eval='global.numberOfTimes=5'
没有帮助


我可以用bash的
export REPEAT\u TIMES=5
来完成,但这似乎有很多潜在的副作用

与使用node.js的方法相同,通过
process.argv

命令:

coffee ./scripts/doSomeStuff.coffee 5
咖啡脚本:

numberOfTimes = process.argv[2]
# index 0 is the interpreter: coffee
# index 1 is the file: ./scripts/doSomeStuff.coffee
# index 2 is the first argument: 5

还有大量的函数为解析
argv
提供了更好的接口。我玩得很开心。

与node.js一样,通过
process.argv

命令:

coffee ./scripts/doSomeStuff.coffee 5
咖啡脚本:

numberOfTimes = process.argv[2]
# index 0 is the interpreter: coffee
# index 1 is the file: ./scripts/doSomeStuff.coffee
# index 2 is the first argument: 5

还有大量的函数为解析
argv
提供了更好的接口。我玩得很开心。

请不要在别人的答案中插入代码。我不想让人觉得我推荐了你设置默认值的方法,因为这太复杂了。这应该非常简单:
numberOfTimes=process.argv[2]?1
你说得对,我还没有运行代码,忘记了更简单的方法。我认为添加这一点会使答案更加完整,所以我添加了它。请不要在其他人的答案中插入代码。我不想让人觉得我推荐了你设置默认值的方法,因为这太复杂了。这应该非常简单:
numberOfTimes=process.argv[2]?1
你说得对,我还没有运行代码,忘记了更简单的方法。我想加上它会得到一个更完整的答案,所以我加了它。