// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System.Collections.Generic; using System.Text.RegularExpressions; namespace GeoXY2LatLong { /// /// CommandLineArgumentParser class /// public class CommandLineArgumentParser { // Variables private readonly Dictionary parameters; // Constructor public CommandLineArgumentParser(IEnumerable arguments) { parameters = new Dictionary(); var splitter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled); var remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled); string parameter = null; string[] parts; // Valid parameters forms: // {-,/,--}param{ ,=,:}((",')value(",')) // Examples: -param1 value1 --param2 /param3:"Test-:-work" /param4=happy -param5 '--=nice=--' foreach (string argument in arguments) { // Look for new parameters (-,/ or --) and a possible enclosed value (=,:) parts = splitter.Split(argument, 3); switch (parts.Length) { // Found a value (for the last parameter found (space separator)) case 1: if (parameter != null) { if (!parameters.ContainsKey(parameter)) { parts[0] = remover.Replace(parts[0], "$1"); parameters.Add(parameter, parts[0]); } parameter = null; } // else Error: no parameter waiting for a value (skipped) break; // Found just a parameter case 2: // The last parameter is still waiting. With no value, set it to true. if (parameter != null) { if (!parameters.ContainsKey(parameter)) { parameters.Add(parameter, "true"); } } parameter = parts[1]; break; // Parameter with enclosed value case 3: // The last parameter is still waiting. With no value, set it to true. if (parameter != null) { if (!parameters.ContainsKey(parameter)) { parameters.Add(parameter, "true"); } } parameter = parts[1]; // Remove possible enclosing characters (",') if (!parameters.ContainsKey(parameter)) { parts[2] = remover.Replace(parts[2], "$1"); parameters.Add(parameter, parts[2]); } parameter = null; break; } } // In case a parameter is still waiting if (parameter != null) { if (!parameters.ContainsKey(parameter)) { parameters.Add(parameter, "true"); } } } // Retrieve a parameter value if it exists public string this[string param] { get { return parameters[param]; } } public int Count { get { return parameters.Count; } } public bool Contains(string key) { return parameters.ContainsKey(key); } } }