[英]How to code this cli command such that the test pass?
What should I write in task.js
such that this test in task.test.js
pass?我应该在task.test.js
中写什么以task.js
中的这个测试通过?
const fs = require("fs");
const { execSync } = require("child_process");
let deleteFile = (path) => {
try {
fs.unlinkSync(path);
} catch (err) {}
};
beforeEach(() => {
deleteFile(`${__dirname}/task.txt`);
deleteFile(`${__dirname}/completed.txt`);
});
let tasksTxtCli = (...args) => [`${__dirname}/task.sh`, ...args].join(" ");
let usage = `Usage :-
$ ./task add 2 hello world # Add a new item with priority 2 and text "hello world" to the list
$ ./task ls # Show incomplete priority list items sorted by priority in ascending order
$ ./task del INDEX # Delete the incomplete item with the given index
$ ./task done INDEX # Mark the incomplete item with the given index as complete
$ ./task help # Show usage
$ ./task report # Statistics`;
test("prints help when no additional args are provided", () => {
let received = execSync(tasksTxtCli()).toString("utf8");
expect(received).toEqual(expect.stringContaining(usage));
});
When I type ./task help
in command line I have to display text in usage
on console.当我在命令行中键入./task help
时,我必须在控制台上显示usage
中的文本。 Currently task.js
is blank.目前task.js
是空白的。 If anyone could guide me it would be a great help.如果有人可以指导我,那将是一个很大的帮助。
First you have to parse the command line input and for that you have to use process.argv() method.首先,您必须解析命令行输入,为此您必须使用 process.argv() 方法。
const arguments = process.argv.slice(2); // delete first two arguments as the..
// first one is the process execution path
//second one is the path for the js file
console.log(arguments[0]);
Now run "./task help" on the shell you will see the output 'help' Using above you can parse your cli input.现在在 shell 上运行“./task help”,您将看到 output 'help' 使用上面的内容,您可以解析 cli 输入。 After that you can use if else or switch case statements for various inputs and executes them.之后,您可以对各种输入使用 if else 或 switch case 语句并执行它们。 For eg例如
if(arguments[0] === "help") {
const help = `Usage :-
$ ./task add 2 hello world # Add a new item with priority 2 and text "hello world" to the list
$ ./task ls # Show incomplete priority list items sorted by priority in ascending order
$ ./task del INDEX # Delete the incomplete item with the given index
$ ./task done INDEX # Mark the incomplete item with the given index as complete
$ ./task help # Show usage
$ ./task report # Statistics`;
console.log(help);
}
// like that you can do for others
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.