예제 #1
0
        internal static CommandLineAction Create(PropertyInfo actionProperty, List<string> knownAliases)
        {
            var ret = PropertyInitializer.CreateInstance<CommandLineAction>();
            ret.ActionMethod = ArgAction.ResolveMethod(actionProperty.DeclaringType, actionProperty);
            ret.Source = actionProperty;
            ret.Arguments.AddRange(new CommandLineArgumentsDefinition(actionProperty.PropertyType).Arguments);
            ret.IgnoreCase = true;

            if (actionProperty.DeclaringType.HasAttr<ArgIgnoreCase>() && actionProperty.DeclaringType.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (actionProperty.HasAttr<ArgIgnoreCase>() && actionProperty.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            ret.Metadata.AddRange(actionProperty.Attrs<IArgMetadata>().AssertAreAllInstanceOf<ICommandLineActionMetadata>());

            // This line only calls into CommandLineArgument because the code to strip 'Args' off the end of the
            // action property name lives here.  This is a pre 2.0 hack that's only left in place to support apps that
            // use the 'Args' suffix pattern.
            ret.Aliases.AddRange(CommandLineArgument.FindDefaultShortcuts(actionProperty, knownAliases, ret.IgnoreCase));

            return ret;
        }
예제 #2
0
        public static string GetShortcut(PropertyInfo info)
        {
            var actionProperty = ArgAction.GetActionProperty(info.DeclaringType);
            if (actionProperty != null && actionProperty.Name == info.Name) return null;

            var attr = info.Attr<ArgShortcut>();

            if (attr == null) return info.GetArgumentName()[0] + "";
            else return attr.Shortcut;
        }
예제 #3
0
        private static string GetShortcutInternal(PropertyInfo info, List<string> knownShortcuts)
        {
            var actionProperty = ArgAction.GetActionProperty(info.DeclaringType);
            if (actionProperty != null && actionProperty.Name == info.Name) return null;

            var attr = info.Attr<ArgShortcut>();

            if (attr == null)
            {
                string shortcutVal = "";
                foreach (char c in info.GetArgumentName())
                {
                    shortcutVal += c;
                    if (knownShortcuts.Contains(shortcutVal) == false) return shortcutVal;
                }
                return shortcutVal;
            }
            else
            {
                if (attr.policy.HasValue && attr.policy.Value == ArgShortcutPolicy.NoShortcut && attr.Shortcut != null)
                {
                    throw new InvalidArgDefinitionException("You cannot specify a shortcut value and an ArgShortcutPolicy of NoShortcut");
                }

                if (attr.Shortcut == null) return null;
                if (attr.Shortcut.StartsWith("-")) attr.Shortcut = attr.Shortcut.Substring(1);
                else if (attr.Shortcut.StartsWith("/")) attr.Shortcut = attr.Shortcut.Substring(1);
                return attr.Shortcut;
            }
        }
예제 #4
0
        internal static bool IsArgument(PropertyInfo property)
        {
            if (property.HasAttr<ArgIgnoreAttribute>()) return false;
            if (CommandLineAction.IsActionImplementation(property)) return false;

            if (property.Name == Constants.ActionPropertyConventionName &&
                property.HasAttr<ArgPosition>() &&
                property.Attr<ArgPosition>().Position == 0 &&
                property.HasAttr<ArgRequired>())
            {
                return false;
            }

            return true;
        }
예제 #5
0
        internal static CommandLineArgument Create(PropertyInfo property, List<string> knownAliases)
        {
            var ret = PropertyInitializer.CreateInstance<CommandLineArgument>();
            ret.DefaultValue = property.HasAttr<DefaultValueAttribute>() ? property.Attr<DefaultValueAttribute>().Value : null;
            ret.Position = property.HasAttr<ArgPosition>() ? property.Attr<ArgPosition>().Position : -1;
            ret.Source = property;
            ret.ArgumentType = property.PropertyType;

            ret.IgnoreCase = true;

            if (property.DeclaringType.HasAttr<ArgIgnoreCase>() && property.DeclaringType.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (property.HasAttr<ArgIgnoreCase>() && property.Attr<ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            ret.Aliases.AddRange(FindDefaultShortcuts(property, knownAliases, ret.IgnoreCase));

            // TODO - I think the first generic call can just be more specific
            ret.Metadata.AddRange(property.Attrs<IArgMetadata>().AssertAreAllInstanceOf<ICommandLineArgumentMetadata>());

            return ret;
        }
예제 #6
0
        private static void SetPropertyFromTextValue(ParserContext context, ConsoleControl control, PropertyInfo property, string textValue)
        {
            bool isObservable = textValue.StartsWith("{") && textValue.EndsWith("}");

            if (isObservable)
            {
                var observablePropertyName = textValue.Substring(1, textValue.Length - 2);
                var viewModelObservable = context.CurrentViewModel as ObservableObject;
                if (viewModelObservable == null) throw new InvalidOperationException("View model is not observable");
                new ViewModelBinding(control, property, viewModelObservable, observablePropertyName);
            }
            else if(property.HasAttr<MarkupPropertyAttribute>())
            {
                property.Attr<MarkupPropertyAttribute>().Processor.Process(context);
            }
            else if (property.PropertyType == typeof(string))
            {
                property.SetValue(control, textValue);
            }
            else if (property.PropertyType == typeof(ConsoleString))
            {
                property.SetValue(control, new ConsoleString(textValue));
            }
            else if (property.PropertyType.IsEnum)
            {
                var enumVal = Enum.Parse(property.PropertyType, textValue);
                property.SetValue(control, enumVal);
            }
            else if (property.PropertyType == typeof(Event))
            {
                Event ev = property.GetValue(control) as Event;
                var target = context.CurrentViewModel;
                Action handler = () =>
                {
                    var method = target.GetType().GetMethod(textValue, new Type[0]);
                    if (method != null)
                    {
                        method.Invoke(target, new object[0]);
                    }
                    else
                    {
                        var action = target.GetType().GetProperty(textValue);
                        if (action == null || action.PropertyType != typeof(Action))
                        {
                            throw new InvalidOperationException("Not a method or action");
                        }

                        ((Action)action.GetValue(target)).Invoke();
                    }
                };

                ev.SubscribeForLifetime(handler, control.LifetimeManager);
            }
            else
            {
                var parseMethod = property.PropertyType.GetMethod("Parse", new Type[] { typeof(string) });
                var parsed = parseMethod.Invoke(null, new object[] { textValue });
                property.SetValue(control, parsed);
            }
        }
        private static string GetShortcutInternal(PropertyInfo info, List<string> knownShortcuts)
        {
            var actionProperty = ArgAction.GetActionProperty(info.DeclaringType);
            if (actionProperty != null && actionProperty.Name == info.Name) return null;

            var attr = info.Attr<ArgShortcut>();

            if (attr == null)
            {
                string shortcutVal = "";
                foreach (char c in info.GetArgumentName())
                {
                    shortcutVal += c;
                    if (knownShortcuts.Contains(shortcutVal) == false) return shortcutVal;
                }
                return shortcutVal;
            }
            else
            {
                if (attr.Shortcut == null) return null;
                if (attr.Shortcut.StartsWith("-")) attr.Shortcut = attr.Shortcut.Substring(1);
                else if (attr.Shortcut.StartsWith("/")) attr.Shortcut = attr.Shortcut.Substring(1);
                return attr.Shortcut;
            }
        }