Format a command line argument string from a parsed instance.
136 {
137 if (options == null)
138 {
139 throw new ArgumentNullException("options");
140 }
141
142 UnParserSettings settings = new UnParserSettings();
143 configuration(settings);
144 settings.Consumed = true;
145
146 Type type = options.GetType();
147 StringBuilder builder = new StringBuilder();
148
149 type.GetVerbSpecification()
150 .MapValueOrDefault(verb => builder.Append(verb.Name)
151 .Append(' '),
152 builder
153 );
154
155 var specs =
156 (from info in
157 type.GetSpecifications(pi => new
158 {
160 Value = pi.GetValue(options,
null)
161 .NormalizeValue(),
162 PropertyValue = pi.GetValue(options, null),
163 }
164 )
165 where !info.PropertyValue.IsEmpty(info.Specification, settings.SkipDefault)
166 select info)
167 .Memoize();
168
169 var allOptSpecs = from info in specs.Where(i => i.Specification.Tag ==
SpecificationType.Option)
172 (o.TargetType ==
TargetType.Switch && o.FlagCounter && (
int)info.Value > 0) ||
173 (o.TargetType ==
TargetType.Switch && (
bool)info.Value)
174 where !o.Hidden || settings.ShowHidden
175 orderby o.UniqueName()
176 select info;
177
178 var shortSwitches = from info in allOptSpecs
181 where o.ShortName.Length > 0
182 orderby o.UniqueName()
183 select info;
184
185 var optSpecs = settings.GroupSwitches
186 ? allOptSpecs.Where(info => !shortSwitches.Contains(info))
187 : allOptSpecs;
188
189 var valSpecs = from info in specs.Where(i => i.Specification.Tag ==
SpecificationType.Value)
191 orderby v.Index
192 select info;
193
194 builder = settings.GroupSwitches && shortSwitches.Any()
195 ? builder.Append('-')
196 .Append(string.Join(string.Empty,
197 shortSwitches.Select(info =>
198 {
199 OptionSpecification o =
200 (OptionSpecification)info
201 .Specification;
202
203 return o.FlagCounter
204 ? string
205 .Concat(Enumerable
206 .Repeat(o.ShortName,
207 (int)info.Value
208 )
209 )
210 : o.ShortName;
211 }
212 )
213 .ToArray()
214 )
215 )
216 .Append(' ')
217 : builder;
218
219 optSpecs.ForEach(opt =>
220 builder
222 .Append(' ')
223 );
224
225 builder.AppendWhen(valSpecs.Any() && parser.Settings.EnableDashDash, "-- ");
226
227 valSpecs.ForEach(val => builder.Append(
FormatValue(val.Specification, val.Value))
228 .Append(' ')
229 );
230
231 return builder
232 .ToString()
233 .TrimEnd(' ');
234 }
static Specification FromProperty(PropertyInfo property)
static string FormatValue(Specification spec, object value)
static string FormatOption(OptionSpecification spec, object value, UnParserSettings settings)