Esempio n. 1
0
        private void AddSubcommandImpl <TSubCommand>(SubcommandAttribute subcommand)
            where TSubCommand : class, new()
        {
            if (App.Commands.Any(c => c.Name.Equals(subcommand.Name, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException(Strings.DuplicateSubcommandName(subcommand.Name));
            }

            var childApp = App.Command(subcommand.Name, subcommand.Configure);
            var builder  = new ReflectionAppBuilder <TSubCommand>(childApp);

            builder._bindResult = _bindResult;
            builder.Initialize();

            var subcommandProp = typeof(TTarget).GetTypeInfo().GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (subcommandProp != null)
            {
                var setter = ReflectionHelper.GetPropertySetter(subcommandProp);
                builder.OnBind(o =>
                               setter.Invoke(this._target, o));
            }

            var parentProp = subcommand.CommandType.GetTypeInfo().GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (parentProp != null)
            {
                var setter = ReflectionHelper.GetPropertySetter(parentProp);
                builder.OnBind(o =>
                               setter.Invoke(o, this._target));
            }
        }
        public static T ParseArgs <T>(params string[] args)
            where T : class, new()
        {
            var applicationBuilder = new ReflectionAppBuilder <T>();
            var context            = new DefaultCommandLineContext(NullConsole.Singleton, Directory.GetCurrentDirectory(), args);

            return((T)applicationBuilder.Bind(context).ParentTarget);
        }
        private static BindResult Bind <TApp>(IConsole console, string[] args) where TApp : class, new()
        {
            if (console == null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            var applicationBuilder = new ReflectionAppBuilder <TApp>();

            return(applicationBuilder.Bind(console, args));
        }
Esempio n. 4
0
        private void AddSubcommandImpl <TSubCommand>(Type parent, SubcommandAttribute subcommand)
            where TSubCommand : class, new()
        {
            var parentApp = App;
            var childApp  = App.Command(subcommand.Name, subcommand.Configure);

            var builder = new ReflectionAppBuilder <TSubCommand>(childApp);

            builder.Initialize();

            var subcommandProp = parent.GetTypeInfo().GetProperty("Subcommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (subcommandProp != null)
            {
                var setter = ReflectionHelper.GetPropertySetter(subcommandProp);
                builder.OnBind(o =>
                {
                    if (parentApp.State is BindContext ctx)
                    {
                        setter.Invoke(ctx.Target, o);
                    }
                });
            }

            var parentProp = subcommand.CommandType.GetTypeInfo().GetProperty("Parent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

            if (parentProp != null)
            {
                var setter = ReflectionHelper.GetPropertySetter(parentProp);
                builder.OnBind(o =>
                {
                    if (parentApp.State is BindContext ctx)
                    {
                        setter.Invoke(o, ctx.Target);
                    }
                });
            }
        }
        private static BindResult Bind <TApp>(CommandLineContext context) where TApp : class, new()
        {
            var applicationBuilder = new ReflectionAppBuilder <TApp>();

            return(applicationBuilder.Bind(context));
        }