* Refractored a bit.
[ngenerator.git] / Main.cs
blob21a06b857c39ebdf3e8ba17e6cd98bbcc83e3fce
1 // /home/jeremie/TaoParser/NGenerator/MainClass.cs
2 // Writtent by jeremie at 21:48 03/06/2007
3 //
4 // This file is licensed under the LGPL licence as described in the COPYING file
6 using System;
7 using System.IO;
8 using System.CodeDom;
9 using System.CodeDom.Compiler;
11 namespace NGenerator
13 public class MainClass
15 static CommandLine cmd;
17 public static void Main(string[] args)
19 cmd = new CommandLine(args);
20 if (args.Length == 0 || cmd.IsDefined("h"))
21 PrintUsageAndExit();
22 string[] names = cmd.Names;
24 if (names == null || (names != null && names.Length != 1))
25 PrintUsageAndExit();
27 PopulateOptions();
29 Logger.Debug("CommandLine retrieved and parsed. Processing the files : " + string.Join(", ", names));
31 FileInfo fi = null;
33 if (Options.Input == InputType.Directory)
34 // TODO changes these things to use Options
35 fi = GccXmlHelper.ParseDirectory(Options.InputFile.FullName);
36 else
37 fi = GccXmlHelper.ParseFile(Options.InputFile.FullName);
39 string filename = fi.FullName;
40 Logger.Debug("File generated : " + filename + ". Does it exists ? : " + fi.Exists.ToString());
42 ITranslator trans = new GccXmlTranslator();
43 CodeCompileUnit cu = trans.Translate(filename);
44 CodeDomProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
45 cp.GenerateCodeFromCompileUnit(cu, Options.Output, new CodeGeneratorOptions());
48 static void PrintUsageAndExit()
50 string usage = "NGenerator v0.1 (c) 2007 LAVAL Jérémie -- Generate *clean* C# P/Invoke class and method for interoperating with C libraries\n\n" +
51 "Usage is : ngenerator <options> filepath|directorypath\nWhere <options> may be :\n\n" +
52 "\t-h : print this help notice\n" + "\t-d : Tell NGenerator that the supplied path is a directory path" +
53 "\t-o filename : write the source code in filename\n\t-u Unsafe|Safe[UnsafeWithWrapper : the degree of \"unsafeness\" of the source, see man";
55 Console.WriteLine(usage);
57 Environment.Exit(0);
60 static void PopulateOptions()
62 Options.InputFile = new FileInfo(cmd.Names[0]);
63 Options.Input = (cmd.IsDefined("d")) ? InputType.Directory : InputType.SingleFile;
64 Options.Output = (cmd.IsDefined("o")) ? new StreamWriter(cmd["o"]) : null;
65 // TODO: make a cool Enum.(Try)Parse method instead of the funny thing that is Enum.Parse()
66 Options.Unsafe = (cmd["u"].Equals("Unsafe", StringComparison.OrdinalIgnoreCase)) ? UnsafeLevel.Unsafe : UnsafeLevel.Safe;
68 // Now Options is read-only
69 Options.Seal();