From 878215eb4233f4fc515edee10d802ef31a1d43da Mon Sep 17 00:00:00 2001 From: Pedro Belo Date: Fri, 3 Apr 2015 11:58:51 -0700 Subject: [PATCH] Use commander to parse command-line options Makes it a bit more declarative/easier to add new commands and options to the command line. --- react-native-cli/index.js | 36 +++++++++++++++-------------------- react-native-cli/package.json | 3 +++ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/react-native-cli/index.js b/react-native-cli/index.js index 5742067f8ba80f..519c98950529c5 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -6,6 +6,7 @@ var fs = require('fs'); var path = require('path'); +var program = require('commander'); var spawn = require('child_process').spawn; var CLI_MODULE_PATH = function() { @@ -25,29 +26,22 @@ try { if (cli) { cli.run(); } else { - var args = process.argv.slice(2); - if (args.length === 0) { - console.error( - 'You did not pass any commands, did you mean to run `react-native init`?' - ); + program + .command('init ') + .description('create new React Native project with given name') + .action(init); + + program.on('*', function(command) { + console.error('Unknown command: ' + command); + program.outputHelp(); process.exit(1); - } + }); - if (args[0] === 'init') { - if (args[1]) { - init(args[1]); - } else { - console.error( - 'Usage: react-native init ' - ); - process.exit(1); - } - } else { - console.error( - 'Command `%s` unrecognized. ' + - 'Did you mean to run this inside a react-native project?', - args[0] - ); + program.parse(process.argv); + + if (!program.args.length) { + console.error('You did not pass any commands, did you mean to run `react-native init`?'); + program.outputHelp(); process.exit(1); } } diff --git a/react-native-cli/package.json b/react-native-cli/package.json index 8644e7eae217ca..a4f2a1b55495dd 100644 --- a/react-native-cli/package.json +++ b/react-native-cli/package.json @@ -5,5 +5,8 @@ "main": "index.js", "bin": { "react-native": "index.js" + }, + "dependencies": { + "commander": "^2.7.x" } }