Exemple #1
0
 /// <summary>
 /// Instantaneously replaces the Options singleton instance with the data provided.
 /// </summary>
 /// <param name="io"></param>
 public static void ReplaceCurr(OptionsInformation io)
 {
     lock (padlock)
     {
         instance = io;
     }
 }
Exemple #2
0
 /// <summary>
 /// Will read options from a file into the given instance.
 /// </summary>
 public static void ReadJson(string fn, OptionsInformation optionsInformation)
 {
     try
     {
         var jsonStr = File.ReadAllText(fn);
         JsonConvert.PopulateObject(jsonStr, optionsInformation);
     }
     catch (Exception ex)
     {
         Log.Singleton.Error(ex, "When reading options JSON file");
     }
 }
Exemple #3
0
 /// <summary>
 /// Will save options to a file. Catches exceptions.
 /// </summary>
 public static void WriteJson(OptionsInformation optionsInformation, string filename)
 {
     // execute in-line, in order to represent to correct order to the human operator
     try
     {
         var jsonStr = JsonConvert.SerializeObject(optionsInformation, Formatting.Indented);
         File.WriteAllText(filename, jsonStr);
     }
     catch (Exception ex)
     {
         Log.Singleton.Error(ex, $"When writing options to a JSON file: {filename}");
     }
 }
