node-webkit如何解析'事件参数

node-webkit How to parse 'open' event parameter?

本文关键字:事件 参数 何解析 node-webkit      更新时间:2023-09-26

我需要一个包含所有参数的数组,比如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标志),这将有效。但如果他们这样做,我会考虑使用进程。也许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。