node.js - 使用 NodeJS 创建 CLI - 将变量传递给 exec linux 命令

标签 node.js linux command-line-interface child-process node-commander

我正在尝试使用 nodejs 和 commander.js 包创建一个简单的 CLI。

CLI 的目的是 touch 一个新文件

const program = require('commander');
const exec    = require('child_process').exec;

program.version('0.0.1')
       .description('Command Line Interface (CLI)');

program.command('make:controller <name>')
       .description('Add a new controller called <name>')
       .action(function (name) {
           exec("touch name");
       });

program.parse(process.argv);

命令:make:controller NewController

Linux:触摸新 Controller

我如何将名称变量传递给 exec() 命令。


P.S(创建新文件后我还想往里面写点东西)

最佳答案

const program = require('commander');
const exec    = require('child_process').exec;

program.version('0.0.1')
       .description('Command Line Interface (CLI)');

program.command('make:controller <name>')
       .description('Add a new controller called <name>')
       .action(function (name) {
           exec("touch " + name);//mind the space after touch
       });

program.parse(process.argv);

关于node.js - 使用 NodeJS 创建 CLI - 将变量传递给 exec linux 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139966/

相关文章:

node.js - 为什么只有当我尝试使用 find 函数时,我的 Sequelize 模型才未定义?

node.js - 如何通过 webpack 导入 jQuery 插件并将其放在 $/jQuery 下? (ES6 + typescript )

linux - alpine docker 镜像和busybox docker 镜像有什么区别?

c - 检查内核模块中的 '.read' 函数时出错

ffmpeg - 如何使用 ffmpeg 在横向模式下制作我的视频

javascript - 使用 NodeJS 设置环境变量

javascript - 使用 UTF-8 时 JSON 通过流识别为无效

javascript - 格式化 Twitter API 日期在 Node.js 后端使用 moment.js

linux - 如何检测当前进程中动态库(共享对象)的大小?

graphql - 为什么Apollo Client下载:schema fail with "Error: Cannot find module ' graphql/validation/rules/UniqueTypeNames'"