-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialFoldersCommand.cs
52 lines (48 loc) · 1.99 KB
/
SpecialFoldersCommand.cs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Text.RegularExpressions;
using McMaster.Extensions.CommandLineUtils;
namespace MrBogomips.Utils.DotNetLocalInfo
{
[Command(
Description="Show .NET Special Folders Path",
ExtendedHelpText="Enumerates the actual values of the Environment.SpecialFolder Enum")]
public class SpecialFoldersCommand
{
private IConsole Console {get; set;}
[Option("-f|--filter <REGEX>", Description = "Filter special folder names")]
public string Filter {get;}
[Option("--all", Description="Show all special folders including those without a value")]
public bool All {get;}
[Option("--name <SPECIAL_FOLDER_NAME>", Description="Retrieve the value of the special folder name")]
public string Name {get;}
public void OnExecute(IConsole console) {
Console = console;
if (string.IsNullOrEmpty(Name))
EnumerateFolders();
else
GetSpecialFolderValue();
}
private void GetSpecialFolderValue() {
try {
var specialFolder = Enum.Parse<Environment.SpecialFolder>(Name);
var value = Environment.GetFolderPath(specialFolder);
Console.WriteLine(value);
} catch (Exception) {
Console.Error.WriteLine($"The name '{Name}' is not a valid special forlder name");
}
}
private void EnumerateFolders() {
Regex reFilter = null;
if (!string.IsNullOrEmpty(Filter)) {
reFilter = new Regex(Filter);
}
foreach (var i in Enum.GetValues<Environment.SpecialFolder>())
{
var name = Enum.GetName<Environment.SpecialFolder>(i);
if (reFilter != null && !reFilter.IsMatch(name)) continue;
var value = Environment.GetFolderPath(i);
if (All || !string.IsNullOrEmpty(value)) Console.WriteLine($"{name}: {value}");
}
}
}
}