从 config txt 文件解析命令行参数

我正在尝试创建一个函数来解析文本文件中的命令行参数。这意味着每个标志和值都需要作为一个数组中的单独项返回。如果行为空或以#,;或开头,则应忽略这些行]。


我当前的功能存在多个问题。首先,在 reduce 函数中拆分数组不会像使用 push 那样将数组添加到累加器,而是将一个新数组添加到累加器。其次,引号内的字符串可以拆分成数组,即使它们应该被视为单个参数。


const argsFile = `

# Command line arguments

--download example.com

--pass


--no-fail

--output "C:\\Users\\User\\Desktop\\New Folder"


--binary-location 'C:\\Users\\Desktop\\New Folder\\executable program.exe'

`;


let parsedArguments = argsFile.split(/\r?\n/)

    .filter(argument => (!argument.startsWith('#') && !argument.startsWith(';') && !argument.startsWith(']')))

    .reduce((a, c) => [...a, c.split(' ')])

    .filter(argument => argument !== '');

    

console.dir(parsedArguments)


这是我的函数所需的输出:


[

    "--download",

    "example.com",

    "--pass",

    "--no-fail",

    "--output",

    "C:\\Users\\User\\Desktop\\New Folder",

    "--binary-location",

    "C:\\Users\\Desktop\\New Folder\\executable program.exe"

]

如何修改我的函数以获得所需的输出?如果有一个图书馆可以处理这种情况,我还没有找到。


慕姐4208626
浏览 212回答 1
1回答

暮色呼如

Yargs 似乎可以非常可靠地解析字符串的参数,而且它是相当可配置的。我想出了以下内容,它似乎产生了您想要的输出。但是,没有用任何其他字符串对其进行测试:const parse = require("yargs-parser");const argsFile = `# Command line arguments--download example.com--pass--no-fail--output "C:\\Users\\User\\Desktop\\New Folder"--binary-location 'C:\\Users\\Desktop\\New Folder\\executable program.exe'`;let parsedArguments = argsFile&nbsp; .split(/\r?\n/)&nbsp; .filter(&nbsp; &nbsp; (argument) =>&nbsp; &nbsp; &nbsp; !argument.startsWith("#") &&&nbsp; &nbsp; &nbsp; !argument.startsWith(";") &&&nbsp; &nbsp; &nbsp; !argument.startsWith("]")&nbsp; )&nbsp; .map((line) => {&nbsp; &nbsp; return parse(line, {&nbsp; &nbsp; &nbsp; string: true,&nbsp; &nbsp; &nbsp; configuration: {&nbsp; &nbsp; &nbsp; &nbsp; "boolean-negation": false,&nbsp; &nbsp; &nbsp; &nbsp; "camel-case-expansion": false,&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; });&nbsp; })&nbsp; .map((ar) => {&nbsp; &nbsp; delete ar._;&nbsp; &nbsp; let properties = Object.keys(ar);&nbsp; &nbsp; if (properties.length == 0) return [];&nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; "--" + properties[0],&nbsp; &nbsp; &nbsp; typeof ar[properties[0]] == "boolean" ? "" : ar[properties[0]],&nbsp; &nbsp; ];&nbsp; })&nbsp; .filter((argument) => argument.length != 0);let flatArgs = [].concat.apply([], parsedArguments).filter((i) => i != "");console.dir(flatArgs);产生以下结果:[ '--download',&nbsp; 'example.com',&nbsp; '--pass',&nbsp; '--no-fail',&nbsp; '--output',&nbsp; 'C:\\Users\\User\\Desktop\\New Folder',&nbsp; '--binary-location',&nbsp; 'C:\\Users\\Desktop\\New Folder\\executable program.exe' ]Yargs 解析器针对您的特定要求“过于积极地”解析字符串,这就是为什么我们必须通过有点反转解析器所做的映射来进行映射(以“--”开头,忽略布尔值等)。然后,最后,我们必须“展平”数组,因为每一行都被解析到它自己的数组中。编辑:因此,如果我们也必须处理短参数,那么 yargs 将不适合,因为我们在解析后无法访问原始字符串。我们可以使用 yarg 的内部函数来标记字符串,但是(我只需要将它转换为 js):function tokenizeArgString(argString) {&nbsp; &nbsp; if (Array.isArray(argString)) {&nbsp; &nbsp; &nbsp; &nbsp; return argString.map(e => typeof e !== 'string' ? e + '' : e);&nbsp; &nbsp; }&nbsp; &nbsp; argString = argString.trim();&nbsp; &nbsp; let i = 0;&nbsp; &nbsp; let prevC = null;&nbsp; &nbsp; let c = null;&nbsp; &nbsp; let opening = null;&nbsp; &nbsp; const args = [];&nbsp; &nbsp; for (let ii = 0; ii < argString.length; ii++) {&nbsp; &nbsp; &nbsp; &nbsp; prevC = c;&nbsp; &nbsp; &nbsp; &nbsp; c = argString.charAt(ii);&nbsp; &nbsp; &nbsp; &nbsp; // split on spaces unless we're in quotes.&nbsp; &nbsp; &nbsp; &nbsp; if (c === ' ' && !opening) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!(prevC === ' ')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // don't split the string if we're in matching&nbsp; &nbsp; &nbsp; &nbsp; // opening or closing single and double quotes.&nbsp; &nbsp; &nbsp; &nbsp; if (c === opening) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opening = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if ((c === "'" || c === '"') && !opening) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; opening = c;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!args[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; args[i] = '';&nbsp; &nbsp; &nbsp; &nbsp; args[i] += c;&nbsp; &nbsp; }&nbsp; &nbsp; return args;}const argsFile = `# Command line arguments--download example.com--pass--no-fail--output "C:\\Users\\User\\Desktop\\New Folder"-a test--binary-location 'C:\\Users\\Desktop\\New Folder\\executable program.exe'`;let parsedArguments = argsFile.split(/\r?\n/)&nbsp; &nbsp; .filter(argument => (!argument.startsWith('#') && !argument.startsWith(';') && !argument.startsWith(']')))&nbsp; &nbsp; .map(line => tokenizeArgString(line))&nbsp; &nbsp; .filter(argument => argument.length != 0);let flatArgsNoQuotes = [].concat.apply([], parsedArguments).map(args => args.replace(/['"]+/g, '')).filter(i => i != "");&nbsp; &nbsp;&nbsp;console.dir(flatArgsNoQuotes)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript