1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #!/usr/bin/env node
const commander = require('commander'); const program = new commander.Command();
const pkg = require('../package.json');
program .storeOptionsAsProperties() .name(Object.keys(pkg.bin)[0]) .usage('<command> [option]') .version(pkg.version) .option('-d, --debug', '开启调试模式', false) .option('-e, --envName <envName>', '输出环境变量名称');
const clone = program.command('clone <source> [destination]'); clone .description('clone a repository') .option('-f, --force', '是否强制克隆') .action((source, destination, cmdObj) => { console.log('clone command called', source, destination, cmdObj.force); });
const service = new commander.Command('service');
service .command('start [port]') .description('service start at same port') .action((port) => { console.log('service starting at ', port); });
service .command('stop') .description('stop service') .action(() => { console.log('service stop'); });
program.addCommand(service);
program .command('install [name]', 'install package', { executableFile: 'zjw-cli', hidden: true }) .alias('i')
program .arguments('<username> [password]') .description('test command', { username: 'user to login', password: 'password for user, if required' }) .action((username, password) => { console.log(username, password); });
program.helpInformation = function() { return 'output your help information\n'; }
program.on('option:debug', () => { console.log('debug: ', program.debug) process.env.LOG_LEVEL = 'verbose' })
program.on('command:*', (obj) => { console.log(obj); console.error('未知的命令:', obj[0]); const availableCommands = program.commands.map(cmd => cmd.name()); console.log('可用命令:', availableCommands.join(', ')) })
program.parse(process.argv);
|