/// <summary> /// Constructor for KScriptContainer /// </summary> /// <param name="prop">Properties class to use for KScript parsing.</param> /// <param name="parser">The parser object to use.</param> public KScriptContainer(KScriptProperties prop, KScriptParser parser) { _random = new Random(); defs = new Dictionary <string, def>(); arrays = new Dictionary <string, List <string> >(); Properties = prop; Parser = parser; AllowExecution = true; StringHandler = new KScriptStringHandler(this); LoadedKScriptObjects = new Dictionary <string, Type>(); ObjectStorageContainer = new KScriptObjectStorageContainer(); LoadedVariableFunctions = new Dictionary <string, Type>(); LoadedParserHandlers = new Dictionary <string, Type>(); LoadedOperatorHandlers = new Dictionary <string, Type>(); KScriptArrayContainer = new KScriptArrayContainer(); }
public void Write(KScriptParser parser) { Out("About KScript"); Out("-----------------------------------------------"); Out($"Version: {Assembly.GetExecutingAssembly().GetName().Version.ToString()}"); Out("Supported Commands:"); IEnumerable <Type> q = from t in Assembly.GetExecutingAssembly().GetTypes() where t.IsClass && typeof(KScriptObject).IsAssignableFrom(t) && t.Namespace.StartsWith(KScript.Global.GlobalIdentifiers.ASSEMBLY_PATH) select t; IndentedTextWriter indentedTextWriter = new IndentedTextWriter(Console.Out) { Indent = 2 }; foreach (Type t in q.ToList()) { bool HideClass = t.GetCustomAttributes <KScriptHideObject>().Any(); bool HasNoInnerObjects = t.GetCustomAttributes <KScriptNoInnerObjects>().Any(); if (!HideClass) { indentedTextWriter.Indent = 0; indentedTextWriter.WriteLine(); for (int i = 0; i < Console.WindowWidth; i++) { indentedTextWriter.Write("="); } indentedTextWriter.WriteLine(); if (!HasNoInnerObjects) { indentedTextWriter.Write("< " + t.Name + " > ... </ " + t.Name + " >\n"); } else { indentedTextWriter.Write("< " + t.Name + " />\n"); } indentedTextWriter.Indent = 1; indentedTextWriter.WriteLine(); indentedTextWriter.WriteLine("[ Usage ] "); indentedTextWriter.Indent = 2; indentedTextWriter.WriteLine("Object Contents: " + (HasNoInnerObjects ? "Does not require inner elements or content." : "Inner elements or content are required.") + "\n"); indentedTextWriter.WriteLine(string.Format(parser.GetScriptObject(t).UsageInformation())); indentedTextWriter.WriteLine(); IEnumerable <PropertyInfo> properties = t.GetProperties().Where(p => p.CanWrite); int index = 0; if (properties.Any(p => p.GetCustomAttributes <KScriptProperty>().Any())) { indentedTextWriter.Indent = 1; indentedTextWriter.WriteLine("[ Arguments ]"); } foreach (PropertyInfo p in properties) { IEnumerable <KScriptProperty> Properties = p.GetCustomAttributes <KScriptProperty>(false); foreach (KScriptProperty prop in Properties) { indentedTextWriter.Indent = 2; indentedTextWriter.WriteLine("[" + ++index + "] - " + p.Name + (prop.IsRequired() ? " (required)" : "")); indentedTextWriter.Indent = 3; indentedTextWriter.WriteLine(prop.ToString()); IEnumerable <KScriptExample> Examples = p.GetCustomAttributes <KScriptExample>(false); int example_count = 0; if (Examples.Any()) { indentedTextWriter.WriteLine("[ Examples ]"); foreach (KScriptExample example in Examples) { indentedTextWriter.Indent = 4; indentedTextWriter.WriteLine(++example_count + " - " + example.ToString()); } } KScriptAcceptedOptions Accepted_Value = p.GetCustomAttribute <KScriptAcceptedOptions>(false); if (Accepted_Value != null) { indentedTextWriter.Indent = 3; indentedTextWriter.WriteLine("[ Accepted Values ]"); int val_count = 0; foreach (string item in Accepted_Value.GetValues()) { indentedTextWriter.Indent = 4; indentedTextWriter.WriteLine(++val_count + " - " + item); } } } } indentedTextWriter.WriteLine(); } } indentedTextWriter.Dispose(); Out(); Out(); }
static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.UTF8; if (args.Length > 0) { bool hasColor = args.Any(i => i.ToLower() == "-colourful"); if (hasColor) { SetAppearances(); } string culture = "auto"; bool quiet = args.Any(i => i.ToLower() == "-s"); bool clearAfterInput = args.Any(i => i.ToLower() == "-cai"); bool adminPriv = args.Any(i => i.ToLower() == "-admin"); bool generateGuid = args.Any(i => i.ToLower() == "-guid"); bool root = args.Any(i => i.ToLower() == "-root"); bool allCommands = args.Any(i => i.ToLower() == "-all"); bool has_lang = args.Any(i => i.ToLower().StartsWith("-lang") && i.ToLower().Contains("=")); string path = args[0]; if (adminPriv) { RestartWithAdminPriviledges(args); } if (root) { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); Process.Start(Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path))); } if (has_lang) { string lang = args.FirstOrDefault(i => i.ToLower().StartsWith("-lang") && i.ToLower().Contains("=")); if (lang != null) { culture = lang.Split('=')[1]; } } if (!generateGuid && !root) { if (!args[0].ToString().StartsWith("-")) { if (!File.Exists(args[0])) { string[] files = Directory.GetFiles(Directory.GetCurrentDirectory()); string filePath = files.Select(u => Path.GetFileName(u)).FirstOrDefault().ToLower(); if (filePath != null && filePath == args[0].ToLower()) { path = filePath; } else { Console.WriteLine(string.Format("File does not exist - '{0}'...", args[0])); throw new KScriptException("File does not exist."); } } else { path = args[0]; } } } if (generateGuid) { Console.WriteLine("GUID: " + Guid.NewGuid().ToString()); return; } if (!root && !generateGuid) { KScriptParser parser = new KScriptParser(path); parser.Properties.Quiet = quiet; parser.Properties.ClearAfterInput = clearAfterInput; parser.CustomArguments = args.Skip(1).ToArray(); parser.Properties.Language = culture; parser.Properties.PrintInfo = allCommands; parser.Parse(); if (parser.Properties.WaitOnFinish) { if (HasAnyChildProcesses()) { if (!parser.Properties.Quiet) { Console.WriteLine("Awaiting for child processes to finish..."); } Console.ReadKey(); return; } if (!parser.Properties.Quiet) { Console.WriteLine("Press any key to close..."); } Console.ReadKey(); } } } }