summaryrefslogtreecommitdiff
path: root/ACE/contrib/utility/Example/CommandLine/Foo/command.cpp
blob: 50b78a15350d363fe0f32f2fce0eb16fe1c11879 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string>
#include <iostream>

using std::cerr;
using std::endl;

class Command
{
public:
  enum Value
  {
    HELP = 0,
    VERSION,
    DEFAULT
  };

  Command (Value v = Command::DEFAULT)
      : v_ (v)
  {
  }

  operator Value () const
  {
    return v_;
  }

  friend std::ostream&
  operator<< (std::ostream& os, Command c);

  friend std::istream&
  operator>> (std::istream& is, Command& c);

private:
  Value v_;
  static char* labels_[];
};

char* Command::labels_[] = {"help", "version", "default"};


std::ostream&
operator<< (std::ostream& os, Command c)
{
  return os << Command::labels_[c.v_];
}

std::istream&
operator>> (std::istream& is, Command& c)
{
  std::string s;
  is >> s;
  if (is)
  {
    if (s == Command::labels_[Command::HELP]) c.v_ = Command::HELP;
    else if (s == Command::labels_[Command::VERSION]) c.v_ = Command::VERSION;
    else is.setstate (std::ios::failbit);
  }
  return is;
}

int
main ()
{
  Command c = Command::HELP;

  c = Command::DEFAULT;

  Command c1 (Command::HELP);

  c = c1;

  cerr << c << endl;

  switch (c)
  {
  case Command::HELP:
    {
      cerr << "iiihuuu!!!" << endl;
    }
  }

  std::cin >> c1;
  if (std::cin) cerr << c1 << endl;
  else cerr << "*failed" << endl;
}
//$Id$