-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandParser.cs
More file actions
66 lines (53 loc) · 1.85 KB
/
CommandParser.cs
File metadata and controls
66 lines (53 loc) · 1.85 KB
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 ImgExpnd.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ImgExpnd
{
public class CommandParser
{
readonly IEnumerable<ICommandFactory> availableCommands;
public CommandParser(IEnumerable<ICommandFactory> availableCommands)
{
this.availableCommands = availableCommands;
}
internal ICommand ParseCommand(string[] args)
{
// if there are no params , it should show help , so call ShowHelpCommand.
if (args.Length == 0)
{
return new ShowHelpCommand();
}
else
{
var requestedCommandName = args[0];
//remove middle dash and add *Command*
if (requestedCommandName[0] == '-')
{
requestedCommandName = requestedCommandName.Remove(0, 1);
//requestedCommandName = CommandParser.UppercaseFirst(requestedCommandName);
// requestedCommandName += "Command";
};
var command = FindRequestedCommand(requestedCommandName);
if (null == command)
return new NotFoundCommand { Name = requestedCommandName };
return command.MakeCommand(args);
};
}
ICommandFactory FindRequestedCommand(string commandName)
{
return availableCommands
.FirstOrDefault(cmd => (String)cmd.CommandName == commandName);
}
static string UppercaseFirst(string s)
{
// Check for empty string.
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
// Return char and concat substring.
return char.ToUpper(s[0]) + s.Substring(1);
}
}
}