/// <summary> /// Resolves help content. /// </summary> private IReadOnlyCollection <string> GetHelpContent() { try { return(_helpContentProvider.GetInstance() ?? throw ConfigurationExceptions.HelpProviderReturnedNull(_helpContentProvider.GetType())); } catch (Exception ex) { throw ConfigurationExceptions.HelpProviderFailed(_helpContentProvider.GetType(), ex); } }
/// <inheritdoc /> public override void MapValue(TOptions options, TValue value) { try { MapValueCore(options, value); } catch (Exception ex) { throw ConfigurationExceptions.ErrorInPropertyMapping <TOptions>( _propertyName, ex); } }
/// <summary> /// Adds a template to the set. /// </summary> /// <param name="template">Template to add.</param> /// <exception cref="System.Exception">Token is in use.</exception> public void Add(Template template) { Check.NotNull(template, nameof(template)); foreach (var token in template.Tokens) { if (_tokenSet.Add(token)) { continue; } throw ConfigurationExceptions.TemplateTokenInUse(token); } }
/// <summary> /// Creates an instance, or throws if it can't. /// </summary> internal static IProvider <TOptions> CreateOrThrow() { var constructorInfo = typeof(TOptions).GetConstructor(Array.Empty <Type>()); if (constructorInfo == null) { throw ConfigurationExceptions.NoDefaultOptionsConstructor <TOptions>(); } var callCtorExpr = Expression.New(constructorInfo); var lambda = Expression.Lambda <Func <TOptions> >(callCtorExpr); var function = lambda.Compile(); return(new ConstructorProvider <TOptions>(function)); }
/// <summary> /// Displays help, bypassing any invocation of client program handlers. /// </summary> /// <param name="configuration">Configuration instance</param> /// <param name="command">The help context - if showing help for the root application, /// leave as null.</param> public static void ShowHelp(ICommandLineConfiguration configuration, string?command = null) { if (configuration.HelpTemplate == null) { throw ConfigurationExceptions.NoHelpOptionDefined(); } var helpToken = configuration.HelpTemplate.Tokens.First().DistinguishedForm !; var args = string.IsNullOrWhiteSpace(command) ? new[] { helpToken } : new[] { command, helpToken }; Run(configuration, args !); }
/// <summary> /// Performs mapping of a converted value to options. /// </summary> /// <typeparam name="TOptions">Options type</typeparam> /// <typeparam name="TValue">Value type</typeparam> /// <param name="mapper">Mapper instance</param> /// <param name="options">Options object</param> /// <param name="context">Context, template or position argument</param> /// <param name="value">Value to map</param> internal static void MapValue <TOptions, TValue>(IMapper <TOptions, TValue> mapper, string context, TOptions options, TValue value) where TOptions : class { try { mapper.MapValue(options, value); } catch (CommandLineException) { throw; } catch (Exception ex) { throw ConfigurationExceptions.MappingFailed(context, ex); } }
/// <inheritdoc /> public override void MapValue(TOptions options, TValue value) { try { MapValueCore(options, value); } catch (NullReferenceException ex) { throw ConfigurationExceptions.NullReferenceInCollectionMapping <TOptions>( CollectionType, PropertyName, ex); } catch (Exception ex) { throw ConfigurationExceptions.ErrorInCollectionMapping <TOptions>( CollectionType, PropertyName, ex); } }
/// <summary> /// Creates a new instance of this type. /// </summary> /// <param name="marginWidth">Output width.</param> /// <param name="marginHeight">Output height.</param> /// <param name="startRow">Start row.</param> /// <param name="formatter">Line formatter</param> public FormatInfo(int marginWidth, int marginHeight, int startRow, IFormatter formatter) { if (marginWidth < MinimumMarginWidth) { throw ConfigurationExceptions.MinimumFormatInfoWidthNotMet(MinimumMarginWidth); } if (marginHeight < MinimumMarginHeight) { throw ConfigurationExceptions.MinimumFormatInfoHeightNotMet(MinimumMarginHeight); } FormatWidth = marginWidth; FormatHeight = marginHeight; StartRow = startRow; LineFormatter = formatter ?? throw ConfigurationExceptions.HelpLineFormatterNotSet(); }
/// <inheritdoc /> public object GetOptions() { var provider = OptionsProvider ?? ConstructorProvider <TOptions> .CreateOrThrow(); object options; try { options = provider.GetInstance(); } catch (Exception ex) { throw ConfigurationExceptions.OptionsProviderFailed(typeof(TOptions), ex); } return(options ?? throw ConfigurationExceptions.OptionsProviderReturnedNull(typeof(TOptions))); }
/// <summary> /// Creates or throws a converter. /// </summary> internal static IValueConverter <TValue> CreateOrThrow <TValue>() { var created = StringConverter <TValue> .TryCreate(out var converter) || ParseConverter <TValue> .TryCreate(out converter) || EnumConverter <TValue> .TryCreate(out converter) || NullableTypeParseConverter <TValue> .TryCreate(out converter) || NullableEnumConverter <TValue> .TryCreate(out converter) || ConstructorConverter <TValue> .TryCreate(out converter) || CastConverter <TValue> .TryCreate(out converter); if (created) { return(converter !); } throw ConfigurationExceptions.NoDefaultConverter <TValue>(); }
/// <inheritdoc /> public virtual void MapValue(TOptions options, TValue value) { try { MapValueCore(options, value); } catch (NullReferenceException ex) { throw ConfigurationExceptions.NullReferenceInMapping <TOptions>(ex); } catch (UsageException) { throw; } catch (Exception ex) { throw ConfigurationExceptions.ErrorInDelegateMapping(ex); } }
/// <summary> /// Registers a command as an application sub-program. /// </summary> /// <param name="template">Template that identifies the command.</param> /// <param name="configureAction">Configuration action.</param> /// <typeparam name="TCommandOptions">Command options.</typeparam> /// <returns>Configuration.</returns> /// <exception cref="Exception">Invalid configuration.</exception> public ApplicationConfiguration <TOptions> Command <TCommandOptions>(string template, Action <CommandConfiguration <TCommandOptions> > configureAction) where TCommandOptions : class, TOptions { Check.NotNull(configureAction, nameof(configureAction)); try { var commandTemplate = Template.ForCommand(template); var configuration = new CommandConfiguration <TCommandOptions>(commandTemplate); configureAction(configuration); _subConfigurations.Add(configuration); ParserConfig.AddTemplate(commandTemplate); return(this); } catch (Exception ex) { throw ConfigurationExceptions.InvalidCommandConfiguration(template, ex); } }
/// <summary> /// Creates a new instance. /// </summary> /// <param name="helpWriter">Help writer</param> /// <param name="helpContentProvider">Provider for content</param> internal HelpProgram(IHelpWriter helpWriter, IProvider <IReadOnlyCollection <string> > helpContentProvider) { _helpWriter = helpWriter; _helpContentProvider = helpContentProvider ?? throw ConfigurationExceptions.NoHelpContentProviderDefined(); }
// Ensures the client handler was defined. private ClientHandler <TOptions> GetClientHandlerOrThrow() { return(ClientHandler ?? throw ConfigurationExceptions.NoClientHandlerDefined( Template?.ToString() ?? DefaultContext)); }