Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3334)

Unified Diff: dart/pkg/dart2js_incremental/lib/server.dart

Issue 858833002: Add server options. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/pkg/dart2js_incremental/lib/server.dart
diff --git a/dart/pkg/dart2js_incremental/lib/server.dart b/dart/pkg/dart2js_incremental/lib/server.dart
index a31c64d96400a511d1e8d276a3d349510653ed64..fd329639dc1403a3c55cb31490bc11603551a67c 100644
--- a/dart/pkg/dart2js_incremental/lib/server.dart
+++ b/dart/pkg/dart2js_incremental/lib/server.dart
@@ -159,21 +159,123 @@ class Conversation {
}
}
-main(List<String> arguments) {
- if (arguments.length > 0) {
- Conversation.documentRoot = Uri.base.resolve(arguments[0]);
+class Options {
kasperl 2015/01/19 09:37:27 Consider having this in a separate part or library
ahe 2015/01/19 10:23:08 Done.
+ final List<String> arguments;
+ final Uri packageRoot;
+ final String host;
+ final int port;
+
+ Options({this.arguments, this.packageRoot, this.host, this.port});
+
+ static String extractArgument(String option, String short, {String long}) {
+ if (option.startsWith(short)) {
+ return option.substring(short.length);
+ }
+ if (long != null && option.startsWith(long)) {
+ return option.substring(long.length);
+ }
+ return null;
}
- var host = '127.0.0.1';
- if (arguments.length > 1) {
- host = arguments[1];
+
+ static Options parse(List<String> commandLine) {
+ Iterator<String> iterator = commandLine.iterator;
+ List<String> arguments = <String>[];
+ Uri packageRoot;
+ String host = "127.0.0.1";
+ int port = 0;
+ bool showHelp = false;
+ List<String> unknownOptions = <String>[];
+
+ LOOP: while (iterator.moveNext()) {
+ String option = iterator.current;
+ switch (option) {
+ case "-p":
+ iterator.moveNext();
+ packageRoot = Uri.base.resolve(iterator.current);
+ continue;
+
+ case "-h":
+ iterator.moveNext();
+ host = iterator.current;
+ continue;
+
+ case "-n":
+ iterator.moveNext();
+ port = int.parse(iterator.current);
+ continue;
+
+ case "--help":
+ showHelp = true;
+ continue;
+
+ case "--":
+ break LOOP;
+
+ default:
+ String argument;
+
+ argument = extractArgument(option, "-p", long: "--package-root");
+ if (argument != null) {
+ packageRoot = Uri.base.resolve(argument);
+ continue;
+ }
+
+ argument = extractArgument(option, "-h", long: "--host");
+ if (argument != null) {
+ host = argument;
+ continue;
+ }
+
+ argument = extractArgument(option, "-n", long: "--port");
+ if (argument != null) {
+ port = int.parse(option);
+ continue;
+ }
+
+ if (option.startsWith("-")) {
+ unknownOptions.add(option);
+ continue;
+ }
+
+ arguments.add(option);
+ break;
+ }
+ }
+ if (showHelp) {
+ print(USAGE);
+ }
+ if (!unknownOptions.isEmpty) {
+ print(USAGE);
+ print("Unknown options: '${unknownOptions.join('\', \'')}'");
+ return null;
+ }
+ while (iterator.moveNext()) {
+ arguments.add(iterator.current);
+ }
+ if (arguments.length > 1) {
+ print(USAGE);
+ print("Extra arguments: '${arguments.skip(1).join('\', \'')}'");
+ return null;
+ }
+ if (packageRoot == null) {
+ packageRoot = Uri.base.resolve('packages/');
+ }
+ return new Options(
+ arguments: arguments, packageRoot: packageRoot, host: host, port: port);
}
- int port = 0;
- if (arguments.length > 2) {
- port = int.parse(arguments[2]);
+}
+
+main(List<String> arguments) {
+ Options options = Options.parse(arguments);
+ if (options == null) {
+ exit(1);
}
- if (arguments.length > 3) {
- Conversation.packageRoot = Uri.base.resolve(arguments[4]);
+ if (!options.arguments.isEmpty) {
+ Conversation.documentRoot = Uri.base.resolve(options.arguments.single);
}
+ Conversation.packageRoot = options.packageRoot;
+ String host = options.host;
+ int port = options.port;
HttpServer.bind(host, port).then((HttpServer server) {
print('HTTP server started on http://$host:${server.port}/');
server.listen(Conversation.onRequest, onError: Conversation.onError);
@@ -182,3 +284,26 @@ main(List<String> arguments) {
exit(1);
});
}
+
+const String USAGE = """
+Usage: server.dart [options] [--] documentroot
+
+Development web server which serves files relative to [documentroot]. If a file
+is missing, and the requested file name ends with '.dart.js', the server will
+look for a file with the same name save '.js', compile it to JavaScript, and
+serve that file instead.
ahe 2015/01/19 09:32:22 Not implemented yet.
+
+Supported options:
+
+ -p<path>, --package-root=<path>
+ Where to find packages, that is, "package:..." imports.
+
+ -h<name>, --host=<name>
+ Host name to bind the web server to (default 127.0.0.1).
+
+ -n<port>, --port=<port>
+ Port number to bind the web server to.
+
+ --help
+ Show this message.
+""";
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698