From 007b2329842679ddf994df7ec0f9c70e73ee3caf Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 3 Apr 2017 17:09:26 +0200 Subject: [PATCH] CLI: Renamed --strict-long/message to --force-long/message with backward compatible aliases, see #741 --- cli/pbjs.js | 60 ++++++++++++++++++++++++++----------------- cli/targets/static.js | 8 +++--- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/cli/pbjs.js b/cli/pbjs.js index 2e79b0b48..330785cf0 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -23,30 +23,33 @@ exports.main = function main(args, callback) { var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins"; var argv = minimist(args, { alias: { - target : "t", - out : "o", - path : "p", - wrap : "w", - root : "r", - lint : "l" + target: "t", + out: "o", + path: "p", + wrap: "w", + root: "r", + lint: "l", + // backward compatibility: + "force-long": "strict-long", + "force-message": "strict-message" }, string: [ "target", "out", "path", "wrap", "root", "lint" ], - boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long", "strict-message" ], + boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-message" ], default: { - target : "json", - create : true, - encode : true, - decode : true, - verify : true, - convert : true, - delimited : true, - beautify : true, - comments : true, - es6 : null, - lint : lintDefault, - "keep-case" : false, - "strict-long" : false, - "strict-message": false + target: "json", + create: true, + encode: true, + decode: true, + verify: true, + convert: true, + delimited: true, + beautify: true, + comments: true, + es6: null, + lint: lintDefault, + "keep-case": false, + "force-long": false, + "force-message": false } }); @@ -54,6 +57,13 @@ exports.main = function main(args, callback) { files = argv._, paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || []; + // alias hyphen args in camel case + Object.keys(argv).forEach(function(key) { + var camelKey = key.replace(/\-([a-z])/g, function($0, $1) { return $1.toUpperCase(); }); + if (camelKey !== key) + argv[camelKey] = argv[key]; + }); + // protobuf.js package directory contains additional, otherwise non-bundled google types paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || "."); @@ -110,8 +120,9 @@ exports.main = function main(args, callback) { " --no-delimited Does not generate delimited encode/decode functions.", " --no-beautify Does not beautify generated code.", " --no-comments Does not output any JSDoc comments.", - " --strict-long Strictly references 'Long' for s-/u-/int64 and s-/fixed64 types.", - " --strict-message Strictly references message types instead of typedefs.", + "", + " --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.", + " --force-message Enfores the use of runtime messages instead of plain objects.", "", "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -", "" @@ -119,6 +130,9 @@ exports.main = function main(args, callback) { return 1; } + if (typeof argv["strict-long"] === "boolean") + argv["force-long"] = argv["strict-long"]; + // Resolve glob expressions for (var i = 0; i < files.length;) { if (glob.hasMagic(files[i])) { diff --git a/cli/targets/static.js b/cli/targets/static.js index 7d29f8f2e..2316b7a4e 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -313,7 +313,7 @@ function toJsType(field) { case "sint64": case "fixed64": case "sfixed64": - type = config["strict-long"] ? "Long" : "number|Long"; + type = config.forceLong ? "Long" : "number|Long"; break; case "bool": type = "boolean"; @@ -328,7 +328,7 @@ function toJsType(field) { if (field.resolve().resolvedType instanceof Enum) type = field.resolvedType.fullName.substring(1); // reference the enum else if (field.resolvedType instanceof Type) - type = field.resolvedType.fullName.substring(1) + (config["strict-message"] ? "" : "$Properties"); // reference the typedef + type = field.resolvedType.fullName.substring(1) + (config.forceMessage ? "" : "$Properties"); // reference the typedef else type = "*"; // should not happen break; @@ -441,7 +441,7 @@ function buildType(ref, type) { push(""); pushComment([ "Encodes the specified " + type.name + " message. Does not implicitly {@link " + fullName + ".verify|verify} messages.", - "@param {" + fullName + (config["strict-message"] ? "" : "$Properties") + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode", + "@param {" + fullName + (config.forceMessage ? "" : "$Properties") + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode", "@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to", "@returns {$protobuf.Writer} Writer" ]); @@ -451,7 +451,7 @@ function buildType(ref, type) { push(""); pushComment([ "Encodes the specified " + type.name + " message, length delimited. Does not implicitly {@link " + fullName + ".verify|verify} messages.", - "@param {" + fullName + (config["strict-message"] ? "" : "$Properties") + "} message " + type.name + " message or plain object to encode", + "@param {" + fullName + (config.forceMessage ? "" : "$Properties") + "} message " + type.name + " message or plain object to encode", "@param {$protobuf.Writer} [writer] Writer to encode to", "@returns {$protobuf.Writer} Writer" ]);