繁体   English   中英

在node.js命令行中解析包含正斜杠的字符串

[英]Parsing of strings containing forward slash in node.js command line

嗨,我正在编写一个节点js应用程序,我需要在其中通过命令行将路径传递给某些文件。 我需要这样做以进行配置。 我知道我可以将所有配置详细信息放在json文件中,然后将其加载到应用程序中。 但这是一个特定的要求。

这是我的代码

app.js

  ----
  -----
  console.log(process.argv); 
  ---
  --
 // Start server

现在,当我在节点中以以下方式运行此文件时:

 node app.js hii

输出量

 'hii'

但是如果我这样做

node app.js '/samplePath'

我在DOS中得到以下输出:

'\'/samplePath\''

我在Git Bash中得到以下输出:

'C:/Program Files/Git/samplePath'

我如何只获得“ / samplePath”作为输出? 我究竟做错了什么? 任何帮助,将不胜感激。

为了得到最后的项目,尝试

 process.argv[2].split(/\//).pop();

您将必须自行分析命令行选项。 但是,您可以研究此节点模块,它将使您轻松完成这一工作。

npm install commander

这是他们的Github页面上的示例

var program = require('commander');

program
  .version('0.0.1')
  .option('-p, --peppers', 'Add peppers')
  .option('-P, --pineapple', 'Add pineapple')
  .option('-b, --bbq', 'Add bbq sauce')
  .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
  .parse(process.argv);

暂无
暂无

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

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