import { program, Command } from 'commander'; import fs from 'fs'; // 将多个子命令加入主程序中 let version = '0.0.1'; try { // @ts-ignore if (ENVISION_VERSION) version = ENVISION_VERSION; } catch (e) {} // @ts-ignore program.name('app').description('A CLI tool with envison').version(version, '-v, --version'); const ls = new Command('ls').description('List files in the current directory').action(() => { console.log('List files'); console.log(fs.readdirSync(process.cwd())); }); program.addCommand(ls); export { program, Command }; /** * 在命令行中运行程序 * @param args */ export const runProgram = (args: string[]) => { const [_app, _command] = process.argv; program.parse([_app, _command, ...args]); };