Azavea.NijPredictivePolicing.ACSAlchemist.ImportJob.Load C# (CSharp) Method

Load() public method

Takes a collection of command line arguments, and parses them / loads them onto our properties. Will also accept a single filename, and treat that as if it were provided at the command line
public Load ( string args ) : bool
args string
return bool
        public bool Load(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < args.Length; i++)
            {
                sb.Append(args[i]).Append(' ');
            }
            string line = sb.ToString();
            this.ArgumentLine = line;
            //char delim = '-';
            char[] delims = new char[] { (char)45, (char)8211, (char)65533 }; //'-', '–', '?';
            int idx = FindDelimAfterWhitespace(line, 0, delims);     //int idx = line.IndexOf(delim);

            if (idx == -1)
            {
                //if we have arguments, but they didn't include flags, it's probably a file,
                //look for the file.
                if (File.Exists(args[0]))
                {
                    var lines = File.ReadAllLines(args[0]);
                    sb = new StringBuilder();
                    foreach (string chunk in lines)
                    {
                        if (chunk.StartsWith("#") || chunk.StartsWith("/"))
                            continue;

                        if (string.IsNullOrEmpty(chunk))
                            continue;

                        string tmpChunk = Utilities.TrimComments(chunk, '#');

                        sb.Append(tmpChunk.Trim()).Append(" ");
                    }
                    line = sb.ToString();
                }
                else
                {
                    _log.ErrorFormat("The arguments file you provided could not be read: {0}", args[0]);
                    return false;
                }
            }

            // iterate over our input, copying values to our properties

            var thisType = typeof(ImportJob);
            idx = FindDelimAfterWhitespace(line, 0, delims);     //int idx = line.IndexOf(delim);
            while (idx >= 0)
            {
                int nextSpace = line.IndexOf(' ', idx + 1);

                string flag = line.Substring(idx + 1, nextSpace - (idx + 1));
                string contents = string.Empty;

                idx += 1 + flag.Length;
                int end = FindDelimAfterWhitespace(line, idx, delims);   //int end = line.IndexOf(delim, idx);
                if (end == -1)
                {
                    end = line.Length;
                }
                if (end > idx)
                {
                    contents = line.Substring(idx, end - idx).Trim();
                    if (string.IsNullOrEmpty(contents))
                    {
                        contents = true.ToString();
                    }

                    idx = end;
                }
                idx = FindDelimAfterWhitespace(line, idx, delims);       //idx = line.IndexOf(delim, idx);

                // actually set the properties now
                for (int p = 0; p < Arguments.Length; p++)
                {
                    var arg = Arguments[p];
                    if (arg.Flag == flag)
                    {
                        var prop = thisType.GetProperty(arg.PropertyName);
                        object defval = (prop.PropertyType == typeof(AcsState)) ? (object)AcsState.None : null;
                        prop.SetValue(this, Utilities.GetAsType(prop.PropertyType, contents, defval), null);
                        break;
                    }
                }
            }
            return true;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Our main entry point for the command line app
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static int Main(string[] args)
        {
            Init();
            LoadConfigFile();
            ShowWelcomeScreen();
            ShowCopyrightAndLicense();


            ImportJob job = new ImportJob();

            if ((args != null) && (args.Length > 0))
            {
                _log.Debug("Loading arguments...");
                if (!job.Load(args))
                {
                    _log.Debug("Error while loading arguments. Exiting.");
                    return(-1);
                }

                if (!job.ExecuteJob())
                {
                    _log.Fatal("An error was encountered while performing the operation.  Please examine the log output and try again if necessary.");
                }
            }
            else
            {
                DisplayOptions();
            }

            //#if DEBUG
            //            _log.Debug("Done! Press ANY KEY to Quit");
            //            Console.ReadKey();
            //#endif
            return(0);
        }
All Usage Examples Of Azavea.NijPredictivePolicing.ACSAlchemist.ImportJob::Load