Exemple #1
0
        /// <summary>
        /// Infers application options based on the command-line arguments.
        /// </summary>
        /// <param name="exePath">path to AasxPackageExplorer.exe</param>
        /// <param name="args">command-line arguments</param>
        /// <returns>inferred options</returns>
        public static OptionsInformation InferOptions(string exePath, string[] args)
        {
            var optionsInformation = new OptionsInformation();

            // Load the default command-line options from a file with a conventional file name

            var pathToDefaultOptions = System.IO.Path.Combine(
                System.IO.Path.GetDirectoryName(exePath),
                System.IO.Path.GetFileNameWithoutExtension(exePath) + ".options.json");

            Log.Singleton.Info(
                "The default options are expected in the JSON file: {0}", pathToDefaultOptions);
            if (File.Exists(pathToDefaultOptions))
            {
                Log.Singleton.Info(
                    "Loading the default options from: {0}", pathToDefaultOptions);
                OptionsInformation.ReadJson(pathToDefaultOptions, optionsInformation);
            }
            else
            {
                Log.Singleton.Info(
                    "The JSON file with the default options does not exist;" +
                    "no default options were loaded: {0}", pathToDefaultOptions);
            }

            // Cover the special case for having a single positional command-line option

            if (args.Length == 1 && !args[0].StartsWith("-"))
            {
                string directAasx = args[0];
                Log.Singleton.Info("Direct request to load AASX {0} ..", directAasx);
                optionsInformation.AasxToLoad = directAasx;
            }

            // Parse options from the command-line and execute the directives on the fly (such as parsing and
            // overruling given in the additional option files, *e.g.*, through "-read-json" and "-options")

            Log.Singleton.Info($"Parsing {args.Length} command-line option(s)...");

            for (var i = 0; i < args.Length; i++)
            {
                Log.Singleton.Info($"Command-line option: {i}: {args[i]}");
            }

            OptionsInformation.ParseArgs(args, optionsInformation);

            return(optionsInformation);
        }
Exemple #2
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;
                }

                // 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 == "-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];
                    AasxPackageExplorer.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;
                }

                // Colors
                {
                    var found = false;
                    for (int i = 0; i < 10; i++)
                    {
                        if (arg == $"-c{i:0}" && morearg > 0)
                        {
                            // ReSharper disable PossibleNullReferenceException
                            try
                            {
                                var c = (Color)ColorConverter.ConvertFromString(args[index + 1]);
                                optionsInformation.AccentColors.Add(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];
                    }
                }
            }
        }