Browse Source

Cleared command line options to make them more standard

pull/731/head
Marco Zille 9 years ago
parent
commit
a80fefde9b
  1. 111
      src/CLI/CLI.cs
  2. 16
      src/CLI/Generator.cs
  3. 7
      src/CLI/Options.cs

111
src/CLI/CLI.cs

@ -10,63 +10,82 @@ namespace CppSharp
class CLI class CLI
{ {
private static Options _options = new Options(); private static Options _options = new Options();
private static List<string> _assemblies;
static void AddIncludeDirs(String dir) static void AddIncludeDirs(String dir)
{ {
_options.IncludeDirs.Add(dir); _options.IncludeDirs.Add(dir);
} }
static void ParseCommandLineArgs(string[] args) static bool ParseCommandLineArgs(string[] args)
{ {
var showHelp = args.Length == 0; var showHelp = false;
var optionSet = new Mono.Options.OptionSet() var optionSet = new Mono.Options.OptionSet()
{ {
{ "h|header=", "the path to an header file to generate source from", h => _options.HeaderFiles.Add(h) }, { "I=", "the {PATH} of a folder to search for include files", i => { AddIncludeDirs(i); } },
{ "pa|path=", "the path of a folder whose files will generate code (can append a filter at the end like '<path>/*.hpp'", pa => { GetFilesFromPath(pa); } }, { "l=", "{LIBRARY} that includes the definitions for the generated source code", l => _options.Libraries.Add(l) },
{ "inc|includedir=", "the path of a folder to search for include files", i => { AddIncludeDirs(i); } }, { "L=", "the {PATH} of a folder to search for additional libraries", l => _options.LibraryDirs.Add(l) },
{ "l|library=", "the path of a library that includes the definitions for the generated source code", l => _options.Libraries.Add(l) }, { "D:", "additional define with (optional) value to add to be used while parsing the given header files", (n, v) => AddDefine(n, v) },
{ "ld|librarydir=", "the path of a folder to search for additional libraries", l => _options.LibraryDirs.Add(l) }, { "o=|outputdir=", "the {PATH} for the destination folder that will contain the generated code", od => _options.OutputDir = od },
{ "d|define=", "a define to add for the parse of the given header files", d => _options.Defines.Add(d) }, { "on=|outputnamespace=", "the {NAMESPACE} that will be used for the generated code", on => _options.OutputNamespace = on },
{ "od|outputdir=", "the path for the destination folder that will contain the generated code", od => _options.OutputDir = od }, { "iln=|inputlibraryname=", "the {NAME} of the shared library that contains the actual definitions (without extension)", iln => _options.InputLibraryName = iln },
{ "on|outputnamespace=", "the namespace that will be used for the generated code", on => _options.OutputNamespace = on }, { "isln=|inputsharedlibraryname=", "the full {NAME} of the shared library that contains the actual definitions (with extension)", isln => _options.InputSharedLibraryName = isln },
{ "iln|inputlibraryname=", "the name of the shared library that contains the actual definitions (without extension)", iln => _options.InputLibraryName = iln }, { "g=|gen=|generator=", "the {TYPE} of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g); } },
{ "isln|inputsharedlibraryname=", "the full name of the shared library that contains the actual definitions (with extension)", isln => _options.InputSharedLibraryName = isln }, { "p=|platform=", "the {PLATFORM} that the generated code will target: 'win', 'osx' or 'linux'", p => { GetDestinationPlatform(p); } },
{ "gen|generator=", "the type of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g); } }, { "a=|arch=", "the {ARCHITECTURE} that the generated code will target: 'x86' or 'x64'", a => { GetDestinationArchitecture(a); } },
{ "p|platform=", "the platform that the generated code will target: 'win', 'osx', 'linux'", p => { GetDestinationPlatform(p); } },
{ "a|arch=", "the architecture that the generated code will target: 'x86', 'x64'", a => { GetDestinationArchitecture(a); } },
{ "c++11", "enables GCC C++ 11 compilation (valid only for Linux platform)", cpp11 => { _options.Cpp11ABI = (cpp11 != null); } }, { "c++11", "enables GCC C++ 11 compilation (valid only for Linux platform)", cpp11 => { _options.Cpp11ABI = (cpp11 != null); } },
{ "cs|checksymbols", "enable the symbol check for the generated code", cs => { _options.CheckSymbols = (cs != null); } }, { "cs|checksymbols", "enable the symbol check for the generated code", cs => { _options.CheckSymbols = (cs != null); } },
{ "ub|unitybuild", "enable unity build", ub => { _options.UnityBuild = (ub != null); } }, { "ub|unitybuild", "enable unity build", ub => { _options.UnityBuild = (ub != null); } },
{ "help", "shows the help", hl => { showHelp = (hl != null); } } { "h|help", "shows the help", hl => { showHelp = (hl != null); } },
}; };
List<String> additionalArguments = null;
try try
{ {
_assemblies = optionSet.Parse(args); additionalArguments = optionSet.Parse(args);
} }
catch (Mono.Options.OptionException e) catch (Mono.Options.OptionException e)
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
Environment.Exit(0); return false;
} }
if (showHelp) if (showHelp || additionalArguments != null && additionalArguments.Count == 0)
{ {
// Print usage and exit. ShowHelp(optionSet);
Console.WriteLine("{0} [options]+", AppDomain.CurrentDomain.FriendlyName); return false;
Console.WriteLine("Generates target language bindings for interop with unmanaged code.");
Console.WriteLine();
optionSet.WriteOptionDescriptions(Console.Out);
Environment.Exit(0);
} }
if (_assemblies == null) foreach(String s in additionalArguments)
{ HandleAdditionalArgument(s);
Console.WriteLine("Invalid arguments.");
Environment.Exit(0); return true;
} }
static void ShowHelp(Mono.Options.OptionSet options)
{
Console.WriteLine("Usage: {0} [options]+", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Generates target language bindings to interop with unmanaged code.");
Console.WriteLine();
Console.WriteLine("Options:");
options.WriteOptionDescriptions(Console.Out);
Console.WriteLine();
}
static void AddDefine(String name, String value)
{
if (name == null)
throw new Mono.Options.OptionException("Invalid definition name for option -D.", "-D");
_options.Defines.Add(name, value);
}
static void HandleAdditionalArgument(String args)
{
if (System.IO.Directory.Exists(args))
GetFilesFromPath(args);
else if (System.IO.File.Exists(args))
_options.HeaderFiles.Add(args);
} }
static void GetFilesFromPath(String path) static void GetFilesFromPath(String path)
@ -115,10 +134,10 @@ namespace CppSharp
{ {
case "csharp": case "csharp":
_options.Kind = CppSharp.Generators.GeneratorKind.CSharp; _options.Kind = CppSharp.Generators.GeneratorKind.CSharp;
break; return;
case "cli": case "cli":
_options.Kind = CppSharp.Generators.GeneratorKind.CLI; _options.Kind = CppSharp.Generators.GeneratorKind.CLI;
break; return;
} }
throw new NotSupportedException("Unknown generator kind: " + generator); throw new NotSupportedException("Unknown generator kind: " + generator);
@ -130,13 +149,13 @@ namespace CppSharp
{ {
case "win": case "win":
_options.Platform = TargetPlatform.Windows; _options.Platform = TargetPlatform.Windows;
break; return;
case "osx": case "osx":
_options.Platform = TargetPlatform.MacOS; _options.Platform = TargetPlatform.MacOS;
break; return;
case "linux": case "linux":
_options.Platform = TargetPlatform.Linux; _options.Platform = TargetPlatform.Linux;
break; return;
} }
throw new NotSupportedException("Unknown target platform: " + platform); throw new NotSupportedException("Unknown target platform: " + platform);
@ -148,10 +167,10 @@ namespace CppSharp
{ {
case "x86": case "x86":
_options.Architecture = TargetArchitecture.x86; _options.Architecture = TargetArchitecture.x86;
break; return;
case "x64": case "x64":
_options.Architecture = TargetArchitecture.x64; _options.Architecture = TargetArchitecture.x64;
break; return;
} }
throw new NotSupportedException("Unknown target architecture: " + architecture); throw new NotSupportedException("Unknown target architecture: " + architecture);
@ -159,17 +178,17 @@ namespace CppSharp
static void Main(string[] args) static void Main(string[] args)
{ {
ParseCommandLineArgs(args);
Generator gen = new Generator(_options);
try try
{ {
if (ParseCommandLineArgs(args) == false)
return;
Generator gen = new Generator(_options);
gen.Run(); gen.Run();
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Error: " + ex.ToString()); Console.WriteLine(ex.Message);
} }
} }
} }

16
src/CLI/Generator.cs

@ -98,20 +98,20 @@ namespace CppSharp
if (_triple.Contains("linux")) if (_triple.Contains("linux"))
SetupLinuxOptions(parserOptions); SetupLinuxOptions(parserOptions);
Console.WriteLine("\n\nAdding " + (_options.IncludeDirs.Count) + " include dirs\n\n");
foreach (String s in _options.IncludeDirs) foreach (String s in _options.IncludeDirs)
{
parserOptions.AddIncludeDirs(s); parserOptions.AddIncludeDirs(s);
Console.WriteLine("Add include: " + s);
}
foreach (String s in _options.LibraryDirs) foreach (String s in _options.LibraryDirs)
parserOptions.AddLibraryDirs(s); parserOptions.AddLibraryDirs(s);
foreach (String s in _options.Defines) foreach (KeyValuePair<String, String> d in _options.Defines)
parserOptions.AddDefines(s); {
if(d.Value == null || d.Value == String.Empty)
parserOptions.AddDefines(d.Key);
else
parserOptions.AddDefines(d.Key + "=" + d.Value);
}
options.OutputDir = _options.OutputDir; options.OutputDir = _options.OutputDir;
options.OutputNamespace = _options.OutputNamespace; options.OutputNamespace = _options.OutputNamespace;

7
src/CLI/Options.cs

@ -35,8 +35,8 @@ namespace CppSharp
private List<String> _libraries = new List<string>(); private List<String> _libraries = new List<string>();
public List<String> Libraries { get { return _libraries; } set { _libraries = value; } } public List<String> Libraries { get { return _libraries; } set { _libraries = value; } }
private List<String> _defines = new List<string>(); private Dictionary<String, String> _defines = new Dictionary<String, String>();
public List<String> Defines { get { return _defines; } set { _defines = value; } } public Dictionary<String, String> Defines { get { return _defines; } set { _defines = value; } }
private String _outputDir = ""; private String _outputDir = "";
public String OutputDir { get { return _outputDir; } set { _outputDir = value; } } public String OutputDir { get { return _outputDir; } set { _outputDir = value; } }
@ -62,9 +62,6 @@ namespace CppSharp
private GeneratorKind _kind = GeneratorKind.CSharp; private GeneratorKind _kind = GeneratorKind.CSharp;
public GeneratorKind Kind { get { return _kind; } set { _kind = value; } } public GeneratorKind Kind { get { return _kind; } set { _kind = value; } }
//private bool _verbose = false;
//public bool Verbose { get { return _verbose; } set { _verbose = value; } }
private bool _checkSymbols = false; private bool _checkSymbols = false;
public bool CheckSymbols { get { return _checkSymbols; } set { _checkSymbols = value; } } public bool CheckSymbols { get { return _checkSymbols; } set { _checkSymbols = value; } }

Loading…
Cancel
Save