This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
BaseParameters.cs
66 lines (51 loc) · 2.26 KB
/
BaseParameters.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using NDesk.Options;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO.Abstractions;
using System.Linq;
namespace Pretzel.Logic.Commands
{
public sealed class BaseParameters
{
public OptionSet Options { get; private set; }
public string CommandName { get; private set; }
public bool Help { get; private set; }
public bool Debug { get; private set; }
public bool Safe { get; private set; }
[Export("SourcePath")]
public string Path { get; private set; }
[Export]
public IFileSystem FileSystem { get; private set; }
public List<string> CommandArgs { get; private set; }
private BaseParameters(string[] arguments, IFileSystem fileSystem)
{
Options = new OptionSet
{
{"help", "Display help mode", p => Help = true},
{"debug", "Enable debugging", p => Debug = true},
{ "safe", "Disable custom plugins", v => Safe = true },
{ "d|directory=", "[Obsolete, use --source instead] The path to site directory", p => Path = p },
{ "s|source=", "The path to the source site (default current directory)", p => Path = p}
};
FileSystem = fileSystem;
CommandName = arguments.Take(1).FirstOrDefault();
CommandArgs = Options.Parse(arguments.Skip(1));
SetPath(CommandArgs.FirstOrDefault());
}
public static BaseParameters Parse(string[] arguments, IFileSystem fileSystem)
{
return new BaseParameters(arguments, fileSystem);
}
private void SetPath(string firstArgument)
{
// take the first argument after the command
if (firstArgument != null && !firstArgument.StartsWith("-") && !firstArgument.StartsWith("/"))
{
Path = FileSystem.Path.IsPathRooted(firstArgument)
? firstArgument
: FileSystem.Path.Combine(FileSystem.Directory.GetCurrentDirectory(), firstArgument);
}
Path = string.IsNullOrWhiteSpace(Path) ? FileSystem.Directory.GetCurrentDirectory() : FileSystem.Path.GetFullPath(Path);
}
}
}