/// <summary> /// Analyze the type and build the usage model /// </summary> public void Analyze() { foreach (var v in _VerbDefinitions) { if (_Verbs.Exists(x => x.Name == v.Value.Name)) { throw new InvalidOperationException($"Verb {v.Value.Name} already analyzed"); } var verb = Verb.FromAttribute(v.Value).OnTargetProperty(v.Key.PropertyType); // get options var optionDefinitionForCUrrentVerb = OptionDefinitionAttributeHelper.ExtractPropertiesMarkedWithOptionAttribute(v.Key.PropertyType); foreach (var otp in optionDefinitionForCUrrentVerb) { verb.AddOptionFromAttribute(otp.Value, otp.Key.PropertyType); } // get examples ICLIArguments concreteSettings = Activator.CreateInstance(v.Key.PropertyType) as ICLIArguments; if (concreteSettings == null) { throw new InvalidOperationException($"Unable to instance {v.Key.PropertyType.Name}: is not a valid {typeof(ICLIArguments).Name}"); } concreteSettings.Examples.ToList().ForEach(x => verb.AddExample(x)); this._Verbs.Add(verb); } foreach (var opt in this._OptionDefinitions) { if (this._Options.Exists(x => x.Code == opt.Value.Code)) { throw new InvalidOperationException($"Option {opt.Value.Code} already analyzed"); } var option = Option.FromAttribute(opt.Value).OnTargetProperty(opt.Key.PropertyType); this._Options.Add(option); } }
/// <summary> /// BUilds the model to map CLI usage /// </summary> /// <returns></returns> public CLIUsageModel BuildModel() { var model = new CLIUsageModel(this._TargetType, this._Options, this._Verbs); foreach (var opt in this._OptionDefinitions) { model.MapOption(opt.Value.LongCode, opt.Key); } foreach (var opt in this._VerbDefinitions) { model.MapVerb(opt.Value.Name, opt.Key); } // get examples ICLIArguments concreteSettings = Activator.CreateInstance(this._TargetType) as ICLIArguments; if (concreteSettings == null) { throw new InvalidOperationException($"Unable to instance {this._TargetType.Name}: is not a valid {typeof(ICLIArguments).Name}"); } concreteSettings.Examples.ToList().ForEach(x => model.AddExample(x)); return(model); }
/// <summary> /// Default Constructor /// </summary> /// <param name="text"></param> /// <param name="sample"></param> public CLIUsageExample(String text, ICLIArguments sample) { this.HelpText = text; this.Sample = sample; }