【问题标题】:node-webkit How to parse 'open' event parameter?node-webkit 如何解析“打开”事件参数?
【发布时间】:2016-05-12 18:39:06
【问题描述】:

我需要一个包含所有参数的数组,例如 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"
});

【问题讨论】:

    标签: javascript node.js command-line-arguments node-webkit


    【解决方案1】:

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

    // 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 来做这件事

    【讨论】:

      【解决方案2】:

      我的解决方案:
      1.将引号中的所有空格替换为somethink like " " (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 上工作

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 2014-09-07
        • 1970-01-01
        • 2014-09-13
        • 2015-04-29
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        相关资源
        最近更新 更多