-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathToggleParser.cs
More file actions
27 lines (21 loc) · 1.06 KB
/
Copy pathToggleParser.cs
File metadata and controls
27 lines (21 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
namespace SmartSystemMenu
{
class ToggleParser
{
private readonly Dictionary<string, string> toggles;
public ToggleParser(string[] args)
{
toggles =
args.Zip(args.Skip(1).Concat(new[] { string.Empty }), (first, second) => new { first, second })
.Where(pair => IsToggle(pair.first))
.ToDictionary(pair => RemovePrefix(pair.first).ToLowerInvariant(), g => IsToggle(g.second) ? string.Empty : g.second);
}
private static string RemovePrefix(string toggle) => new string(toggle.SkipWhile(c => c == '-').ToArray());
private static bool IsToggle(string arg) => arg.StartsWith("-", StringComparison.InvariantCulture);
public bool HasToggle(string toggle) => toggles.ContainsKey(toggle.ToLowerInvariant());
public string GetToggleValueOrDefault(string toggle, string defaultValue) => toggles.TryGetValue(toggle.ToLowerInvariant(), out var value) ? value : defaultValue;
}
}