/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="serviceProvider">Service provider to resolve internal services with</param> /// <param name="parserOptions">The options the parser will use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IServiceProvider serviceProvider) { ValidateOptions(parserOptions); ParserOptions = UpdateOptionsIfNeeded(parserOptions); if (serviceProvider == null) { var services = new ServiceCollection(); services.AddInternalCommandLineParserServices(this, ParserOptions); Services = services.BuildServiceProvider(); } else { Services = serviceProvider; } logger = Services.GetRequiredService <ILogger <CommandLineParser> >(); m_option = new TOption(); (m_helpOptionName, m_helpOptionNameLong) = parserOptions.GetConfiguredHelpOption(); InitialzeModel(); }
/// <summary> /// Adds <see cref="CommandLineParser"/> to the services /// This won't overwrite existing services. /// </summary> /// <param name="services">Current service collection</param> /// <param name="options">Current options reference</param> public static void AddCommandLineParser(this IServiceCollection services, CommandLineParserOptions options = null) { Func <IServiceProvider, CommandLineParser> factory = (IServiceProvider provider) => new CommandLineParser(provider.GetService <CommandLineParserOptions>(), provider); services .AddInternalCommandLineParserServices2(options) .AddCommandLineParserFactory(factory); }
private CommandLineParserOptions UpdateOptionsIfNeeded(CommandLineParserOptions options) { if (!string.IsNullOrWhiteSpace(options.AppName)) { return(options); } options.AppName = Process.GetCurrentProcess().ProcessName; return(options); }
private void ValidateOptions(CommandLineParserOptions options) { if (options is null) { throw new ArgumentNullException("The provided options cannot be null."); } if (string.IsNullOrWhiteSpace(options.PrefixShortOption)) { throw new ArgumentException($"The provided options are not valid. {nameof(options.PrefixShortOption)} cannot be null or empty."); } if (string.IsNullOrWhiteSpace(options.PrefixLongOption)) { throw new ArgumentException($"The provided options are not valid. {nameof(options.PrefixLongOption)} cannot be null or empty."); } if (options.PrefixShortOption.Length != 1) { throw new ArgumentException($"The provided options are not valid. {nameof(options.PrefixShortOption)} needs to be a single character."); } }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="argumentResolverFactory">argument resolver to use</param> /// <param name="containerResolver">container resolver to use</param> /// <param name="parserOptions">The options the parser will use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IArgumentResolverFactory argumentResolverFactory, IContainerResolver containerResolver) { ParserOptions = parserOptions; m_option = new TOption(); m_options = new Dictionary <string, CommandLineOptionBase>(); m_commands = new List <CommandLineCommandBase>(); ArgumentResolverFactory = argumentResolverFactory; ContainerResolver = containerResolver; if (string.IsNullOrWhiteSpace(ParserOptions.AppName)) { ParserOptions.AppName = Process.GetCurrentProcess().ProcessName; } Printer = new UsagePrinter(parserOptions, this, new UsageBuilder(parserOptions)); if (ParserOptions.EnableHelpOption) { var tokens = ParserOptions.HelpOptionName.Split('|'); if (tokens.Length > 1) { m_helpOptionName = $"{ParserOptions.PrefixShortOption}{tokens[0]}"; m_helpOptionNameLong = $"{ParserOptions.PrefixLongOption}{tokens[1]}"; } else { m_helpOptionName = $"{ParserOptions.PrefixLongOption}{tokens[0]}"; m_helpOptionNameLong = null; } } InitialzeModel(); }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">The parser options</param> public CommandLineParser(CommandLineParserOptions parserOptions) : this(parserOptions, new DefaultArgumentResolverFactory(), new DefaultContainerResolver()) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">The parser options</param> public CommandLineParser(CommandLineParserOptions parserOptions) : this(parserOptions, null as IServiceProvider) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="argumentResolverFactory">argument resolver to use</param> /// <param name="containerResolver">container resolver to use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IArgumentResolverFactory argumentResolverFactory, IContainerResolver containerResolver) : base(parserOptions, argumentResolverFactory, containerResolver) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">options that the parser will use</param> /// <param name="containerResolver">container resolver to use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IContainerResolver containerResolver) : base(parserOptions, containerResolver) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">options that the parser will use</param> /// <param name="argumentResolverFactory">argument resolver to use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IArgumentResolverFactory argumentResolverFactory) : base(parserOptions, argumentResolverFactory) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">The parser options</param> public CommandLineParser(CommandLineParserOptions parserOptions) : base(parserOptions) { }
/// <summary> /// Creates a new instance of the commandline parser /// </summary> /// <param name="parserOptions">options that the parser will use</param> /// <param name="serviceProvider">Services Provider to use</param> public CommandLineParser(CommandLineParserOptions parserOptions, IServiceProvider serviceProvider) : base(parserOptions, serviceProvider) { }
public CommandLineParser(CommandLineParserOptions parserOptions, IServiceCollection serviceCollection) : base(parserOptions, serviceCollection) { }
/// <summary> /// Adds <see cref="ICommandLineParser{TOption}"/> to the services /// This won't overwrite existing services. /// </summary> /// <typeparam name="TOption">Base option type</typeparam> /// <param name="services">Current service collection</param> /// <param name="options">Current options reference</param> public static void AddCommandLineParser <TOption>(this IServiceCollection services, CommandLineParserOptions options = null) where TOption : class, new() { Func <IServiceProvider, CommandLineParser <TOption> > factory = (IServiceProvider provider) => new CommandLineParser <TOption>(provider.GetService <CommandLineParserOptions>(), provider); services .AddInternalCommandLineParserServices2(options) .AddCommandLineParserFactoryGeneric(factory); }