Exemple #4
0
 public static void TryReadOptionsFile(string filename, OptionsInformation optionsInformation)
 {
     try
     {
         var optionsTxt   = File.ReadAllText(filename);
         var argsFromFile = optionsTxt.Split(
             new[] { '\r', '\n', '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
         OptionsInformation.ParseArgs(argsFromFile, optionsInformation);
     }
     catch (Exception ex)
     {
         Log.Singleton.Error(ex, "Reading options file: " + filename);
     }
 }
Exemple #5
0
        public static void ParseArgs(string[] args, OptionsInformation optionsInformation)
        {
            // This is a sweep line for plugin arguments.
            var pluginArgs = new List <string>();

            for (int index = 0; index < args.Length; index++)
            {
                var arg     = args[index].Trim().ToLower();
                var morearg = (args.Length - 1) - index;

                // flags
                if (arg == "-maximized")
                {
                    optionsInformation.WindowMaximized = true;
                    continue;
                }
                if (arg == "-noflyouts")
                {
                    optionsInformation.UseFlyovers = false;
                    continue;
                }
                if (arg == "-intbrowse")
                {
                    optionsInformation.InternalBrowser = true;
                    continue;
                }
                if (arg == "-twopass")
                {
                    optionsInformation.EclassTwoPass = true;
                    continue;
                }
                if (arg == "-indirect-load-save")
                {
                    optionsInformation.IndirectLoadSave = true;
                    continue;
                }
                if (arg == "-load-without-prompt")
                {
                    optionsInformation.LoadWithoutPrompt = true;
                    continue;
                }
                if (arg == "-show-id-as-iri")
                {
                    optionsInformation.ShowIdAsIri = true;
                    continue;
                }
                if (arg == "-verbose-connect")
                {
                    optionsInformation.VerboseConnect = true;
                    continue;
                }
                if (arg == "-stay-connected")
                {
                    optionsInformation.DefaultStayConnected = true;
                    continue;
                }

                // commands, which are executed on the fly ..
                if (arg == "-read-json" && morearg > 0)
                {
                    // parse
                    var fn = System.IO.Path.GetFullPath(args[index + 1]);
                    index++;

                    // execute in-line, in order to represent to correct order to the human operator
                    OptionsInformation.ReadJson(fn, optionsInformation);

                    // next arg
                    continue;
                }
                if (arg == "-write-json" && morearg > 0)
                {
                    // parse
                    var filename = System.IO.Path.GetFullPath(args[index + 1]);
                    index++;

                    // do
                    OptionsInformation.WriteJson(optionsInformation, filename);

                    // next arg
                    continue;
                }

                // options
                if (arg == "-left" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.WindowLeft = i;
                    }
                    index++;
                    continue;
                }
                if (arg == "-top" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.WindowTop = i;
                    }
                    index++;
                    continue;
                }
                if (arg == "-width" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.WindowWidth = i;
                    }
                    index++;
                    continue;
                }
                if (arg == "-height" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.WindowHeight = i;
                    }
                    index++;
                    continue;
                }

                if (arg == "-id-aas" && morearg > 0)
                {
                    optionsInformation.TemplateIdAas = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-id-asset" && morearg > 0)
                {
                    optionsInformation.TemplateIdAsset = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-id-sm-template" && morearg > 0)
                {
                    optionsInformation.TemplateIdSubmodelTemplate = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-id-sm-instance" && morearg > 0)
                {
                    optionsInformation.TemplateIdSubmodelInstance = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-id-cd" && morearg > 0)
                {
                    optionsInformation.TemplateIdConceptDescription = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-eclass" && morearg > 0)
                {
                    optionsInformation.EclassDir = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-dict-import-dir" && morearg > 0)
                {
                    optionsInformation.DictImportDir = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-qualifiers" && morearg > 0)
                {
                    optionsInformation.QualifiersFile = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-logo" && morearg > 0)
                {
                    optionsInformation.LogoFile = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-aasxrepo" && morearg > 0)
                {
                    optionsInformation.AasxRepositoryFn = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-contenthome" && morearg > 0)
                {
                    optionsInformation.ContentHome = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-splash" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.SplashTime = i;
                    }
                    index++;
                    continue;
                }
                if (arg == "-options" && morearg > 0)
                {
                    string pathToOptions = args[index + 1];
                    Log.Singleton.Info(
                        $"Parsing options from a non-default options file: {pathToOptions}");
                    var fullFilename = System.IO.Path.GetFullPath(pathToOptions);
                    OptionsInformation.TryReadOptionsFile(fullFilename, optionsInformation);

                    index++;
                    continue;
                }
                if (arg == "-backupdir" && morearg > 0)
                {
                    optionsInformation.BackupDir = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-resthost" && morearg > 0)
                {
                    optionsInformation.RestServerHost = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-restport" && morearg > 0)
                {
                    optionsInformation.RestServerPort = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-rem" && morearg > 0)
                {
                    // Add one argument to the plugin list
                    optionsInformation.Remarks.Add(args[index + 1]);
                    index++;
                    continue;
                }
                if (arg == "-write-all-json" && morearg > 0)
                {
                    // will be executed very late!
                    optionsInformation.WriteDefaultOptionsFN = args[index + 1];
                    index++;
                    continue;
                }
                if (arg == "-plugin-dir" && morearg > 0)
                {
                    optionsInformation.PluginDir = args[index + 1];
                    index++;
                    continue;
                }

                // Sweep-line options for plugins and DLL path
                if (arg == "-p" && morearg > 0)
                {
                    // Add exactly one following argument to the sweep line of plugin arguments
                    pluginArgs.Add(args[index + 1]);
                    index += 1;
                    continue;
                }
                if (arg == "-dll" && morearg > 0)
                {
                    // Process and reset the sweep line
                    optionsInformation.PluginDll.Add(
                        new PluginDllInfo(args[index + 1], pluginArgs.ToArray()));
                    pluginArgs.Clear();
                    index++;
                    continue;
                }
                if (arg == "-update-period" && morearg > 0)
                {
                    if (Int32.TryParse(args[index + 1], out int i))
                    {
                        optionsInformation.DefaultUpdatePeriod = i;
                    }
                    index++;
                    continue;
                }

                // Colors
                {
                    var found = false;
                    for (int i = 0; i < 10; i++)
                    {
                        if (arg == $"-c{i:0}" && morearg > 0)
                        {
                            // ReSharper disable PossibleNullReferenceException
                            try
                            {
                                var c = AnyUiColor.FromString(args[index + 1].Trim());
                                optionsInformation.AccentColors.Add((ColorNames)i, c);
                            }
                            catch (Exception ex)
                            {
                                AdminShellNS.LogInternally.That.SilentlyIgnoredError(ex);
                            }
                            // ReSharper enable PossibleNullReferenceException

                            index++;
                            found = true;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                }

                // if come to this point and obviously not an option, take this as load argument
                // allow for more options to come (motivation: allow "-write-json options.json" to be the last argument)
                if (!arg.StartsWith("-"))
                {
                    if (System.IO.File.Exists(args[index]))
                    {
                        optionsInformation.AasxToLoad = args[index];
                    }
                }
            }
        }
        public static string ReportOptions(ReportOptionsFormat fmt,
                                           OptionsInformation options = null)
        {
            var sb = new StringBuilder();

            //
            // small lambdas
            //

            Action appendTableHeader = () =>
            {
                sb.AppendLine($"| {"JSON option",-35} | {"Command line",-20} " +
                              $"| {"Argument",-20} | Description |");
                sb.AppendLine($"|-{new String('-', 35)}-|-{new String('-', 20)}-" +
                              $"|-{new String('-', 20)}-|-------------|");
            };

            Action <string, string, string, string> appendTableRow = (json, cmd, arg, description) =>
            {
                sb.AppendLine($"| {MdEsc(json),-35} | {MdEsc(cmd),-20} " +
                              $"| {MdEsc(arg),-20} | {MdEsc(description)} |");
            };

            //
            // Regular options
            //

            if (fmt == ReportOptionsFormat.Markdown)
            {
                sb.AppendLine("# Regular options for JSON and command line");
                sb.AppendLine();
                sb.AppendLine(AdminShellUtil.CleanHereStringWithNewlines(
                                  @"The following options can be used either directly in the command line of the 
                    exectable or in a JSON-file for configuration (via the ""-read-json"" option)."));
                sb.AppendLine();
            }

            var fields = typeof(OptionsInformation).GetFields(BindingFlags.Public | BindingFlags.Instance);
            var first  = true;

            foreach (var fi in fields)
            {
                foreach (var fia in fi.GetCustomAttributes(typeof(OptionDescription), true))
                {
                    if (fia is OptionDescription fiaod)
                    {
                        if (fmt == ReportOptionsFormat.Markdown)
                        {
                            if (first)
                            {
                                first = false;
                                appendTableHeader();
                            }
                            appendTableRow(fi.Name, fiaod.Cmd, fiaod.Arg, fiaod.Description);
                        }
                    }
                }
            }

            sb.AppendLine();

            //
            // Special options
            //

            if (fmt == ReportOptionsFormat.Markdown)
            {
                sb.AppendLine("# Special options for JSON and command line");
                sb.AppendLine();
                sb.AppendLine(AdminShellUtil.CleanHereStringWithNewlines(
                                  @"The following options are also be provided."));
                sb.AppendLine();

                appendTableHeader();
                appendTableRow("", "-read-json", "<path>", "Reads a JSON formatted options file.");
                appendTableRow("", "-write-json", "<path>", "Writes the currently loaded options " +
                               "into a JSON formatted file.");
            }

            sb.AppendLine();

            //
            // Report current options?
            //

            if (options != null)
            {
                var jsonStr = JsonConvert.SerializeObject(options, Formatting.Indented);

                if (fmt == ReportOptionsFormat.Markdown)
                {
                    sb.AppendLine("# Current options");
                    sb.AppendLine();
                    sb.AppendLine(AdminShellUtil.CleanHereStringWithNewlines(
                                      @"The following options are currently loaded or set by default."));
                    sb.AppendLine();

                    sb.AppendLine("```");
                    sb.AppendLine(jsonStr);
                    sb.AppendLine("```");

                    sb.AppendLine();
                }
            }

            //
            // End of report
            //

            return(sb.ToString());
        }