我正在尝试创建一个函数来解析文本文件中的命令行参数。这意味着每个标志和值都需要作为一个数组中的单独项返回。如果行为空或以#,;或开头,则应忽略这些行]。
我当前的功能存在多个问题。首先,在 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
暮色呼如
随时随地看视频慕课网APP
相关分类