繁体   English   中英

node-webkit如何解析“打开”事件参数?

[英]node-webkit How to parse 'open' event parameter?

我需要一个包含所有参数的数组,例如gui.App.argv中的值
是否包含一些函数来解析此内容?

function openfile(cmdline){
    console.log('command line: ' + cmdline);
}
openfile(gui.App.argv); //my file.txt, my file.txt  (this is what I need)
gui.App.on('open', function(cmdline) {
    openfile(cmdline);  //app.exe --original-process-start-time=13049249391168190 "my file.txt" "my file2.txt"
});

我只是遇到了同样的问题,这是我如何解决的问题:

// Listen to `open` event
gui.App.on('open', function (cmdline) {
   // Break out the params from the command line
   var arr = /(.*)--original-process-start-time\=(\d+)(.*)/.exec(cmdline);

   // Get the last match and split on spaces
   var params = arr.pop().split(' ');

   console.log('Array of parameters', params);
});

只要他们不更改事件的输出结构(即--original-process-start-time标志),这就会起作用,但是如果他们这样做的话,我会考虑使用process.execPath这样做

我的解决方案:
1.用诸如“ ”之类的符号替换引号中的所有空格。 (HTML空间)。
2.用空格分隔字符串。
3.替换为“ ” 通过空间。

gui.App.on('open', function(cmdline) {
    cmdline = cmdline.replace(/"([^"]+)"/g, function(a) {
        return a.replace(/\s/g, " "); 
    }).split(' ');
    for (var i = 0, length = cmdline.length, arg = '', args = []; i < length; ++i) {
        arg = cmdline[i].replace(/&nbsp;/g, ' ');
        // Filter by exe file and exe args.
        if (arg === '"' + process.execPath + '"' || arg.search(/^\-\-/) === 0) continue;
        args.push(arg);
    }
    console.log(args);
});

使用0.14.x,0.15.x。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM