This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.cs
127 lines (119 loc) · 3.7 KB
/
config.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Xml.Serialization;
[XmlRoot("config")]
public class Config {
[XmlAttribute] public string Author;
[XmlAttribute] public string BlogDirectory;
[XmlAttribute] public string BlogFileName;
[XmlAttribute] public string BlogWebDirectory;
[XmlAttribute] public string BlogImageBasedir;
[XmlAttribute] public string Copyright;
[XmlAttribute] public string Description;
[XmlAttribute] public string ManagingEditor;
[XmlAttribute] public string Link;
[XmlAttribute] public string Title;
[XmlAttribute] public string RSSFileName;
[XmlAttribute] public string InputEncoding;
[XmlAttribute] public string OutputEncoding;
[XmlAttribute] public string AnalyticsStub;
[XmlAttribute] public string Prefix;
[XmlAttribute] public string BlogTemplate;
[XmlAttribute] public string EntryTemplate;
[XmlAttribute] public string CommentsStub;
public bool Parse (string[] args)
{
for (int i = 0; i < args.Length; ++i) {
string arg = args [i];
switch (arg) {
case "-h": case "--help":
PrintHelp ();
return false;
case "-p": case "--prefix":
if (NextArgument (args, ref i, ref Prefix))
break;
return false;
case "-d": case "--blog-directory":
if (NextArgument (args, ref i, ref BlogDirectory))
break;
return false;
case "-b": case "--blog-template":
if (NextArgument (args, ref i, ref BlogTemplate))
break;
return false;
case "-e": case "--entry-template":
if (NextArgument (args, ref i, ref EntryTemplate))
break;
return false;
case "-x": case "--rss-filename":
if (NextArgument (args, ref i, ref RSSFileName))
break;
return false;
default:
if (ExtractArgument ("-p", arg, ref Prefix))
break;
if (ExtractArgument ("--prefix", arg, ref Prefix))
break;
if (ExtractArgument ("-b", arg, ref BlogTemplate))
break;
if (ExtractArgument ("--blog-template", arg, ref BlogTemplate))
break;
if (ExtractArgument ("-e", arg, ref EntryTemplate))
break;
if (ExtractArgument ("--entry-template", arg, ref EntryTemplate))
break;
if (ExtractArgument ("-d", arg, ref BlogDirectory))
break;
if (ExtractArgument ("--blog-directory", arg, ref BlogDirectory))
break;
if (ExtractArgument ("-x", arg, ref RSSFileName))
break;
if (ExtractArgument ("--rss-filename", arg, ref RSSFileName))
break;
Error ("unrecognized option `{0}'", arg);
return false;
}
}
return true;
}
private static void PrintHelp ()
{
Console.WriteLine ("Usage: lb [OPTION]*");
Console.WriteLine ("lb (Lame Blog) is a blog engine.");
Console.WriteLine (@"
Options:
-p, --prefix=DIR Root directory for generated files.
-d, --blog-directory=DIR Where to find blog entry files (*.txt, *.html).
-b, --blog-template=FILE Blog template file .
-e, --entry-template=FILE Entry template file.
-x, --rss-filename=FILE Basename for RSS filename.
-h, --help Display this message and exit.
");
}
private void Error (string format, params object[] args)
{
Console.Write ("lb: ");
Console.WriteLine (format, args);
Console.WriteLine ("Try `lb --help' for more information.");
}
private bool ExtractArgument (string prefix, string argument, ref string value)
{
if (argument.Length - 1 <= prefix.Length)
return false;
if (!argument.StartsWith (prefix))
return false;
char delim = argument [prefix.Length];
if (delim != '=' && delim != ':')
return false;
value = argument.Substring (prefix.Length+1);
return true;
}
private bool NextArgument (string[] args, ref int i, ref string value)
{
if ((i+1) >= args.Length) {
Error ("missing argument for `{0}'", args [i]);
return false;
}
value = args [++i];
return true;
}
}