Jon Skeet | 3f22511 | 2008-11-24 16:09:39 +0000 | [diff] [blame^] | 1 | using System; |
| 2 | |
| 3 | namespace Google.ProtocolBuffers.Examples.AddressBook |
| 4 | { |
| 5 | /// <summary> |
| 6 | /// Entry point. Repeatedly prompts user for an action to take, delegating actual behaviour |
| 7 | /// to individual actions. Each action has its own Main method, so that it can be used as an |
| 8 | /// invidual complete program. |
| 9 | /// </summary> |
| 10 | class Program { |
| 11 | static int Main(string[] args) { |
| 12 | if (args.Length > 1) { |
| 13 | Console.Error.WriteLine("Usage: AddressBook [file]"); |
| 14 | Console.Error.WriteLine("If the filename isn't specified, \"addressbook.data\" is used instead."); |
| 15 | return 1; |
| 16 | } |
| 17 | string addressBookFile = args.Length > 0 ? args[0] : "addressbook.data"; |
| 18 | |
| 19 | bool stopping = false; |
| 20 | while (!stopping) { |
| 21 | Console.WriteLine("Options:"); |
| 22 | Console.WriteLine(" L: List contents"); |
| 23 | Console.WriteLine(" A: Add new person"); |
| 24 | Console.WriteLine(" Q: Quit"); |
| 25 | Console.Write("Action? "); |
| 26 | Console.Out.Flush(); |
| 27 | char choice = Console.ReadKey().KeyChar; |
| 28 | Console.WriteLine(); |
| 29 | try { |
| 30 | switch (choice) { |
| 31 | case 'A': |
| 32 | case 'a': |
| 33 | AddPerson.Main(new string[] { addressBookFile }); |
| 34 | break; |
| 35 | case 'L': |
| 36 | case 'l': |
| 37 | ListPeople.Main(new string[] { addressBookFile }); |
| 38 | break; |
| 39 | case 'Q': |
| 40 | case 'q': |
| 41 | stopping = true; |
| 42 | break; |
| 43 | default: |
| 44 | Console.WriteLine("Unknown option: {0}", choice); |
| 45 | break; |
| 46 | } |
| 47 | } catch (Exception e) { |
| 48 | Console.WriteLine("Exception executing action: {0}", e); |
| 49 | } |
| 50 | Console.WriteLine(); |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | } |
| 55 | } |