Exemplo n.º 1
0
        public static FlexibleOptions Initialize(string[] args, bool thrownOnError)
        {
            DefaultProgramInitialization();

            InitializeLog();

            ProgramOptions = CheckCommandLineParams(args, thrownOnError);

            if (ProgramOptions.Get <bool> ("help", false))
            {
                show_help("");
                CloseApplication(0, true);
            }

            // display program initialization header
            if (!Console.IsOutputRedirected)
            {
                ConsoleUtils.DisplayHeader(
                    typeof(ConsoleUtils).Namespace.Replace(".SimpleHelpers", ""),
                    "options: " + (ProgramOptions == null ? "none" : "\n#    " + String.Join("\n#    ", ProgramOptions.Options.Select(i => i.Key + "=" + i.Value))));
            }

            return(ProgramOptions);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks the command line params.<para/>
        /// arguments format: key=value or --key value
        /// </summary>
        /// <param name="args">The args.</param>
        internal static FlexibleOptions CheckCommandLineParams(string[] args, bool thrownOnError)
        {
            FlexibleOptions mergedOptions         = null;
            FlexibleOptions argsOptions           = null;
            FlexibleOptions localOptions          = new FlexibleOptions();
            FlexibleOptions externalLoadedOptions = null;

            try
            {
                // parse local configuration file
                // display the options listed in the configuration file
                try
                {
                    var appSettings = System.Configuration.ConfigurationManager.AppSettings;
                    foreach (var k in appSettings.AllKeys)
                    {
                        localOptions.Set(k, appSettings[k]);
                    }
                }

                catch (Exception appSettingsEx)
                {
                    if (thrownOnError)
                    {
                        throw;
                    }
                    LogManager.GetCurrentClassLogger().Warn(appSettingsEx);
                }

                // parse console arguments
                // parse arguments like: key=value
                argsOptions = ParseCommandLineArguments(args);

                // merge arguments with app.config options. Priority: arguments > app.config
                mergedOptions = FlexibleOptions.Merge(localOptions, argsOptions);
                // adjust alias for web hosted configuration file
                if (String.IsNullOrEmpty(mergedOptions.Get("config")))
                {
                    mergedOptions.Set("config", mergedOptions.Get("S3ConfigurationPath", mergedOptions.Get("webConfigurationFile")));
                }

                // load and parse web hosted configuration file (priority order: argsOptions > localOptions)
                string externalConfigFile = mergedOptions.Get("config", "");
                bool   configAbortOnError = mergedOptions.Get("configAbortOnError", true);
                if (!String.IsNullOrWhiteSpace(externalConfigFile))
                {
                    foreach (var file in externalConfigFile.Trim(' ', '\'', '"', '[', ']').Split(',', ';'))
                    {
                        LogManager.GetCurrentClassLogger().Info("Loading configuration file from {0} ...", externalConfigFile);
                        externalLoadedOptions = FlexibleOptions.Merge(externalLoadedOptions, LoadExtenalConfigurationFile(file.Trim(' ', '\'', '"'), configAbortOnError));
                    }
                }
            }
            catch (Exception ex)
            {
                if (thrownOnError)
                {
                    throw;
                }
                LogManager.GetCurrentClassLogger().Error(ex);
            }

            // merge options with the following priority:
            // 1. console arguments
            // 2. web configuration file
            // 3. local configuration file (app.config or web.config)
            mergedOptions = FlexibleOptions.Merge(mergedOptions, externalLoadedOptions, argsOptions);

            // reinitialize log options if different from local configuration file
            InitializeLog(mergedOptions.Get("logFilename"), mergedOptions.Get("logLevel", "Info"));

            // return final merged options
            ProgramOptions = mergedOptions;
            return(mergedOptions);
        }