public RootCommand Build() { // Check attribute exists if (_att is null) { throw new ArgumentNullException("Object doesn't implement RootCommandAttribute"); } // Create root command var root = new RootCommand(_att.Description ?? ""); // Search in properties for arguments and options foreach (var p in _t.GetProperties()) { var arg = new ArgumentFactory(p); if (arg.IsValid) { root.AddArgument(arg.Build()); continue; } var opt = new OptionFactory(p); if (opt.IsValid) { root.AddOption(opt.Build()); } } return(root); }
/// <summary> /// Builds the specified command. /// </summary> /// <param name="instance">The instance that contains the handler.</param> /// <param name="methodInfo">The methodInfo for the handler.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">Object doesn't implement CommandAttribute</exception> public Command Build(object?instance = null, MethodInfo?methodInfo = null) { if (_att is null) { throw new ArgumentNullException("Object doesn't implement CommandAttribute"); } var method = methodInfo ?? _handlerInfo; // Load default name var defName = _t.Name.PascalToKebabCase(); //Create command var cmd = new Command(_att.Name ?? defName, _att.Description) { TreatUnmatchedTokensAsErrors = _att.TreatUnmatchedTokensAsErrors }; // Add Aliases foreach (var a in _att.Aliases) { cmd.AddAlias(a); } // Add arguments and options foreach (var p in _t.GetProperties()) { var arg = new ArgumentFactory(p); if (arg.IsValid) { cmd.AddArgument(arg.Build()); continue; } var opt = new OptionFactory(p); if (opt.IsValid) { cmd.AddOption(opt.Build()); } } if (instance is not null && method is not null) { cmd.Handler = CommandHandler.Create(method, instance); } return(cmd); }