Summary

Class:WireMock.Net.StandAlone.SimpleCommandLineParser
Assembly:WireMock.Net.StandAlone
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net.StandAlone\SimpleCommandLineParser.cs
Covered lines:66
Uncovered lines:6
Coverable lines:72
Total lines:115
Line coverage:91.6%
Branch coverage:85%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
Parse(...)000.9330.938
Contains(...)0010
GetValues(...)0000
GetValue(...)0011
GetBoolValue(...)0010
GetIntValue(...)0010
GetStringValue(...)0010

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net.StandAlone\SimpleCommandLineParser.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4
 5namespace WireMock.Net.StandAlone
 6{
 7    // Based on http://blog.gauffin.org/2014/12/simple-command-line-parser/
 8    internal class SimpleCommandLineParser
 9    {
 10        private const string Sigil = "--";
 11        private const string SigilAzureServiceFabric = "'--";
 12
 13        private enum SigilType
 14        {
 15            Normal,
 16            AzureServiceFabric
 17        }
 18
 4719        private IDictionary<string, string[]> Arguments { get; } = new Dictionary<string, string[]>();
 20
 21        public void Parse(string[] args)
 522        {
 523            SigilType sigil = SigilType.Normal;
 524            string currentName = null;
 25
 526            var values = new List<string>();
 6727            foreach (string arg in args)
 2628            {
 2629                if (arg.StartsWith(Sigil))
 730                {
 731                    sigil = SigilType.Normal;
 32
 733                    if (!string.IsNullOrEmpty(currentName))
 634                    {
 635                        Arguments[currentName] = values.ToArray();
 636                    }
 37
 738                    values.Clear();
 739                    currentName = arg.Substring(Sigil.Length);
 740                }
 41                // Azure Service Fabric passes the command line parameter surrounded with single quotes. (https://github
 1942                else if (arg.StartsWith(SigilAzureServiceFabric))
 643                {
 644                    sigil = SigilType.AzureServiceFabric;
 45
 646                    if (!string.IsNullOrEmpty(currentName))
 247                    {
 248                        Arguments[currentName] = values.ToArray();
 249                    }
 50
 651                    values.Clear();
 652                    currentName = arg.Substring(SigilAzureServiceFabric.Length);
 653                }
 1354                else if (string.IsNullOrEmpty(currentName))
 055                {
 056                    Arguments[arg] = new string[0];
 057                }
 58                else
 1359                {
 1360                    if (sigil == SigilType.Normal)
 761                    {
 762                        values.Add(arg);
 763                    }
 64                    else
 665                    {
 666                        values.Add(arg.Substring(0, arg.Length - 1));
 667                    }
 1368                }
 2669            }
 70
 571            if (!string.IsNullOrEmpty(currentName))
 572            {
 573                Arguments[currentName] = values.ToArray();
 574            }
 575        }
 76
 77        public bool Contains(string name)
 1678        {
 1679            return Arguments.ContainsKey(name);
 1680        }
 81
 82        public string[] GetValues(string name, string[] defaultValue = null)
 083        {
 084            return Contains(name) ? Arguments[name] : defaultValue;
 085        }
 86
 87        public T GetValue<T>(string name, Func<string[], T> func, T defaultValue = default(T))
 1688        {
 1689            return Contains(name) ? func(Arguments[name]) : defaultValue;
 1690        }
 91
 92        public bool GetBoolValue(string name, bool defaultValue = false)
 393        {
 394            return GetValue(name, values =>
 595            {
 596                string value = values.FirstOrDefault();
 597                return !string.IsNullOrEmpty(value) ? bool.Parse(value) : defaultValue;
 598            }, defaultValue);
 399        }
 100
 101        public int? GetIntValue(string name, int? defaultValue = null)
 4102        {
 4103            return GetValue(name, values =>
 6104            {
 6105                string value = values.FirstOrDefault();
 6106                return !string.IsNullOrEmpty(value) ? int.Parse(value) : defaultValue;
 6107            }, defaultValue);
 4108        }
 109
 110        public string GetStringValue(string name, string defaultValue = null)
 9111        {
 18112            return GetValue(name, values => values.FirstOrDefault() ?? defaultValue, defaultValue);
 9113        }
 114    }
 115}