From 25e77fb1b07566f0b1836804e76fe70d496f5c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Sun, 3 May 2015 12:22:20 +0300 Subject: [PATCH] Use the CLP library to parse the arguments. Fixed #43. --- bin/docs/help | 28 ------------ bin/git-stats | 116 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 69 insertions(+), 75 deletions(-) delete mode 100644 bin/docs/help diff --git a/bin/docs/help b/bin/docs/help deleted file mode 100644 index 4190d65..0000000 --- a/bin/docs/help +++ /dev/null @@ -1,28 +0,0 @@ -git-stats --help -A GitHub-like contributions calendar, but locally, with all your git commits. - -usage: git-stats [start] [end] [options] [data] - -start: Optional start date -end: Optional end date - -options: - -v Displays version information. - -h --help Displays this help. - --no-ansi Doesn't use ANSI colors in the squares. - --record Records a new commit. Don't use this unless you are - a mad scientist. If you are a developer, just use this - option as part of the module. - --light Enable the light theme. - -examples: - git-stats # Displays your commit calendar - git-stats -v - git-stats -h - git-stats --light # Light mode - git-stats '1 January 2012' # All the commits from 1 January 2012, to now - git-stats '1 January 2012' '31 December 2012' # All the commits from 2012 - -Your commit history is kept in the .git-stats, in your $HOME directory (~/) - -Documentation can be found at https://github.com/IonicaBizau/git-stats diff --git a/bin/git-stats b/bin/git-stats index 686c03f..681c3c9 100755 --- a/bin/git-stats +++ b/bin/git-stats @@ -7,6 +7,8 @@ var GitStats = require("../lib") , AnsiParser = require("ansi-parser") , Logger = require("bug-killer") , GitStatsColors = require("git-stats-colors") + , CLP = require("clp") + , Package = require("../package") ; // Configurations @@ -14,55 +16,75 @@ Logger.config.displayDate = false; Logger.config.logLevel = 4; Moment.suppressDeprecationWarnings = true; -// CLI options -switch (process.argv[2]) { - case "--record": - var data = process.argv[3].replace(/^\"|\"$/g, ""); - try { - data = JSON.parse(data); - } catch (e) { - throw e; - } - GitStats.record(data, function (err) { - if (err) { return Logger.log(err, "error"); } - }); - break; - case "--help": - console.log(Fs.readFileSync(__dirname + "/docs/help", "utf-8")); - break; - case "-v": - console.log(require("../package.json").version); - break; - default: - var options = {}; +// Parse the command line arguments +var recordOpt = new CLP.Option(["record"], "Records a new commit. Don't use this unless you are a mad scientist. If you are a developer, just use this option as part of the module.", "data") + , startDateOpt = new CLP.Option(["s", "start"], "Optional start date", "date") + , endDateOpt = new CLP.Option(["e", "end"], "Optional end date", "date") + , noAnsiOpt = new CLP.Option(["n", "no-ansi"], "Forces the tool not to use ANSI styles.") + , lightOpt = new CLP.Option(["l", "light"], "Enables the light theme.") + , parser = new CLP({ + name: "Git Stats" + , version: Package.version + , exe: Package.name + , examples: [ + "git-stats # Displays your commit calendar" + , "git-stats --light # Light mode" + , "git-stats '1 January 2012' # All the commits from 1 January 2012, to now" + , "git-stats '1 January 2012' '31 December 2012' # All the commits from 2012" + ] + , docs_url: "https://github.com/IonicaBizau/git-stats" + , notes: "Your commit history is kept in the .git-stats, in your $HOME directory (~/)" + , process: true + }, [ + startDateOpt + , endDateOpt + , noAnsiOpt + , lightOpt + , recordOpt + ]) + , options = null + ; - if (process.argv.length !== 2) { - if (process.argv[2]) { - options.start = Moment(process.argv[2]); - } +// --record +if (recordOpt.is_provided) { - if (process.argv[3]) { - options.end = Moment(process.argv[3]); - } - } + try { + options = JSON.parse(recordOpt.value.replace(/^\"|\"$/g, "")); + } catch (e) { + Logger.log(e, "error"); + return process.exit(1); + } - if (!options.start || !options.start.isValid()) { - options.start = Moment().subtract(1, "years"); - } - - if (!options.end || !options.end.isValid()) { - options.end = Moment(); - } - - GitStats.ansiCalendar(options, function (err, data) { - if (err) { return Logger.log(err, "error"); } - data = AnsiParser.removeAnsi(data); - if (process.argv.indexOf("--no-ansi") === -1) { - data = GitStatsColors(data, process.argv.indexOf("--light") !== -1 ? "LIGHT": "DARK"); - } - - console.log(err || data); - }); - break; + return GitStats.record(options, function (err) { + if (err) { return Logger.log(err, "error"); } + process.exit(0); + }); } + +// Create the options +options = { + start: startDateOpt.value ? Moment(startDateOpt.value) : Moment().subtract(1, "years") + , end: endDateOpt.value ? Moment(endDateOpt.value) : Moment() +}; + +// Validate the dates +if (!options.start || !options.start.isValid()) { + options.start = Moment().subtract(1, "years"); + Logger.log("Invalid start date. Using default instead (" + options.start.format("LL") + ").", "warn"); +} + +if (!options.end || !options.end.isValid()) { + options.end = Moment(); + Logger.log("Invalid end date. Using default instead (" + options.end.format("LL") + ").", "warn"); +} + +// Create the ANSI calendar +GitStats.ansiCalendar(options, function (err, data) { + if (err) { return Logger.log(err, "error"); } + data = AnsiParser.removeAnsi(data); + if (!noAnsiOpt.is_provided) { + data = GitStatsColors(data, lightOpt.is_provided ? "LIGHT": "DARK"); + } + process.stdout.write(data + "\n"); +});