3 import 'dart_tutorial/addressbook.pb.dart';
5 /// This function fills in a Person message based on user input.
6 Person promptForAddress() {
7 final person = Person();
9 print('Enter person ID: ');
10 final input = stdin.readLineSync();
11 person.id = int.parse(input);
14 person.name = stdin.readLineSync();
16 print('Enter email address (blank for none) : ');
17 final email = stdin.readLineSync();
18 if (email.isNotEmpty) person.email = email;
21 print('Enter a phone number (or leave blank to finish): ');
22 final number = stdin.readLineSync();
23 if (number.isEmpty) break;
25 final phoneNumber = Person_PhoneNumber()..number = number;
27 print('Is this a mobile, home, or work phone? ');
29 final type = stdin.readLineSync();
32 phoneNumber.type = Person_PhoneType.MOBILE;
35 phoneNumber.type = Person_PhoneType.HOME;
38 phoneNumber.type = Person_PhoneType.WORK;
41 print('Unknown phone type. Using default.');
43 person.phones.add(phoneNumber);
49 /// Reads the entire address book from a file, adds one person based
50 /// on user input, then writes it back out to the same file.
51 void main(List<String> arguments) {
52 if (arguments.length != 1) {
53 print('Usage: add_person ADDRESS_BOOK_FILE');
57 final file = File(arguments.first);
58 AddressBook addressBook;
59 if (!file.existsSync()) {
60 print('File not found. Creating new file.');
61 addressBook = AddressBook();
63 addressBook = AddressBook.fromBuffer(file.readAsBytesSync());
65 addressBook.people.add(promptForAddress());
66 file.writeAsBytes(addressBook.writeToBuffer());