OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart2js_incremental.options; | |
6 | |
7 class Options { | |
8 final List<String> arguments; | |
9 final Uri packageRoot; | |
10 final String host; | |
11 final int port; | |
12 | |
13 Options({this.arguments, this.packageRoot, this.host, this.port}); | |
14 | |
15 static String extractArgument(String option, String short, {String long}) { | |
16 if (option.startsWith(short)) { | |
17 return option.substring(short.length); | |
18 } | |
19 if (long != null && option.startsWith(long)) { | |
20 return option.substring(long.length); | |
21 } | |
22 return null; | |
23 } | |
24 | |
25 static Options parse(List<String> commandLine) { | |
26 Iterator<String> iterator = commandLine.iterator; | |
27 List<String> arguments = <String>[]; | |
28 Uri packageRoot; | |
29 String host = "127.0.0.1"; | |
30 int port = 0; | |
31 bool showHelp = false; | |
32 List<String> unknownOptions = <String>[]; | |
33 | |
34 LOOP: while (iterator.moveNext()) { | |
35 String option = iterator.current; | |
36 switch (option) { | |
37 case "-p": | |
38 iterator.moveNext(); | |
39 packageRoot = Uri.base.resolve(iterator.current); | |
40 continue; | |
41 | |
42 case "-h": | |
43 iterator.moveNext(); | |
44 host = iterator.current; | |
45 continue; | |
46 | |
47 case "-n": | |
48 iterator.moveNext(); | |
49 port = int.parse(iterator.current); | |
50 continue; | |
51 | |
52 case "--help": | |
53 showHelp = true; | |
54 continue; | |
55 | |
56 case "--": | |
57 break LOOP; | |
58 | |
59 default: | |
60 String argument; | |
61 | |
62 argument = extractArgument(option, "-p", long: "--package-root"); | |
63 if (argument != null) { | |
64 packageRoot = Uri.base.resolve(argument); | |
65 continue; | |
66 } | |
67 | |
68 argument = extractArgument(option, "-h", long: "--host"); | |
69 if (argument != null) { | |
70 host = argument; | |
71 continue; | |
72 } | |
73 | |
74 argument = extractArgument(option, "-n", long: "--port"); | |
75 if (argument != null) { | |
76 port = int.parse(option); | |
77 continue; | |
78 } | |
79 | |
80 if (option.startsWith("-")) { | |
81 unknownOptions.add(option); | |
82 continue; | |
83 } | |
84 | |
85 arguments.add(option); | |
86 break; | |
87 } | |
88 } | |
89 if (showHelp) { | |
90 print(USAGE); | |
91 } | |
92 if (!unknownOptions.isEmpty) { | |
93 print(USAGE); | |
94 print("Unknown options: '${unknownOptions.join('\', \'')}'"); | |
95 return null; | |
96 } | |
97 while (iterator.moveNext()) { | |
98 arguments.add(iterator.current); | |
99 } | |
100 if (arguments.length > 1) { | |
101 print(USAGE); | |
102 print("Extra arguments: '${arguments.skip(1).join('\', \'')}'"); | |
103 return null; | |
104 } | |
105 if (packageRoot == null) { | |
106 packageRoot = Uri.base.resolve('packages/'); | |
107 } | |
108 return new Options( | |
109 arguments: arguments, packageRoot: packageRoot, host: host, port: port); | |
110 } | |
111 } | |
112 | |
113 const String USAGE = """ | |
114 Usage: server.dart [options] [--] documentroot | |
115 | |
116 Development web server which serves files relative to [documentroot]. If a file | |
117 is missing, and the requested file name ends with '.dart.js', the server will | |
118 look for a file with the same name save '.js', compile it to JavaScript, and | |
119 serve that file instead. | |
120 | |
121 Supported options: | |
122 | |
123 -p<path>, --package-root=<path> | |
124 Where to find packages, that is, "package:..." imports. | |
125 | |
126 -h<name>, --host=<name> | |
127 Host name to bind the web server to (default 127.0.0.1). | |
128 | |
129 -n<port>, --port=<port> | |
130 Port number to bind the web server to. | |
131 | |
132 --help | |
133 Show this message. | |
134 """; | |
OLD | NEW |