1
2
3
4
5
6 using System;
7 using System.Collections;
8
9 void PrintHelp()
10 {
11 Console.Out() << "echo [options] <arg>..." << endl() << endl();
12 Console.Out() << "Prints arguments to standard output." << endl() << endl();
13 Console.Out() << "Options:" << endl() << endl();
14 Console.Out() << "--help | -h" << endl();
15 Console.Out() << " Print help and exit." << endl() << endl();
16 }
17
18 int main(int argc, const char** argv)
19 {
20 try
21 {
22 List<string> args;
23 for (int i = 1; i < argc; ++i;)
24 {
25 string arg = argv[i];
26 if (arg.StartsWith("--"))
27 {
28 if (arg == "--help")
29 {
30 PrintHelp();
31 return 1;
32 }
33 else
34 {
35 throw Exception("unknown option '" + arg + "'");
36 }
37 }
38 else if (arg.StartsWith("-"))
39 {
40 string options = arg.Substring(1);
41 if (options.IsEmpty())
42 {
43 throw Exception("unknown option '" + arg + "'");
44 }
45 else
46 {
47 bool unknown = false;
48 string uo;
49 for (char o : options)
50 {
51 switch (o)
52 {
53 case 'h':
54 {
55 PrintHelp();
56 return 1;
57 }
58 default:
59 {
60 unknown = true;
61 uo.Append(o);
62 break;
63 }
64 }
65 if (unknown)
66 {
67 throw Exception("unknown option '-" + uo + "'");
68 }
69 }
70 }
71 }
72 else
73 {
74 args.Add(arg);
75 }
76 }
77 bool first = true;
78 for (const string& arg : args)
79 {
80 if (first)
81 {
82 first = false;
83 }
84 else
85 {
86 Console.Out() << " ";
87 }
88 Console.Out() << arg;
89 }
90 Console.Out() << endl();
91 }
92 catch (const Exception& ex)
93 {
94 Console.Error() << ex.ToString() << endl();
95 return 1;
96 }
97 return 0;
98 }