|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace Force.Blazer.Exe.CommandLine |
| 10 | +{ |
| 11 | + public class CommandLineParser<T> where T : new() |
| 12 | + { |
| 13 | + private T _options; |
| 14 | + |
| 15 | + private List<string> _nonKeyOptions; |
| 16 | + |
| 17 | + public CommandLineParser(string[] args) |
| 18 | + { |
| 19 | + ParseArgumentsInternal(args); |
| 20 | + } |
| 21 | + |
| 22 | + private void ParseArgumentsInternal(string[] args) |
| 23 | + { |
| 24 | + var t = new T(); |
| 25 | + |
| 26 | + var knownOptions = |
| 27 | + typeof(T).GetProperties() |
| 28 | + .Select(x => new Tuple<PropertyInfo, CommandLineOptionAttribute>(x, (CommandLineOptionAttribute)x.GetCustomAttributes(typeof(CommandLineOptionAttribute), true).FirstOrDefault())) |
| 29 | + .Where(x => x.Item2 != null) |
| 30 | + .ToArray(); |
| 31 | + |
| 32 | + var boolOptions = |
| 33 | + knownOptions.Where(x => x.Item1.PropertyType == typeof(bool) || x.Item1.PropertyType == typeof(bool?)).ToArray(); |
| 34 | + |
| 35 | + Action<string> throwError = e => { throw new InvalidOperationException("Invalid commandline argument " + e); }; |
| 36 | + var dict = new Dictionary<string, string>(); |
| 37 | + var list = new List<string>(); |
| 38 | + string key = null; |
| 39 | + foreach (var arg in args) |
| 40 | + { |
| 41 | + if (arg.Length > 0) |
| 42 | + { |
| 43 | + if (arg[0] == '-') |
| 44 | + { |
| 45 | + if (arg.Length == 1) throwError(arg); |
| 46 | + if (arg[1] == '-') |
| 47 | + { |
| 48 | + if (arg[1] == '-' && arg.Length <= 3) throwError(arg); |
| 49 | + if (key != null) dict[key] = string.Empty; |
| 50 | + key = arg.Remove(0, 2); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + key = arg.Remove(0, 1); |
| 55 | + } |
| 56 | + |
| 57 | + dict[key] = string.Empty; |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + if (boolOptions.Any(x => x.Item2.ShortKey.ToString(CultureInfo.InvariantCulture) == key || x.Item2.LongKey == key)) key = null; |
| 62 | + if (key == null) |
| 63 | + { |
| 64 | + list.Add(arg); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + dict[key] = arg; |
| 69 | + key = null; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + _nonKeyOptions = list; |
| 76 | + |
| 77 | + foreach (var v in dict) |
| 78 | + { |
| 79 | + var option = knownOptions.FirstOrDefault( |
| 80 | + x => x.Item2.ShortKey.ToString(CultureInfo.InvariantCulture) == v.Key || x.Item2.LongKey == v.Key); |
| 81 | + if (option != null) |
| 82 | + { |
| 83 | + if (boolOptions.Contains(option)) |
| 84 | + { |
| 85 | + option.Item1.SetValue(t, true, null); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + var converted = new TypeConverter().ConvertTo(v.Value, option.Item1.PropertyType); |
| 90 | + option.Item1.SetValue(t, converted, null); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + _options = t; |
| 96 | + } |
| 97 | + |
| 98 | + public T Get() |
| 99 | + { |
| 100 | + return _options; |
| 101 | + } |
| 102 | + |
| 103 | + public string[] GetNonParamOptions() |
| 104 | + { |
| 105 | + return _nonKeyOptions.ToArray(); |
| 106 | + } |
| 107 | + |
| 108 | + public string GetNonParamOptions(int idx) |
| 109 | + { |
| 110 | + if (_nonKeyOptions.Count <= idx) return null; |
| 111 | + return _nonKeyOptions[idx]; |
| 112 | + } |
| 113 | + |
| 114 | + public string GenerateHelp() |
| 115 | + { |
| 116 | + var options = typeof(T).GetProperties() |
| 117 | + .Select(x => (CommandLineOptionAttribute)x.GetCustomAttributes(typeof(CommandLineOptionAttribute), true).FirstOrDefault()) |
| 118 | + .Where(x => x != null) |
| 119 | + .ToArray(); |
| 120 | + |
| 121 | + var maxParamLen = 0; |
| 122 | + |
| 123 | + foreach (var option in options) |
| 124 | + { |
| 125 | + var cnt = 0; |
| 126 | + if (option.ShortKey != default(char) && option.LongKey != null) cnt = 6 + option.LongKey.Length; |
| 127 | + else if (option.ShortKey != default(char)) cnt = 2; |
| 128 | + else cnt = 2 + option.LongKey.Length; |
| 129 | + maxParamLen = Math.Max(maxParamLen, cnt); |
| 130 | + } |
| 131 | + |
| 132 | + var b = new StringBuilder(); |
| 133 | + foreach (var option in options) |
| 134 | + { |
| 135 | + b.Append("\t"); |
| 136 | + string str; |
| 137 | + if (option.ShortKey != default(char) && option.LongKey != null) |
| 138 | + { |
| 139 | + str = string.Format("-{0}, --{1}", option.ShortKey, option.LongKey); |
| 140 | + } |
| 141 | + else if (option.ShortKey != default(char)) |
| 142 | + { |
| 143 | + str = string.Format("-{0}", option.ShortKey); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + str = string.Format("--{0}", option.LongKey); |
| 148 | + } |
| 149 | + |
| 150 | + b.Append(str); |
| 151 | + b.Append(new string(' ', maxParamLen - str.Length)); |
| 152 | + b.Append("\t"); |
| 153 | + b.Append(option.Description); |
| 154 | + b.AppendLine(); |
| 155 | + } |
| 156 | + |
| 157 | + return b.ToString(); |
| 158 | + } |
| 159 | + |
| 160 | + public string GenerateHeader() |
| 161 | + { |
| 162 | + var title = (AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true).First(); |
| 163 | + var copy = (AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true).First(); |
| 164 | + var version = (AssemblyFileVersionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).First(); |
| 165 | + |
| 166 | + return string.Format("{0} {2} {1}", title.Title, copy.Copyright, version.Version); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments