3 using System.Collections.Generic;
4 using System.Text.RegularExpressions;
6 namespace bytex64.WebThing {
8 public static string ConfigurationPrefix = "/etc";
10 public static Dictionary<string,string> Options;
11 public static Dictionary<string,Dictionary<string,string>> PluginOptions;
12 public static string[] Arguments;
13 private static HashSet<string> CommandLineOptions;
15 public static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
16 public static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
19 Options = new Dictionary<string,string>();
20 PluginOptions = new Dictionary<string,Dictionary<string,string>>();
21 CommandLineOptions = new HashSet<string>();
24 private static void ParseOption(string key, string val) {
25 Match m = item_re.Match(key);
26 string plugin = m.Groups[1].Value;
27 string name = m.Groups[2].Value;
28 if (plugin.Length > 0) { // Plugin Option
29 if (!PluginOptions.ContainsKey(plugin))
30 PluginOptions[plugin] = new Dictionary<string,string>();
31 PluginOptions[plugin][name] = val;
32 } else { // Global Option
37 public static void DumpOptions() {
38 Console.WriteLine("Options:");
39 foreach (string key in Options.Keys)
40 Console.WriteLine(" {0}\t{1}", key, Options[key]);
42 Console.WriteLine("Plugin Options:");
43 foreach (string plugin in PluginOptions.Keys) {
44 Console.WriteLine(" {0}", plugin);
45 foreach (string key in PluginOptions[plugin].Keys)
46 Console.WriteLine(" {0}\t{1}", key, PluginOptions[plugin][key]);
49 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
52 public static void ParseCommandLine() {
53 Queue<string> q = new Queue<string>();
54 foreach (string s in System.Environment.GetCommandLineArgs()) {
57 q.Dequeue(); // Remove self argument
59 Regex SimpleOpt = new Regex(@"^-([\w.]+)(?:[:=](.*))?$");
60 Regex LongOpt = new Regex(@"^--([\w.]+)$");
61 List<string> args = new List<string>();
64 string opt = q.Dequeue();
67 m = SimpleOpt.Match(opt);
69 string key = m.Groups[1].Value;
70 string val = m.Groups[2].Value;
72 ParseOption(key, val);
74 ParseOption(key, null);
76 CommandLineOptions.Add(key);
80 m = LongOpt.Match(opt);
82 string key = m.Groups[1].Value;
84 string val = q.Peek();
85 if (!(SimpleOpt.IsMatch(val) || LongOpt.IsMatch(val))) {
87 ParseOption(key, val);
89 ParseOption(key, null);
92 ParseOption(key, null);
94 CommandLineOptions.Add(key);
102 Arguments = args.ToArray();
105 public static void Load() {
106 LoadFile(ConfigurationPrefix + "/WebThing.conf");
107 LoadFile(Environment.GetEnvironmentVariable("HOME") + "/.config/WebThing/WebThing.conf");
108 if (Options.ContainsKey("config"))
109 LoadFile(Options["config"]);
112 public static void LoadFile(string filename) {
115 f = new StreamReader(File.OpenRead(filename));
116 } catch (FileNotFoundException) {
117 Console.WriteLine("Could not open configuration file {0}", filename);
122 while ((line = f.ReadLine()) != null) {
123 Match m = file_var_re.Match(line.Trim());
125 string key = m.Groups[1].Value;
126 if (!CommandLineOptions.Contains(key))
127 ParseOption(key, m.Groups[2].Value);