-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathParameters.cs
85 lines (70 loc) · 1.86 KB
/
Parameters.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
namespace WinHaste
{
internal class Parameters
{
public enum ParseResult
{
Success,
BadUrl,
BadFilename,
BadArgs
}
private const string DEFAULT_URL = "https://hastebin.com";
private const string USAGE =
@"Usage: WinHaste.exe [service url] [file to haste]
Defaults:
[service url] = https://hastebin.com
When [file to haste] is omitted, haste.exe reads from
standard input until an end-of-file character is read.";
private readonly string[] _args;
public Parameters(string[] args)
{
_args = args;
}
public string Usage => USAGE;
public string Url { get; private set; }
public string FilePath { get; private set; }
public ParseResult Parse()
{
bool pathProvided = false;
if (_args.Length == 0)
{
Url = DEFAULT_URL;
}
else if (_args.Length == 1)
{
Url = ParseUrl(_args[0]);
if (Url == null)
{
Url = DEFAULT_URL;
FilePath = _args[0];
pathProvided = true;
}
}
else if (_args.Length == 2)
{
Url = ParseUrl(_args[0]);
FilePath = _args[1];
pathProvided = true;
}
else
{
return ParseResult.BadArgs;
}
return Url == null ? ParseResult.BadUrl
: !File.Exists(FilePath) && pathProvided
? ParseResult.BadFilename
: ParseResult.Success;
}
private string ParseUrl(string candidate)
{
if (!Uri.TryCreate(candidate, UriKind.Absolute, out var tmpUri))
return null;
if (tmpUri.Scheme != "http" && tmpUri.Scheme != "https")
return null;
return candidate.TrimEnd('/');
}
}
}