|
1 | | -using MatthiWare.CommandLine.Abstractions.Command; |
2 | | -using MatthiWare.CommandLine.Abstractions.Parsing; |
3 | | -using MatthiWare.CommandLine.Core.Command; |
4 | | -using System; |
5 | | -using System.Linq; |
| 1 | +using System; |
6 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
7 | 4 | using System.Text; |
8 | 5 | using MatthiWare.CommandLine.Abstractions; |
| 6 | +using MatthiWare.CommandLine.Abstractions.Command; |
| 7 | +using MatthiWare.CommandLine.Abstractions.Models; |
| 8 | +using MatthiWare.CommandLine.Abstractions.Parsing; |
| 9 | +using MatthiWare.CommandLine.Core.Command; |
9 | 10 |
|
10 | 11 | namespace MatthiWare.CommandLine.Core.Parsing |
11 | 12 | { |
12 | | - internal class ArgumentManager : IArgumentGrouper, IDisposable |
| 13 | + internal class ArgumentManager : IArgumentManager, IDisposable |
13 | 14 | { |
14 | | - private IDictionary<ICommandLineCommand, string> arguments; |
| 15 | + private readonly IDictionary<ICommandLineCommand, ArgumentModel> arguments; |
| 16 | + private readonly List<ArgumentValueHolder> args; |
15 | 17 |
|
16 | | - public ArgumentManager(string[] args, List<CommandLineCommandBase> commands, List<CommandLineOptionBase> options) |
| 18 | + public ArgumentManager(string[] args, ICollection<CommandLineCommandBase> commands, ICollection<CommandLineOptionBase> options) |
17 | 19 | { |
18 | | - arguments = new Dictionary<ICommandLineCommand, string>(commands.Count + options.Count); |
| 20 | + arguments = new Dictionary<ICommandLineCommand, ArgumentModel>(commands.Count + options.Count); |
19 | 21 |
|
20 | | - var lstArgs = new List<ArgumentValueHolder>(args.Select(arg => new ArgumentValueHolder |
| 22 | + this.args = new List<ArgumentValueHolder>(args.Select(arg => new ArgumentValueHolder |
21 | 23 | { |
22 | 24 | Argument = arg, |
23 | 25 | Used = false |
24 | 26 | })); |
25 | 27 |
|
26 | | - Parse(lstArgs, commands); |
| 28 | + ParseCommands(commands); |
27 | 29 |
|
28 | | - Parse(lstArgs, options); |
| 30 | + Parse(options); |
29 | 31 |
|
30 | | - foreach (var item in lstArgs) |
| 32 | + foreach (var item in this.args) |
31 | 33 | { |
32 | | - if (item.Used) continue; |
| 34 | + if (item.Command == null) continue; |
| 35 | + |
| 36 | + int nextIndex = item.Index + 1; |
| 37 | + |
| 38 | + var argValue = nextIndex < this.args.Count ? this.args[nextIndex] : null; |
33 | 39 |
|
34 | | - item.Used = true; |
| 40 | + var argModel = new ArgumentModel |
| 41 | + { |
| 42 | + Key = item.Argument, |
| 43 | + Value = (argValue?.Used ?? true) ? null : argValue.Argument |
| 44 | + }; |
35 | 45 |
|
36 | | - arguments.Add(item.Command, item.Argument); |
| 46 | + arguments.Add(item.Command, argModel); |
37 | 47 | } |
38 | 48 | } |
39 | 49 |
|
40 | | - private void Parse(List<ArgumentValueHolder> args, IEnumerable<ICommandLineCommand> list) |
| 50 | + private void Parse(IEnumerable<ICommandLineCommand> list) |
41 | 51 | { |
42 | 52 | foreach (var item in list) |
43 | 53 | { |
44 | | - int idx = args.FindIndex(arg => !arg.Used && |
45 | | - ((item.HasShortName && string.Equals(item.ShortName, arg.Argument, StringComparison.InvariantCultureIgnoreCase)) || |
46 | | - (item.HasLongName && string.Equals(item.LongName, arg.Argument, StringComparison.InvariantCultureIgnoreCase)))); |
| 54 | + int idx = FindIndex(item); |
| 55 | + |
| 56 | + SetArgumentUsed(idx, item); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void ParseCommands(IEnumerable<CommandLineCommandBase> cmds) |
| 61 | + { |
| 62 | + foreach (var cmd in cmds) |
| 63 | + { |
| 64 | + int idx = FindIndex(cmd); |
| 65 | + |
| 66 | + SetArgumentUsed(idx, cmd); |
47 | 67 |
|
48 | | - args[idx].Used = true; |
49 | | - args[idx].Command = item; |
| 68 | + foreach (var option in cmd.Options) |
| 69 | + { |
| 70 | + // find the option index starting at the command index |
| 71 | + int optionIdx = FindIndex(option, idx); |
| 72 | + |
| 73 | + SetArgumentUsed(optionIdx, option); |
| 74 | + } |
50 | 75 | } |
51 | 76 | } |
52 | 77 |
|
| 78 | + private void SetArgumentUsed(int idx, ICommandLineCommand cmd) |
| 79 | + { |
| 80 | + args[idx].Used = true; |
| 81 | + args[idx].Command = cmd; |
| 82 | + args[idx].Index = idx; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Finds the index of the first unused argument |
| 87 | + /// </summary> |
| 88 | + /// <param name="args">List of arguments to search</param> |
| 89 | + /// <param name="cmd">Option to find</param> |
| 90 | + /// <param name="startOffset">Search offset</param> |
| 91 | + /// <returns></returns> |
| 92 | + private int FindIndex(ICommandLineCommand cmd, int startOffset = 0) |
| 93 | + => args.FindIndex(startOffset, arg => !arg.Used && |
| 94 | + ((cmd.HasShortName && string.Equals(cmd.ShortName, arg.Argument, StringComparison.InvariantCultureIgnoreCase)) || |
| 95 | + (cmd.HasLongName && string.Equals(cmd.LongName, arg.Argument, StringComparison.InvariantCultureIgnoreCase)))); |
| 96 | + |
53 | 97 | public void Dispose() |
54 | 98 | { |
55 | 99 | arguments.Clear(); |
56 | 100 | } |
57 | 101 |
|
58 | | - public bool TryGetValue(ICommandLineCommand cmd, out string value) => arguments.TryGetValue(cmd, out value); |
| 102 | + public bool TryGetValue(ICommandLineCommand cmd, out ArgumentModel model) => arguments.TryGetValue(cmd, out model); |
59 | 103 |
|
60 | 104 | private class ArgumentValueHolder |
61 | 105 | { |
62 | 106 | public string Argument { get; set; } |
63 | 107 | public bool Used { get; set; } |
64 | 108 | public ICommandLineCommand Command { get; set; } |
| 109 | + public int Index { get; set; } |
65 | 110 | } |
66 | 111 | } |
67 | 112 | } |
0 commit comments