コード例 #1
0
        /// <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();
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        private CommandLineParserOptions UpdateOptionsIfNeeded(CommandLineParserOptions options)
        {
            if (!string.IsNullOrWhiteSpace(options.AppName))
            {
                return(options);
            }

            options.AppName = Process.GetCurrentProcess().ProcessName;

            return(options);
        }
コード例 #4
0
        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.");
            }
        }
コード例 #5
0
        /// <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();
        }
コード例 #6
0
 /// <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())
 {
 }
コード例 #7
0
 /// <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)
 {
 }
コード例 #8
0
 /// <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)
 {
 }
コード例 #9
0
 /// <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)
 {
 }
コード例 #10
0
 /// <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)
 {
 }
コード例 #11
0
 /// <summary>
 /// Creates a new instance of the commandline parser
 /// </summary>
 /// <param name="parserOptions">The parser options</param>
 public CommandLineParser(CommandLineParserOptions parserOptions)
     : base(parserOptions)
 {
 }
コード例 #12
0
 /// <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)
 {
 }
コード例 #13
0
 public CommandLineParser(CommandLineParserOptions parserOptions, IServiceCollection serviceCollection)
     : base(parserOptions, serviceCollection)
 {
 }
コード例 #14
0
        /// <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);
        }