Exemplo n.º 1
0
        static void HandleRemainingArguments(RemainingArgumentsAttribute att,
                                             CommandLineBinding binding, MemberInfo m)
        {
            if (att.SkipIfNone && binding._cla.RemainingArguments?.Count == 0)
            {
                // No remaining args
                return;
            }

            var args = binding._cla.RemainingArguments?.ToArray();

            if (args == null)
            {
                args = new string[0];
            }

            if (m is MethodInfo)
            {
                var mi = (MethodInfo)m;
                mi.Invoke(binding.GetModel(), args);
            }
            else if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                pi.SetValue(binding.GetModel(), args);
            }
        }
Exemplo n.º 2
0
        static void AddVersionOption(CommandLineBinding binding, Attribute att, MemberInfo m)
        {
            if (att is VersionOptionAttribute)
            {
                binding._versionOption = (VersionOptionAttribute)att;

                binding._bindingActions.Add(() =>
                {
                    if (binding._versionOption == null)
                    {
                        return;
                    }

                    if (binding._versionShortGetter == null)
                    {
                        binding._versionShortGetter = binding._versionLongGetter;
                    }

                    if (binding._versionShortGetter != null)
                    {
                        binding._cla.VersionOption(binding._versionOption.Template,
                                                   binding._versionShortGetter, binding._versionLongGetter);
                    }
                    else
                    {
                        binding._cla.VersionOption(binding._versionOption.Template,
                                                   binding._versionOption.ShortVersion, binding._versionOption.LongVersion);
                    }
                });
            }
            else if (att is ShortVersionGetterAttribute || att is LongVersionGetterAttribute)
            {
                var           mi     = m as MethodInfo;
                var           pi     = m as PropertyInfo;
                Func <string> getter = null;

                if (mi != null && mi.ReturnParameter.ParameterType == typeof(string) &&
                    mi.GetParameters().Length == 0)
                {
                    getter = () => (string)mi.Invoke(binding.GetModel(), EmptyValues);
                }
                else if (pi != null && pi.PropertyType == typeof(string))
                {
                    getter = () => (string)pi.GetValue(binding.GetModel());
                }

                if (getter != null && att is ShortVersionGetterAttribute)
                {
                    binding._versionShortGetter = getter;
                }
                if (getter != null && att is LongVersionGetterAttribute)
                {
                    binding._versionLongGetter = getter;
                }
            }
        }
Exemplo n.º 3
0
        static void HandleOption(CommandOption co, CommandLineBinding binding, MemberInfo m)
        {
            if (!co.HasValue())
            {
                return;
            }

            object coValue;

            if (co.OptionType == CommandOptionType.NoValue)
            {
                //coValue = bool.Parse(co.Value());
                coValue = true;
            }
            else if (co.OptionType == CommandOptionType.SingleValue)
            {
                coValue = co.Value();
            }
            else
            {
                coValue = co.Values.ToArray();
            }

            if (m is MethodInfo)
            {
                var mi       = (MethodInfo)m;
                var miParams = mi.GetParameters();
                if (miParams.Length == 2)
                {
                    mi.Invoke(binding.GetModel(), new object[] { coValue, binding });
                }
                else if (miParams.Length == 1)
                {
                    mi.Invoke(binding.GetModel(), new object[] { coValue });
                }
                else
                {
                    mi.Invoke(binding.GetModel(), EmptyValues);
                }
            }
            else if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                pi.SetValue(binding.GetModel(), coValue);
            }
        }
Exemplo n.º 4
0
        static void BindRemainingArguments(CommandLineBinding binding)
        {
            var m = binding._remainingArgumentsMember;

            if (m == null)
            {
                return;
            }

            var a            = (RemainingArgumentsAttribute)m.GetCustomAttribute(typeof(RemainingArgumentsAttribute));
            var onConfigName = $"{m.Name}{MemberOnBindMethodSuffix}";
            var onConfigMeth = binding.GetModel().GetType().GetTypeInfo().GetMethod(
                onConfigName, OnBindRemainingArgumentsParams);

            if (onConfigMeth != null)
            {
                onConfigMeth.Invoke(binding.GetModel(), new[] { binding._cla });
            }

            binding._postExecActions.Add(() => { HandleRemainingArguments(a, binding, m); });
        }
