Last active
July 20, 2018 08:54
-
-
Save zekroTJA/ee2d9b49038ffddb7c048a6bb36ace79 to your computer and use it in GitHub Desktop.
Simple argument pasrer class for C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright 2018 zekro Development | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the "Software"), | |
| // to deal in the Software without restriction, including without limitation | |
| // the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| // and/or sell copies of the Software, and to permit persons to whom the | |
| // Software is furnished to do so, subject to the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be included | |
| // in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
| // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| // IN THE SOFTWARE. | |
| /// <summary> | |
| /// ArgumentParser - A simple class to parse start arguments | |
| /// by key-value-principle. | |
| /// </summary> | |
| class ArgumentParser | |
| { | |
| private List<string> _args; | |
| /// <summary> | |
| /// Cretae instance of ArgumentParser. | |
| /// </summary> | |
| /// <param name="args">Start arguments</param> | |
| public ArgumentParser(string[] args) | |
| { | |
| _args = new List<string>(args); | |
| } | |
| /// <summary> | |
| /// Get the first position of the first found key in the arguments list. | |
| /// Returns -1 if key is not existent. | |
| /// </summary> | |
| /// <param name="keys">Key(s) to get positions of.</param> | |
| /// <returns>Position or -1 if not existent.</returns> | |
| public int GetKeyPos(params string[] keys) | |
| { | |
| foreach (string key in keys) | |
| { | |
| int currInd = _args.IndexOf(key); | |
| if (currInd > -1) | |
| return currInd; | |
| } | |
| return -1; | |
| } | |
| /// <summary> | |
| /// Check if one of the passed keys are existent in the argument list. | |
| /// </summary> | |
| /// <param name="keys">Key(s) to check for.</param> | |
| /// <returns>True if one of the given keys is existent.</returns> | |
| public bool CheckForKey(params string[] keys) | |
| { | |
| return (GetKeyPos(keys) > -1); | |
| } | |
| /// <summary> | |
| /// Get the specified value following the first found key in the arguments | |
| /// list if the key is existent. | |
| /// </summary> | |
| /// <param name="keys">Key(s) to search for.</param> | |
| /// <returns>Value following the key.</returns> | |
| public string GetValueByKey(params string[] keys) | |
| { | |
| int pos = GetKeyPos(keys); | |
| if (pos > -1 && _args.Count > pos + 1) | |
| return _args[pos + 1]; | |
| else | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment