Skip to content
Browse files

Updating LaunchOptions to encompase URL and command line parsing, bas…

…ic reflection, etc - removing dependency on nopt
  • Loading branch information...
1 parent 814795a commit efc6d41230d41d8bd95b1254143db1447759cf91 @benvanik committed
Showing with 135 additions and 21 deletions.
  1. +0 −3 .gitmodules
  2. +135 −17 src/gf/launchoptions.js
  3. +0 −1 third_party/nopt
View
3 .gitmodules
@@ -7,9 +7,6 @@
[submodule "third_party/ws"]
path = third_party/ws
url = https://github.com/einaros/ws.git
-[submodule "third_party/nopt"]
- path = third_party/nopt
- url = https://github.com/isaacs/nopt.git
[submodule "third_party/abbrev"]
path = third_party/abbrev
url = https://github.com/isaacs/abbrev-js.git
View
152 src/gf/launchoptions.js
@@ -22,48 +22,111 @@ goog.provide('gf.LaunchOptions');
goog.require('goog.Uri');
goog.require('goog.asserts');
+goog.require('goog.object');
+goog.require('goog.string');
/**
* Launch options utility.
* Will parse out the given URI and provides helpers that subclasses can use to
- * more easily access the information.
+ * more easily access the information. Key lookup is case-insensitive.
*
* @constructor
+ * @param {!Object.<!gf.LaunchOptions.ArgumentInfo>} argumentInfo Argument
+ * information from subclasses.
* @param {string} uri Source app URI string.
+ * @param {Object.<*>=} opt_args Key-value argument map.
*/
-gf.LaunchOptions = function(uri) {
+gf.LaunchOptions = function(argumentInfo, uri, opt_args) {
+ /**
+ * Argument information, used for default lookup/help printing/etc.
+ * @type {!Object.<!gf.LaunchOptions.ArgumentInfo>}
+ */
+ this.argumentInfo = argumentInfo;
+ goog.object.extend(this.argumentInfo, gf.LaunchOptions.getArgumentInfo());
+
/**
* Launch page URI.
* @type {!goog.Uri}
*/
this.uri = new goog.Uri(uri);
- var queryData = this.uri.getQueryData();
- goog.asserts.assert(queryData);
-
/**
- * URI query data.
+ * Argument query data.
* @type {!goog.Uri.QueryData}
*/
- this.queryData = queryData;
+ this.queryData = new goog.Uri.QueryData(undefined, undefined, true);
+
+ // Add URI query data
+ var uriQueryData = this.uri.getQueryData();
+ if (uriQueryData) {
+ this.queryData.extend(uriQueryData);
+ }
+
+ // Add given key/values
+ this.queryData.extend(opt_args);
/**
* Build server daemon, if running dynamic mode.
* @type {string?}
*/
- this.buildServer = this.getString('buildServer', null);
+ this.buildServer = this.getString('buildServer');
+};
+
+
+/**
+ * Argument type.
+ * @typedef {boolean|number|string|null}
+ */
+gf.LaunchOptions.ArgumentType;
+
+
+/**
+ * Argument information.
+ * @typedef {{
+ * help: string,
+ * type: !Object,
+ * defaultValue: gf.LaunchOptions.ArgumentType
+ * }}
+ */
+gf.LaunchOptions.ArgumentInfo;
+
+
+/**
+ * Gets argument information for the default launch option arguments.
+ * @return {!Object.<!gf.LaunchOptions.ArgumentInfo>} Argument information.
+ */
+gf.LaunchOptions.getArgumentInfo = function() {
+ return {
+ 'buildServer': {
+ help: 'Build server daemon address.',
+ type: String,
+ defaultValue: null
+ }
+ };
+};
+
+
+/**
+ * Gets the default value of the given argument.
+ * @param {string} name Argument name.
+ * @return {gf.LaunchOptions.ArgumentType} Default value.
+ */
+gf.LaunchOptions.prototype.getDefaultValue = function(name) {
+ var value = this.argumentInfo[name];
+ goog.asserts.assert(goog.isDef(value));
+ return value.defaultValue;
};
/**
* Gets a number value.
- * @param {string} name String name.
- * @param {number?} defaultValue Default string value.
- * @return {number?} String value.
+ * @param {string} name Number name.
+ * @return {number?} Number value.
*/
-gf.LaunchOptions.prototype.getNumber = function(name, defaultValue) {
+gf.LaunchOptions.prototype.getNumber = function(name) {
+ var defaultValue = /** @type {number} */ (this.getDefaultValue(name));
var value = this.queryData.get(name, defaultValue);
if (!goog.isDef(value) || !value || !value.length) {
return defaultValue;
@@ -73,14 +136,29 @@ gf.LaunchOptions.prototype.getNumber = function(name, defaultValue) {
/**
+ * Gets a number value, ensuring it is not null.
+ * @param {string} name Number name.
+ * @return {number} Number value.
+ */
+gf.LaunchOptions.prototype.getNumberAlways = function(name) {
+ var value = this.getNumber(name);
+ goog.asserts.assert(goog.isDef(value) && value !== null);
+ return value;
+};
+
+
+/**
* Gets a string value.
* @param {string} name String name.
- * @param {string?} defaultValue Default string value.
* @return {string?} String value.
*/
-gf.LaunchOptions.prototype.getString = function(name, defaultValue) {
+gf.LaunchOptions.prototype.getString = function(name) {
+ var defaultValue = /** @type {string} */ (this.getDefaultValue(name));
var value = this.queryData.get(name, defaultValue);
- if (!goog.isDef(value) || !value || !value.length) {
+ if (!goog.isDef(value) ||
+ !value ||
+ !value.length ||
+ goog.string.isEmpty(String(value))) {
return defaultValue;
}
return String(value);
@@ -88,12 +166,24 @@ gf.LaunchOptions.prototype.getString = function(name, defaultValue) {
/**
+ * Gets a string value, ensuring it is not empty or null.
+ * @param {string} name String name.
+ * @return {string} String value.
+ */
+gf.LaunchOptions.prototype.getStringAlways = function(name) {
+ var value = this.getString(name);
+ goog.asserts.assert(goog.isDef(value) && value !== null);
+ return value;
+};
+
+
+/**
* Gets a boolean flag value.
* @param {string} name Flag name.
- * @param {boolean} defaultValue Default flag value.
* @return {boolean} Flag value.
*/
-gf.LaunchOptions.prototype.getFlag = function(name, defaultValue) {
+gf.LaunchOptions.prototype.getFlag = function(name) {
+ var defaultValue = /** @type {boolean} */ (this.getDefaultValue(name));
var value = this.queryData.get(name, defaultValue);
switch (value) {
case undefined:
@@ -108,3 +198,31 @@ gf.LaunchOptions.prototype.getFlag = function(name, defaultValue) {
return true;
}
};
+
+
+/**
+ * Parses a list of command line arguments and builds a key-value map
+ * that can be provided to a {@see gf.LaunchOptions} instance.
+ * This looks for arguments in the form of --key and --key=value.
+ *
+ * @param {!Array.<string>} args Command line arguments.
+ * @return {!Object.<*>} Key-value map.
+ */
+gf.LaunchOptions.parseCommandLine = function(args) {
+ var result = {};
+ for (var n = 0; n < args.length; n++) {
+ var arg = args[n];
+ if (arg.indexOf('--') != 0) {
+ continue;
+ }
+ var parts = arg.split('=');
+ var key = parts[0].substring(2);
+ var value = parts[1];
+ if (parts.length > 1) {
+ result[key] = value;
+ } else {
+ result[key] = true;
+ }
+ }
+ return result;
+};
1 third_party/nopt
@@ -1 +0,0 @@
-Subproject commit 6edc04891ff24d07fb8cc071e760b30c0bb9cf0c

0 comments on commit efc6d41

Please sign in to comment.
Something went wrong with that request. Please try again.