Exemplo n.º 5
0
        static void HandleArgument(CommandArgument ca, CommandLineBinding binding, MemberInfo m)
        {
            if (ca.Values?.Count == 0)
            {
                return;
            }

            object caValue;

            if (ca.MultipleValues)
            {
                caValue = ca.Values.ToArray();
            }
            else
            {
                caValue = ca.Value;
            }

            if (m is MethodInfo)
            {
                var mi       = (MethodInfo)m;
                var miParams = mi.GetParameters();
                if (miParams.Length == 2)
                {
                    mi.Invoke(binding.GetModel(), new object[] { caValue, binding });
                }
                else
                {
                    mi.Invoke(binding.GetModel(), new object[] { caValue });
                }
            }
            else if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                pi.SetValue(binding.GetModel(), caValue);
            }
        }
Exemplo n.º 6
0
        static void AddArgument(CommandLineBinding binding, Attribute att, MemberInfo m)
        {
            var a = (ArgumentAttribute)att;

            // Resolve the option value type
            Type valueType = null;

            if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                valueType = pi.PropertyType;
            }
            else if (m is MethodInfo)
            {
                var mi       = (MethodInfo)m;
                var miParams = mi.GetParameters();

                if (miParams.Length == 1 || (miParams.Length == 2 &&
                                             typeof(CommandLineBinding).GetTypeInfo().IsAssignableFrom(miParams[1].ParameterType)))
                {
                    valueType = miParams[0].ParameterType;
                }
                else if (miParams.Length > 0)
                {
                    throw new NotSupportedException("method signature is not supported");
                }
            }

            // Figure out the argument arity based on value type if it wasn't explicitly specified
            var multi = a.MultipleValues;

            if (multi == null && valueType != null)
            {
                // Try to resolve the option type based on the property type
                if (valueType.GetTypeInfo().IsAssignableFrom(typeof(string)))
                {
                    multi = false;
                }
                else if (valueType.GetTypeInfo().IsAssignableFrom(typeof(string[])))
                {
                    multi = true;
                }
                else
                {
                    throw new NotSupportedException("option value type is not supported");
                }
            }

            // Resolve the arg name if it wasn't explicitly specified
            var argName = a.Name;

            if (string.IsNullOrEmpty(argName))
            {
                argName = m.Name.ToLower();
            }

            // See if there is an optional configuration method for this option
            var onConfigName = $"{m.Name}{MemberOnBindMethodSuffix}";
            var onConfigMeth = binding.GetModel().GetType().GetTypeInfo().GetMethod(
                onConfigName, OnBindArgumentParams);

            CommandArgument ca;

            if (onConfigMeth != null)
            {
                ca = binding._cla.Argument(argName, a.Description,
                                           cmdArg => onConfigMeth.Invoke(binding.GetModel(), new[] { cmdArg }),
                                           multi.GetValueOrDefault());
            }
            else
            {
                ca = binding._cla.Argument(argName, a.Description, multi.GetValueOrDefault());
            }

            binding._postExecActions.Add(() => { HandleArgument(ca, binding, m); });
        }
