Skip to content

Commit f501363

Browse files
committed
Add command line arguments to all examples. Were already on some.
1 parent b432620 commit f501363

File tree

9 files changed

+125
-14
lines changed

9 files changed

+125
-14
lines changed

pkgs/shelf/example/example.dart

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
7+
import 'package:args/args.dart';
58
import 'package:shelf/shelf.dart';
69
import 'package:shelf/shelf_io.dart' as shelf_io;
710

8-
void main() async {
11+
void main(List<String> args) async {
12+
final parser = _getParser();
13+
14+
String address;
15+
int port;
16+
try {
17+
final result = parser.parse(args);
18+
address = result['address'] as String;
19+
port = int.parse(result['port'] as String);
20+
} on FormatException catch (e) {
21+
stderr
22+
..writeln(e.message)
23+
..writeln(parser.usage);
24+
// http://linux.die.net/include/sysexits.h
25+
// #define EX_USAGE 64 /* command line usage error */
26+
exit(64);
27+
}
28+
929
var handler =
1030
const Pipeline().addMiddleware(logRequests()).addHandler(_echoRequest);
1131

12-
var server = await shelf_io.serve(handler, 'localhost', 8080);
32+
var server = await shelf_io.serve(handler, address, port);
1333

1434
// Enable content compression
1535
server.autoCompress = true;
@@ -19,3 +39,8 @@ void main() async {
1939

2040
Response _echoRequest(Request request) =>
2141
Response.ok('Request for "${request.url}"');
42+
43+
ArgParser _getParser() => ArgParser()
44+
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
45+
..addOption('address',
46+
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');

pkgs/shelf/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
stream_channel: ^2.1.0
2222

2323
dev_dependencies:
24+
args: ^2.0.0
2425
dart_flutter_team_lints: ^2.0.0
2526
http: '>=0.13.0 <2.0.0'
2627
test: ^1.16.0

pkgs/shelf_proxy/example/example.dart

+35-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
7+
import 'package:args/args.dart';
58
import 'package:shelf/shelf_io.dart' as shelf_io;
69
import 'package:shelf_proxy/shelf_proxy.dart';
710

8-
Future<void> main() async {
11+
Future<void> main(List<String> args) async {
12+
final parser = _getParser();
13+
14+
String address;
15+
int port;
16+
String targetAddress;
17+
try {
18+
final result = parser.parse(args);
19+
address = result['address'] as String;
20+
port = int.parse(result['port'] as String);
21+
targetAddress = result['targetAddress'] as String;
22+
} on FormatException catch (e) {
23+
stderr
24+
..writeln(e.message)
25+
..writeln(parser.usage);
26+
// http://linux.die.net/include/sysexits.h
27+
// #define EX_USAGE 64 /* command line usage error */
28+
exit(64);
29+
}
30+
931
final server = await shelf_io.serve(
10-
proxyHandler('https://dart.dev'),
11-
'localhost',
12-
8080,
32+
proxyHandler(targetAddress),
33+
address,
34+
port,
1335
);
1436

15-
print('Proxying at http://${server.address.host}:${server.port}');
37+
print(
38+
'Proxying for $targetAddress at http://${server.address.host}:${server.port}');
1639
}
40+
41+
ArgParser _getParser() => ArgParser()
42+
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
43+
..addOption('address',
44+
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on')
45+
..addOption('targetAddress',
46+
abbr: 't', defaultsTo: 'https://dart.dev', help: 'Address proxying for');

pkgs/shelf_proxy/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ dependencies:
1616
shelf: ^1.0.0
1717

1818
dev_dependencies:
19+
args: ^2.0.0
1920
dart_flutter_team_lints: ^2.0.0
2021
test: ^1.6.0

pkgs/shelf_router/example/main.dart

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
import 'dart:async' show Future;
16+
import 'dart:io';
1617

18+
import 'package:args/args.dart';
1719
import 'package:shelf/shelf.dart';
1820
import 'package:shelf/shelf_io.dart' as shelf_io;
1921
import 'package:shelf_router/shelf_router.dart';
@@ -79,8 +81,30 @@ class Api {
7981
}
8082

8183
// Run shelf server and host a [Service] instance on port 8080.
82-
void main() async {
84+
void main(List<String> args) async {
85+
final parser = _getParser();
86+
87+
String address;
88+
int port;
89+
try {
90+
final result = parser.parse(args);
91+
address = result['address'] as String;
92+
port = int.parse(result['port'] as String);
93+
} on FormatException catch (e) {
94+
stderr
95+
..writeln(e.message)
96+
..writeln(parser.usage);
97+
// http://linux.die.net/include/sysexits.h
98+
// #define EX_USAGE 64 /* command line usage error */
99+
exit(64);
100+
}
101+
83102
final service = Service();
84-
final server = await shelf_io.serve(service.handler, 'localhost', 8080);
103+
final server = await shelf_io.serve(service.handler, address, port);
85104
print('Server running on localhost:${server.port}');
86105
}
106+
107+
ArgParser _getParser() => ArgParser()
108+
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
109+
..addOption('address',
110+
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');

pkgs/shelf_router/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
shelf: ^1.0.0
1919

2020
dev_dependencies:
21+
args: ^2.0.0
2122
dart_flutter_team_lints: ^2.0.0
2223
http: '>=0.13.0 <2.0.0'
2324
test: ^1.16.0

pkgs/shelf_router_generator/example/main.dart

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414

1515
import 'dart:async' show Future;
16+
import 'dart:io';
1617

18+
import 'package:args/args.dart';
1719
import 'package:shelf/shelf.dart';
1820
import 'package:shelf/shelf_io.dart' as shelf_io;
1921
import 'package:shelf_router/shelf_router.dart';
@@ -73,8 +75,30 @@ class Api {
7375
}
7476

7577
// Run shelf server and host a [Service] instance on port 8080.
76-
void main() async {
78+
void main(List<String> args) async {
79+
final parser = _getParser();
80+
81+
String address;
82+
int port;
83+
try {
84+
final result = parser.parse(args);
85+
address = result['address'] as String;
86+
port = int.parse(result['port'] as String);
87+
} on FormatException catch (e) {
88+
stderr
89+
..writeln(e.message)
90+
..writeln(parser.usage);
91+
// http://linux.die.net/include/sysexits.h
92+
// #define EX_USAGE 64 /* command line usage error */
93+
exit(64);
94+
}
95+
7796
final service = Service();
78-
final server = await shelf_io.serve(service.handler, 'localhost', 8080);
97+
final server = await shelf_io.serve(service.handler, address, port);
7998
print('Server running on localhost:${server.port}');
8099
}
100+
101+
ArgParser _getParser() => ArgParser()
102+
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
103+
..addOption('address',
104+
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on');

pkgs/shelf_router_generator/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies:
2323
source_gen: ^1.0.0
2424

2525
dev_dependencies:
26+
args: ^2.0.0
2627
build_runner: ^2.0.0
2728
build_verify: ^3.0.0
2829
dart_flutter_team_lints: ^2.0.0

pkgs/shelf_static/example/example.dart

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import 'package:shelf_static/shelf_static.dart';
1111
void main(List<String> args) {
1212
final parser = _getParser();
1313

14+
String address;
1415
int port;
1516
bool logging;
1617
bool listDirectories;
1718

1819
try {
1920
final result = parser.parse(args);
21+
address = result['address'] as String;
2022
port = int.parse(result['port'] as String);
2123
logging = result['logging'] as bool;
2224
listDirectories = result['list-directories'] as bool;
@@ -47,14 +49,16 @@ void main(List<String> args) {
4749
final handler = pipeline.addHandler(createStaticHandler('example/files',
4850
defaultDocument: defaultDoc, listDirectories: listDirectories));
4951

50-
io.serve(handler, 'localhost', port).then((server) {
52+
io.serve(handler, address, port).then((server) {
5153
print('Serving at http://${server.address.host}:${server.port}');
5254
});
5355
}
5456

5557
ArgParser _getParser() => ArgParser()
56-
..addFlag('logging', abbr: 'l', defaultsTo: true)
57-
..addOption('port', abbr: 'p', defaultsTo: '8080')
58+
..addFlag('logging', abbr: 'l', defaultsTo: true, help: 'Enable logging')
59+
..addOption('port', abbr: 'p', defaultsTo: '8080', help: 'Port to listen on')
60+
..addOption('address',
61+
abbr: 'a', defaultsTo: 'localhost', help: 'Address to listen on')
5862
..addFlag('list-directories',
5963
abbr: 'f',
6064
negatable: false,

0 commit comments

Comments
 (0)