1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using System; 11 12 namespace Google.Protobuf.Examples.AddressBook 13 { 14 /// <summary> 15 /// Entry point. Repeatedly prompts user for an action to take, delegating actual behaviour 16 /// to individual actions. Each action has its own Main method, so that it can be used as an 17 /// individual complete program. 18 /// </summary> 19 internal class Program 20 { Main(string[] args)21 private static int Main(string[] args) 22 { 23 if (args.Length > 1) 24 { 25 Console.Error.WriteLine("Usage: AddressBook [file]"); 26 Console.Error.WriteLine("If the filename isn't specified, \"addressbook.data\" is used instead."); 27 return 1; 28 } 29 string addressBookFile = args.Length > 0 ? args[0] : "addressbook.data"; 30 31 bool stopping = false; 32 while (!stopping) 33 { 34 Console.WriteLine("Options:"); 35 Console.WriteLine(" L: List contents"); 36 Console.WriteLine(" A: Add new person"); 37 Console.WriteLine(" Q: Quit"); 38 Console.Write("Action? "); 39 Console.Out.Flush(); 40 char choice = Console.ReadKey().KeyChar; 41 Console.WriteLine(); 42 try 43 { 44 switch (choice) 45 { 46 case 'A': 47 case 'a': 48 AddPerson.Main(new string[] {addressBookFile}); 49 break; 50 case 'L': 51 case 'l': 52 ListPeople.Main(new string[] {addressBookFile}); 53 break; 54 case 'Q': 55 case 'q': 56 stopping = true; 57 break; 58 default: 59 Console.WriteLine("Unknown option: {0}", choice); 60 break; 61 } 62 } 63 catch (Exception e) 64 { 65 Console.WriteLine("Exception executing action: {0}", e); 66 } 67 Console.WriteLine(); 68 } 69 return 0; 70 } 71 } 72 }