Exemplo n.º 7
0
        static void AddOption(CommandLineBinding binding, Attribute att, MemberInfo m)
        {
            var a = (OptionAttribute)att;

            // Resolve the option value type
            Type valueType = null;

            if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                valueType = pi.PropertyType;
            }
            else if (m is MethodInfo)
            {
                var mi       = (MethodInfo)m;
                var miParams = mi.GetParameters();

                if (miParams.Length == 1 || (miParams.Length == 2 &&
                                             typeof(CommandLineBinding).GetTypeInfo().IsAssignableFrom(miParams[1].ParameterType)))
                {
                    valueType = miParams[0].ParameterType;
                }
                else if (miParams.Length > 0)
                {
                    throw new NotSupportedException("method signature is not supported");
                }
            }

            // Figure out the option type based on value type if it wasn't explicitly specified
            CommandOptionType optionType = CommandOptionType.NoValue;

            if (a.OptionType == null && valueType != null)
            {
                // Try to resolve the option type based on the property type
                if (valueType.GetTypeInfo().IsAssignableFrom(typeof(string)))
                {
                    optionType = CommandOptionType.SingleValue;
                }
                else if (valueType.GetTypeInfo().IsAssignableFrom(typeof(string[])))
                {
                    optionType = CommandOptionType.MultipleValue;
                }
                else if (valueType == typeof(bool?) || valueType == typeof(bool))
                {
                    optionType = CommandOptionType.NoValue;
                }
                else
                {
                    throw new NotSupportedException("option value type is not supported");
                }
            }

            // Resolve the option template if it wasn't explicitly specified
            var template = a.Template;

            if (string.IsNullOrEmpty(template))
            {
                template = $"--{m.Name.ToLower()}";
            }

            // See if there is an optional configuration method for this option
            var onConfigName = $"{m.Name}{MemberOnBindMethodSuffix}";
            var onConfigMeth = binding.GetModel().GetType().GetTypeInfo().GetMethod(
                onConfigName, OnBindOptionParams);

            // Add the option based on whether there is a config method
            CommandOption co;

            if (onConfigMeth != null)
            {
                co = binding._cla.Option(template, a.Description,
                                         optionType,
                                         cmdOpt => onConfigMeth.Invoke(binding.GetModel(), new[] { cmdOpt }),
                                         a.Inherited);
            }
            else
            {
                co = binding._cla.Option(template, a.Description,
                                         optionType,
                                         a.Inherited);
            }

            // Add a post-exec handler for this option
            binding._postExecActions.Add(() => HandleOption(co, binding, m));
        }
Exemplo n.º 8
0
        static void AddCommand(CommandLineBinding binding, Attribute att, MemberInfo m)
        {
            var a = (CommandAttribute)att;

            var cmdName = a.Name;

            if (string.IsNullOrEmpty(cmdName))
            {
                cmdName = m.Name.ToLower();
            }

            Type cmdType;

            if (m is MethodInfo)
            {
                var mi       = (MethodInfo)m;
                var miParams = mi.GetParameters();

                if (miParams.Length == 1)
                {
                    cmdType = miParams[0].ParameterType;
                }
                else if (miParams.Length == 0)
                {
                    cmdType = null;
                }
                else
                {
                    throw new NotSupportedException("method signature is not supported");
                }
            }
            else if (m is PropertyInfo)
            {
                var pi = (PropertyInfo)m;
                cmdType = pi.PropertyType;
            }
            else
            {
                return;
            }

            // See if there is an optional configuration method for this sub-command
            var onConfigName = $"{m.Name}{MemberOnBindMethodSuffix}";
            var onConfigMeth = binding.GetModel().GetType().GetTypeInfo().GetMethod(
                onConfigName, OnBindCommandParams);

            // Add the option based on whether there is a config method
            Action <CommandLineApplication> configAction = cla => { };

            if (onConfigMeth != null)
            {
                configAction = cla => onConfigMeth.Invoke(binding.GetModel(), new[] { cla });
            }

            var subCla = binding._cla.Command(cmdName, configAction, a.ThrowOnUnexpectedArg);

            // When a sub-command is specified, its OnExecute handler is invoked instead of the
            // parent's so we inject a post-exec action to invoke the parent's post-exec actions
            Action parentPostExec = () => binding.PostExec(true);
            var    subBinding     = CommandLineBinding.BindModel(cmdType, subCla,
                                                                 binding.GetModel(), parentPostExec);

            subBinding._parentBinding = binding;
            binding._childBindings.Add(subBinding);

            // This is already invoked by Apply which is invoke by Build
            //subModelSetCLA.Invoke(null, new object[] { subModel, cmdType.GetTypeInfo()
            //        .GetCustomAttribute<CommandLineApplicationAttribute>() });

            // We need to make sure the command name from the Command attr is preserved  after
            // processing of the optional CLA attr on the subclass which may have its own name
            subCla.Name = cmdName;

            binding._postExecActions.Add(() => HandleCommand(subCla, subBinding, m,
                                                             () => subBinding.GetModel()));
        }