Summary

Class:WireMock.Net.StandAlone.SimpleCommandLineParser
Assembly:WireMock.Net.StandAlone
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net.StandAlone\SimpleCommandLineParser.cs
Covered lines:0
Uncovered lines:54
Coverable lines:54
Total lines:83
Line coverage:0%
Branch coverage:0%

Metrics

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

File(s)

C:\Users\azureuser\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    {
 010        private IDictionary<string, string[]> Arguments { get; } = new Dictionary<string, string[]>();
 11
 12        public void Parse(string[] args)
 013        {
 014            string currentName = null;
 15
 016            var values = new List<string>();
 017            foreach (string arg in args)
 018            {
 019                if (arg.StartsWith("--"))
 020                {
 021                    if (!string.IsNullOrEmpty(currentName))
 022                    {
 023                        Arguments[currentName] = values.ToArray();
 024                    }
 25
 026                    values.Clear();
 027                    currentName = arg.Substring(2);
 028                }
 029                else if (string.IsNullOrEmpty(currentName))
 030                {
 031                    Arguments[arg] = new string[0];
 032                }
 33                else
 034                {
 035                    values.Add(arg);
 036                }
 037            }
 38
 039            if (!string.IsNullOrEmpty(currentName))
 040            {
 041                Arguments[currentName] = values.ToArray();
 042            }
 043        }
 44
 45        public bool Contains(string name)
 046        {
 047            return Arguments.ContainsKey(name);
 048        }
 49
 50        public string[] GetValues(string name, string[] defaultValue = null)
 051        {
 052            return Contains(name) ? Arguments[name] : defaultValue;
 053        }
 54
 55        public T GetValue<T>(string name, Func<string[], T> func, T defaultValue = default(T))
 056        {
 057            return Contains(name) ? func(Arguments[name]) : defaultValue;
 058        }
 59
 60        public bool GetBoolValue(string name, bool defaultValue = false)
 061        {
 062            return GetValue(name, values =>
 063            {
 064                string value = values.FirstOrDefault();
 065                return !string.IsNullOrEmpty(value) ? bool.Parse(value) : defaultValue;
 066            }, defaultValue);
 067        }
 68
 69        public int? GetIntValue(string name, int? defaultValue = null)
 070        {
 071            return GetValue(name, values =>
 072            {
 073                string value = values.FirstOrDefault();
 074                return !string.IsNullOrEmpty(value) ? int.Parse(value) : defaultValue;
 075            }, defaultValue);
 076        }
 77
 78        public string GetStringValue(string name, string defaultValue = null)
 079        {
 080            return GetValue(name, values => values.FirstOrDefault() ?? defaultValue, defaultValue);
 081        }
 82    }
 83}