-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.cs
144 lines (132 loc) · 5.25 KB
/
Command.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
The MIT License (MIT)
Copyright © 2022 David Zangger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HdHomerun
{
/// <summary>
/// The command class is used to help parse the command the user enters at the prompt.
/// </summary>
internal class Command
{
public string Object { get; set; } // serials, channels
public int? Seq { get; set; } // the sequence
public string Action { get; set; } // del, keep, clean
public int? Count { get; set; } // 0 = all, 1, 2, 3 ...
public bool Valid { get; set; } // was this commmand parsed ok?
public bool Help { get; set; } // Show help
public bool All { get; set; } // Used the * to mean all
/// <summary>
/// Shows the command parsed into a readable string
/// </summary>
/// <returns>the command in an easily readable string</returns>
public override string ToString()
{
string seq = (Seq == null ? "null" : Seq.Value.ToString());
string action = (Action == null ? "null" : Action);
return $"Obj [{Object}] Seq [{seq}] All [{All}] Action [{action}] Count [{Count}]";
}
public Command(string command)
{
// Set some defaults
Help = false;
All = false;
// Examples of some commands the user can enter
// ser # list all the serials
// ser 2 # show recordings for the serial
// ser ? # show help for this object
// ser * # show all recordings for all the series
// ser 2 delete 1|* # delete recording #1 for serial #2
// ser 2 keep 4 # keep up to 4 recordings for serial #2
// ser 2 clean # clean up recordings for serial #2
string[] parts = command.Split(' ');
try
{
// set the object
if (parts.Length > 0)
Object = parts[0];
// The sequence # of the object
if (parts.Length > 1)
{
if (parts[1] == "?")
{
Help = true;
}
else if (parts[1] == "*")
{
Seq = null;
All = true;
}
else
{
try
{
Seq = int.Parse(parts[1]);
}
catch (Exception)
{
if (Object.Equals("log", StringComparison.CurrentCultureIgnoreCase))
{
Action = parts[1];
}
}
}
}
else
{
Seq = null;
All = false;
}
if (!Object.Equals("log", StringComparison.CurrentCultureIgnoreCase))
{
if (parts.Length > 2)
{
Action = parts[2];
}
else
{
Action = null;
}
if (parts.Length > 3)
{
if (parts[3] == "*")
{
Count = null;
All = true;
}
else
Count = int.Parse(parts[3]);
}
else
{
Count = null;
}
}
// Set valid for now
Valid = true;
}
catch (Exception)
{
Valid = false;
}
}
}
}