简体   繁体   English

如何编写此 cli 命令以使测试通过?

[英]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.

相关问题 如何在 deno 测试中通过命令行 arguments? - How to pass command line arguments in deno test? 如何通过命令行将选项传递给frisby测试 - How to pass an option in to the frisby test through command line 如何在Slimerjs中传递cli args? - How to pass cli args in Slimerjs? 如何使用 WebAssembly 和 JS 将命令行 arguments 传递给 C 代码? - How to pass command line arguments to C code with WebAssembly and JS? 如何将变量定义的某些命令传递给夜视仪中的其他外部命令(在测试中) - How to pass on variable defined certain command, into some other external command(in the test) in nightwatch 如何使用 npm 在命令行中传递参数并在反应 javascript 代码中读取它 - How to pass argument in command line with npm and read it in react javascript code 如何通过CLI将十进制传递给CasperJS? - how to pass decimal to CasperJS via CLI? 7z cli - 如何传递带有空格的密码? - 7z cli - how to pass a password with whitespaces? 如何测试 Quasar(作为 Vue CLI 插件)? - How to test Quasar (as Vue CLI plugin)? 如何通过 CLI 自定义命令运行节点脚本? - How to run a node script by CLI custom command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM