aboutsummaryrefslogtreecommitdiff
path: root/src/AddressBook/Program.cs
blob: b3e10cbbb03786103c4cd0f837182f140f568431 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;

namespace Google.ProtocolBuffers.Examples.AddressBook
{
  /// <summary>
  /// Entry point. Repeatedly prompts user for an action to take, delegating actual behaviour
  /// to individual actions. Each action has its own Main method, so that it can be used as an
  /// invidual complete program.
  /// </summary>
  class Program {
    static int Main(string[] args) {
      if (args.Length > 1) {
        Console.Error.WriteLine("Usage: AddressBook [file]");
        Console.Error.WriteLine("If the filename isn't specified, \"addressbook.data\" is used instead.");
        return 1;
      }
      string addressBookFile = args.Length > 0 ? args[0] : "addressbook.data";

      bool stopping = false;
      while (!stopping) {
        Console.WriteLine("Options:");
        Console.WriteLine("  L: List contents");
        Console.WriteLine("  A: Add new person");
        Console.WriteLine("  Q: Quit");
        Console.Write("Action? ");
        Console.Out.Flush();
        char choice = Console.ReadKey().KeyChar;
        Console.WriteLine();
        try {
          switch (choice) {
            case 'A':
            case 'a':
              AddPerson.Main(new string[] { addressBookFile });
              break;
            case 'L':
            case 'l':
              ListPeople.Main(new string[] { addressBookFile });
              break;
            case 'Q':
            case 'q':
              stopping = true;
              break;
            default:
              Console.WriteLine("Unknown option: {0}", choice);
              break;
          }
        } catch (Exception e) {
          Console.WriteLine("Exception executing action: {0}", e);
        }
        Console.WriteLine();
      } 
      return 0;
    }
  }
}