forked from ajryan/WinHaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parameters.cs
73 lines (57 loc) · 1.41 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
using System;
using System.IO;
namespace WinHaste
{
internal class Parameters
{
public enum ParseResult
{
Success,
BadUrl,
BadFilename,
BarArgs
}
private const string DEFAULT_URL = "http://hastebin.com";
private const string USAGE =
@"Usage: WinHaste.exe [service url] [file to haste]
Defaults:
[service url] = http://hastebin.com
When [file to haste] is omitted, haste.exe reads from
standard input until an end-of-file character (^Z) is
encountered.";
private readonly string[] _args;
public Parameters(string[] args)
{
_args = args;
}
public string Usage { get { return USAGE; } }
public string Url { get; private set; }
public string Filename { get; private set; }
public ParseResult Parse()
{
if (_args.Length == 0)
{
Url = DEFAULT_URL;
return ParseResult.Success;
}
if (_args.Length > 2)
return ParseResult.BarArgs;
if (_args.Length > 0)
{
Uri tmpUri;
if (!Uri.TryCreate(_args[0], UriKind.Absolute, out tmpUri))
return ParseResult.BadUrl;
if (tmpUri.Scheme != "http" && tmpUri.Scheme != "https")
return ParseResult.BadUrl;
Url = _args[0].TrimEnd('/');
}
if (_args.Length > 1)
{
if (!File.Exists(_args[1]))
return ParseResult.BadFilename;
Filename = _args[1];
}
return ParseResult.Success;
}
}
}