【问题标题】:How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?如何通过终端或其他方式将参数传递给 JavaScriptCore/Console?
【发布时间】:2011-08-03 03:06:34
【问题描述】:

JSC seems like the simpler-and-easier,更便携,准通用安装 - 世界上 node.js 的明显替代品......我已经能够弄清楚基础知识,但几乎有 nada 漂浮在那里它(为什么?),所以这是一个简单的问题,我希望有人能澄清..

我在一个 .js 文件中有一个不错的 littlejavascript“类”,它的开头是这样的...BTW, it takes a Hex Color code and spits out a "named" color. Neat. 示例用法:

<script type="text/javascript" src="ntc.js"></script>
<script type="text/javascript">
var n_match  = ntc.name("#6195ED");
    n_rgb        = n_match[0]; // This is the RGB value of the closest matching color
    n_name       = n_match[1]; // This is the text string for the name of the match
    n_exactmatch = n_match[2]; // True if exact color match, False if close-match
alert(n_match);
</script>

代码以这样的方式开始,以这样的方式结束......

var ntc = {
     init: function() {
     var color, rgb, hsl;
  ⤹
}
ntc.init();

我可以毫不费力地在本文档的 BOTTOM 处硬编码一些值,就像这样...

var n_match = ntc.name("#000000");
print(n_match);

并从终端简单轻松地运行代码...

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -d ntc.js ↩

#000000,Black,true

但是,对于我的生活,我无法弄清楚如何通过这个傻瓜一些变量..

就像,我只想收到来自204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020 的回电 但似乎无法扭动它的手臂。-e 选项看起来很有希望,但无济于事。

这些年来,javascript 已经变异了许多奇怪的小众任务来处理,将它们交给这个家伙会很棒。比喻特洛伊木马可能已经安装了,甚至可以在潜在的客户端机器上运行。尽管这件事无处不在,但文档与史蒂夫鲍尔默粉丝俱乐部会议的出席人数一样稀少......

也就是说,大声笑,关于这个 JSC 的唯一半有用的 sn-ps 信息之一来自 7 年前一位 MS 员工的帖子,这表明......标题“Commandline.js

import System;
// This function reads the commandline arguments and prints them
function PrintCommandLineArguments() {
    var args:String[] = System.Environment.GetCommandLineArgs();
    var iValue:int;
// Print the command-line arguments    
for (iValue in args)
    Console.WriteLine(args[iValue]);
}
PrintCommandLineArguments(); 

我无法让它工作,但必须有办法,聪明的裤子......哦,坦率地说,这只会让我更加困惑,因为嘴里的泡沫已经占据了所有服务器- 最近的 JS 人,因为这件事绝对是老新闻了……为什么这个运行时环境会被当前的热门话题解决方案所取代呢? JSC很烂吗?提示我,姐妹女朋友。 ∀Ⓛ∃✖

【问题讨论】:

  • 哦,顺便说一句,#462425,Crater Brown,true #465945,Gray Asparagus,true #4682B4,Steel Blue,true #480404,Rustic Red,true #480607,Bulgarian Rose,true #480656,Clairvoyant,true。灰色的芦笋听起来很恶心……但对于热门答案来说,这是一件大事。

标签: php javascript bash command-line webkit


【解决方案1】:

以下似乎有效:

# using Bash on Mac OS X 10.6.7
sudo ln -is /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin

# simple example to print passed arguments
jsc --help   # Usage: jsc [options] [files] [-- arguments]
jsc <(echo 'print(arguments[0]); print(arguments);') -- one two three


# http://chir.ag/projects/ntc/
curl -L -O http://chir.ag/projects/ntc/ntc.js
echo '
for(var i in arguments) {
   var n_match = ntc.name(arguments[i]);
   print(n_match);
}
' >> ntc.js

jsc ntc.js -- 204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 \
              404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 \
              a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020


# For more information on using javascript from the command line see, for example: 
# - http://www.phpied.com/javascript-shell-scripting/
# - http://littlecomputerscientist.wordpress.com/2008/12/19/command-line-scripting-with-javascript/
# - http://littlecomputerscientist.wordpress.com/2008/12/20/improving-spidermonkeys-load-for-command-line-javascript/

【讨论】:

  • 哇,很好的第一个答案......它工作得很好。我要添加的唯一一件事是为了澄清......你想保存colors=jsc ntc.js $arguments``然后echo $colors来正确地得到你的输出......在个人笔记上......你是怎么想到的出去?它当然没有在页面等中说明,出于某种原因,js 的语法一有机会就让我感到困惑。即使看着这个漂亮的、有效的解决方案,我也不明白,哈哈。有没有一些你可以推荐的精神范式、非法药物或小册子让这种事情“发生在你身上”?
猜你喜欢
  • 2017-07-18
  • 1970-01-01
  • 2021-07-13
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多