public static Token GetCmdLnArgs(string[] args) { if (args == null) throw new ArgumentNullException("args"); List<Token> opts = new List<Token>(); List<Token> srcs = new List<Token>(); Action<string> Pick = delegate(string arg) { if (arg == null) return; arg = arg.Trim(); if (arg == "") return; Token t; if (Regex.IsMatch(arg, OptHead)) { if ((t = PickOpt(arg)) == null) { throw new Exception(string.Format("Not supported option: {0}", arg)); } opts.Add(t); } else { srcs.Add(PickSrc(arg)); } }; foreach (string arg in args) { Pick(arg); } Token srct = new Token("", "Sources"); srct.Follows = srcs.ToArray(); Token optt = new Token("", "CompileOptions"); optt.Follows = opts.ToArray(); if (0 == optt.Select("out").Length && srcs.Count > 0) { string v = srcs[0].Value; string dir = System.IO.Path.GetDirectoryName(v); string fn = System.IO.Path.GetFileNameWithoutExtension(v) + ".exe"; optt.FlwsAdd(NewOpt("out", System.IO.Path.Combine(dir, fn))); } Token ret = new Token("", "Arguments"); ret.Follows = new Token[] { optt, srct }; return ret; }
public static void Check(Token root) { if (root == null) { throw new ArgumentNullException("args"); } if (0 == root.Select("CompileOptions").Length) { throw new ArgumentException("No @CompileOptions Token"); } if (1 < root.Select("CompileOptions").Length) { throw new ArgumentException("Too many @CompileOptions Token"); } if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("No source filename is specified to command line parameter"); } if (1 < root.Select("Sources").Length) { throw new ArgumentException("Too many @Sources Token"); } if (0 == root.Select("CompileOptions/@out").Length) { if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("Cannot omit source path when out option was omitted"); } } if (1 < root.Select("CompileOptions/out").Length) { throw new ArgumentException("Too many out options are specified to command line parameter"); } if (1 == root.Select("Sources").Length) { foreach (Token p in root.Select("Sources/SourcePath")) { if (false == File.Exists(p.Value)) { if (false == File.Exists(p.Value + ".nana")) { throw new FileNotFoundException("Source file was not found", p.Value); } else { p.Value += ".nana"; } } } } }