it('should fail in action as port is invalid', done => { commander.loadDefinitions(testDefinitions); expect(() => { commander.parse(['node', './CLI.spec.js', '--arg0', 'arg0Value'], { PROGRAM_ARG_0: 'arg0ENVValue', PROGRAM_ARG_1: 'arg1ENVValue', PROGRAM_ARG_2: 'hello', }); }).toThrow('arg2 is invalid'); done(); });
export default function({ definitions, help, usage, start }) { program.loadDefinitions(definitions); if (usage) { program.usage(usage); } if (help) { program.on('--help', help); } program.parse(process.argv, process.env); const options = program.getOptions(); start(program, options, function() { logStartupOptions(options); }); }
it('should load properly definitions from env', done => { commander.loadDefinitions(testDefinitions); commander.parse([], { PROGRAM_ARG_0: 'arg0ENVValue', PROGRAM_ARG_1: 'arg1ENVValue', PROGRAM_ARG_2: '3', }); expect(commander.arg0).toEqual('arg0ENVValue'); expect(commander.arg1).toEqual('arg1ENVValue'); expect(commander.arg2).toEqual(3); expect(commander.arg4).toEqual('arg4Value'); done(); });
it('should load config from apps', done => { spyOn(console, 'log').and.callFake(() => {}); commander.loadDefinitions(testDefinitions); commander.parse([ 'node', './CLI.spec.js', './spec/configs/CLIConfigApps.json', ]); const options = commander.getOptions(); expect(options.arg1).toBe('my_app'); expect(options.arg2).toBe(8888); expect(options.arg3).toBe('hello'); //config value expect(options.arg4).toBe('/1'); done(); });
it('should not override config.json', done => { spyOn(console, 'log').and.callFake(() => {}); commander.loadDefinitions(testDefinitions); commander.parse( [ 'node', './CLI.spec.js', '--arg0', 'arg0Value', './spec/configs/CLIConfig.json', ], { PROGRAM_ARG_0: 'arg0ENVValue', PROGRAM_ARG_1: 'arg1ENVValue', } ); const options = commander.getOptions(); expect(options.arg2).toBe(8888); expect(options.arg3).toBe('hello'); //config value expect(options.arg4).toBe('/1'); done(); });
it('should load properly definitions from args', done => { commander.loadDefinitions(testDefinitions); commander.parse([ 'node', './CLI.spec.js', '--arg0', 'arg0Value', '--arg1', 'arg1Value', '--arg2', '2', '--arg3', 'some', ]); expect(commander.arg0).toEqual('arg0Value'); expect(commander.arg1).toEqual('arg1Value'); expect(commander.arg2).toEqual(2); expect(commander.arg3).toEqual('some'); expect(commander.arg4).toEqual('arg4Value'); done(); });
function main() { cli.version(version).option('-s, --socks [socks]', 'specify your socks proxy host, default: 127.0.0.1:1080').option('-p, --port [port]', 'specify the listening port of http proxy server, default: 8080').option('-l, --host [host]', 'specify the listening host of http proxy server, default: 127.0.0.1').option('-c, --config [config]', 'read configs from file in json format').option('--level [level]', 'log level, vals: info, error').parse(process.argv); var options = getOptionsArgs(cli); var fileConfig = null; if (options.config) { fileConfig = getFileConfig(options.config); } Object.assign(options, fileConfig); createServer(options); }
program .version(version) .usage('task [options]') .arguments('<task>') .option('-s, --stages <stages>', 'Specify stages you want to run', function(value) { return value.split(','); }) .action(function (task) { taskName = task; }) .parse(process.argv);
program .version(thancPkg.version) .usage('[options] <project_path>') .option('--me', 'thank thanc package and all of its dependencies') .option('-u, --username <username>', 'your Github username') .option('-p, --password <password>', 'your Github password') .option('-t, --token <password>', 'your Github token') .option('-q, --quite', 'Show only the progress bar instead of the repos list') .arguments('<path>') .action(path => projectPath = path ? path : projectPath) .parse(process.argv)
function parseArgs() { program .option('--scenario <value>', 'path to scenario relative to scenario runner') .option('--cycles [value]', 'number of cycles', parseInt, 10) .option('--workers [value]', 'number of workers', parseInt, 10) .parse(process.argv); assert(program.scenario, 'path to scenario should be provided'); console.log('configuration:'); console.log('- scenario', program.scenario); console.log('- cycles', program.cycles); console.log('- workers', program.workers); }
program .version(packageInfo.version) .arguments('<swaggerFile>') .action((swaggerFilePath) => { swaggerFile = path.resolve(swaggerFilePath); }) .option('-b, --handlebars <helperFilePath>', 'path to external handlebars helpers file (defaults to empty)', parseOutput) .option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd()) .option('-t, --templates <templateDir>', 'directory where templates are located (defaults to internal nodejs templates)') .parse(process.argv);
it('should fail when too many apps are set', done => { commander.loadDefinitions(testDefinitions); expect(() => { commander.parse([ 'node', './CLI.spec.js', './spec/configs/CLIConfigFailTooManyApps.json', ]); }).toThrow('Multiple apps are not supported'); done(); });
it('should fail when passing an invalid arguement', done => { commander.loadDefinitions(testDefinitions); expect(() => { commander.parse([ 'node', './CLI.spec.js', './spec/configs/CLIConfigUnknownArg.json', ]); }).toThrow('error: unknown option myArg'); done(); });
it('should load properly use args over env', done => { commander.loadDefinitions(testDefinitions); commander.parse( ['node', './CLI.spec.js', '--arg0', 'arg0Value', '--arg4', ''], { PROGRAM_ARG_0: 'arg0ENVValue', PROGRAM_ARG_1: 'arg1ENVValue', PROGRAM_ARG_2: '4', PROGRAM_ARG_4: 'arg4ENVValue', } ); expect(commander.arg0).toEqual('arg0Value'); expect(commander.arg1).toEqual('arg1ENVValue'); expect(commander.arg2).toEqual(4); expect(commander.arg4).toEqual(''); done(); });
it('should fail with invalid values in JSON', done => { commander.loadDefinitions(testDefinitions); expect(() => { commander.parse( [ 'node', './CLI.spec.js', '--arg0', 'arg0Value', './spec/configs/CLIConfigFail.json', ], { PROGRAM_ARG_0: 'arg0ENVValue', PROGRAM_ARG_1: 'arg1ENVValue', } ); }).toThrow('arg2 is invalid'); done(); });