private void PrintOption(CommandTextWriter writer, CommandMemberDescriptor descriptor, bool isLast)
 {
     if (descriptor.IsSwitch == true)
     {
         writer.WriteLine(descriptor.DisplayName);
     }
     else if (descriptor.DefaultValue == null)
     {
         writer.WriteLine($"{descriptor.DisplayName} 'value' [default: null]");
     }
     else if (descriptor.DefaultValue != DBNull.Value)
     {
         writer.WriteLine($"{descriptor.DisplayName} 'value' [default: '{descriptor.DefaultValue}']");
     }
     else
     {
         writer.WriteLine($"{descriptor.DisplayName} 'value'");
     }
     if (descriptor.Summary != string.Empty && this.Usage != CommandUsage.Simple)
     {
         writer.Indent++;
         writer.WriteMultiline(descriptor.Summary);
         writer.Indent--;
     }
     if (isLast == false)
     {
         writer.WriteLine();
     }
 }
Beispiel #2
0
 internal string[] GetCompletionInternal(object instance, CommandMemberDescriptor memberDescriptor, string find)
 {
     if (memberDescriptor.GetCompletionInternal(instance, find) is string[] items)
     {
         return(items);
     }
     return(this.GetCompletion(instance, new object[] { memberDescriptor, find }));
 }
 private CommandCompletionContext(ICommand command, CommandMemberDescriptor member, string[] args, string find, Dictionary <string, object> properties)
 {
     this.Command          = command;
     this.MemberDescriptor = member;
     this.Arguments        = args;
     this.Find             = find;
     this.Properties       = properties;
 }
 private void PrintVariables(CommandTextWriter writer, CommandMemberDescriptor descriptor)
 {
     writer.WriteLine(descriptor.DisplayName);
     if (descriptor.Summary != string.Empty)
     {
         writer.Indent++;
         writer.WriteMultiline(descriptor.Summary);
         writer.Indent--;
     }
 }
        private static object ParseArray(CommandMemberDescriptor descriptor, string arg)
        {
            System.Collections.IList list;
            if (descriptor.MemberType.IsArray == true)
            {
                list = new System.Collections.ArrayList() as System.Collections.IList;
            }
            else
            {
                list = TypeDescriptor.CreateInstance(null, descriptor.MemberType, null, null) as System.Collections.IList;
            }

            var itemType = GetItemType(descriptor.MemberType);

            if (itemType == null)
            {
                throw new NotSupportedException();
            }

            var segments = arg.Split(new char[] { CommandSettings.ItemSperator });

            try
            {
                var converter = TypeDescriptor.GetConverter(itemType);
                foreach (var item in segments)
                {
                    var s = item.Trim();
                    if (s.Length == 0)
                    {
                        continue;
                    }
                    var element = converter.ConvertFromString(s);
                    list.Add(element);
                }

                if (descriptor.MemberType.IsArray == true)
                {
                    var array = Array.CreateInstance(itemType, list.Count);
                    list.CopyTo(array, 0);
                    list = array as System.Collections.IList;
                }
                else
                {
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException(Resources.Exception_InvalidArgumentType, descriptor.Name, e);
            }
            return(list);
        }
 private void PrintRequirement(CommandTextWriter writer, CommandMemberDescriptor descriptor, bool isLast)
 {
     writer.WriteLine(descriptor.DisplayName);
     if (descriptor.Summary != string.Empty)
     {
         writer.Indent++;
         writer.WriteMultiline(descriptor.Summary);
         writer.Indent--;
     }
     if (isLast == false)
     {
         writer.WriteLine();
     }
 }
 public virtual void Print(TextWriter writer, CommandMemberDescriptor descriptor)
 {
     if (writer is CommandTextWriter commandWriter)
     {
         this.Print(commandWriter, descriptor);
     }
     else
     {
         using var tw = new CommandTextWriter()
               {
                   IsAnsiSupported = this.IsAnsiSupported
               };;
         this.Print(tw, descriptor);
         writer.Write(tw.ToString());
     }
 }
        private string GetString(CommandMemberDescriptor descriptor)
        {
            var patternText = descriptor.DisplayName;

            if (descriptor.IsRequired == true)
            {
                var descriptorName = descriptor.DisplayName;
                if (descriptorName == string.Empty)
                {
                    descriptorName = CommandSettings.NameGenerator(descriptor.DescriptorName);
                }

                if (descriptor.InitValue == DBNull.Value)
                {
                    if (descriptor.IsExplicit == true)
                    {
                        return($"<{patternText} 'value'>");
                    }
                    else
                    {
                        return($"<{descriptorName}>");
                    }
                }
                else
                {
                    var value = descriptor.InitValue ?? "null";
                    if (descriptor.IsExplicit == true)
                    {
                        return($"<{patternText} {descriptorName}, default='{value}'>");
                    }
                    else
                    {
                        return($"<{descriptorName}, default='{value}'>");
                    }
                }
            }
            else if (descriptor.IsSwitch == false)
            {
                return($"[{patternText} 'value']");
            }
            else
            {
                return($"[{patternText}]");
            }
        }
        private static object ParseEnum(CommandMemberDescriptor descriptor, string arg)
        {
            var segments = arg.Split(new char[] { CommandSettings.ItemSperator });
            var names    = Enum.GetNames(descriptor.MemberType).ToDictionary(item => CommandSettings.NameGenerator(item), item => item);
            var nameList = new List <string>(segments.Length);

            foreach (var item in segments)
            {
                if (names.ContainsKey(item) == false)
                {
                    throw new InvalidOperationException(string.Format(Resources.Exception_InvalidValue_Format, arg));
                }

                nameList.Add(names[item]);
            }

            return(Enum.Parse(descriptor.MemberType, string.Join(", ", nameList.ToArray())));
        }
        private static object ParseDefault(CommandMemberDescriptor descriptor, string arg)
        {
            var converter = TypeDescriptor.GetConverter(descriptor.MemberType);

            if (converter.CanConvertFrom(typeof(string)) == false)
            {
                throw new NotSupportedException(string.Format(Resources.Exception_CannotConvert_Format, arg));
            }

            try
            {
                return(converter.ConvertFrom(arg));
            }
            catch (Exception e)
            {
                throw new ArgumentException(Resources.Exception_InvalidArgumentType, descriptor.Name, e);
            }
        }
 public static object Parse(CommandMemberDescriptor descriptor, string arg)
 {
     if (descriptor.MemberType.IsArray == true || typeof(System.Collections.IList).IsAssignableFrom(descriptor.MemberType) == true)
     {
         return(ParseArray(descriptor, arg));
     }
     else if (descriptor.MemberType == typeof(bool))
     {
         return(ParseBoolean(descriptor, arg));
     }
     else if (descriptor.MemberType.IsEnum)
     {
         return(ParseEnum(descriptor, arg));
     }
     else
     {
         return(ParseDefault(descriptor, arg));
     }
 }
        private void PrintRequirement(CommandTextWriter writer, CommandMemberDescriptor descriptor)
        {
            if (descriptor.IsRequired == true)
            {
                writer.WriteLine(descriptor.DisplayName);
            }
            else
            {
                writer.WriteLine(descriptor.DisplayName);
            }

            var description = descriptor.Summary != string.Empty ? descriptor.Summary : descriptor.Description;

            if (description != string.Empty)
            {
                writer.Indent++;
                writer.WriteMultiline(description);
                writer.Indent--;
            }
        }
 private void PrintVariables(CommandTextWriter writer, CommandMemberDescriptor descriptor)
 {
     writer.BeginGroup(descriptor.Name + " ...");
     writer.WriteMultiline(descriptor.Description);
     writer.EndGroup();
 }
 private void PrintUsage(CommandTextWriter writer, CommandMethodDescriptor _, CommandMemberDescriptor memberDescriptor)
 {
     this.BeginGroup(writer, Resources.Text_Usage);
     this.PrintOption(writer, memberDescriptor, false);
     this.EndGroup(writer);
 }
 private void Print(CommandTextWriter writer, CommandMethodDescriptor descriptor, CommandMemberDescriptor memberDescriptor)
 {
     this.PrintSummary(writer, descriptor, memberDescriptor);
     this.PrintDescription(writer, descriptor, memberDescriptor);
     this.PrintUsage(writer, descriptor, memberDescriptor);
 }
 private void PrintDescription(CommandTextWriter writer, CommandMethodDescriptor _, CommandMemberDescriptor memberDescriptor)
 {
     writer.BeginGroup(Resources.Text_Description);
     writer.WriteMultiline(memberDescriptor.Description);
     writer.EndGroup();
 }
 private void PrintUsage(CommandTextWriter writer, CommandMemberDescriptor descriptor)
 {
     writer.BeginGroup(Resources.Text_Usage);
     writer.WriteLine(this.GetString(descriptor));
     writer.EndGroup();
 }
 private void PrintDescription(CommandTextWriter writer, CommandMemberDescriptor descriptor)
 {
     writer.BeginGroup(Resources.Text_Description);
     writer.WriteLine(descriptor.Description);
     writer.EndGroup();
 }
 public static object ParseArray(CommandMemberDescriptor descriptor, IEnumerable <string> args)
 {
     return(ParseArray(descriptor.MemberType, args));
 }
 private void PrintSummary(CommandTextWriter writer, CommandMemberDescriptor descriptor)
 {
     writer.BeginGroup(Resources.Text_Summary);
     writer.WriteLine(descriptor.Summary);
     writer.EndGroup();
 }
 private static object ParseBoolean(CommandMemberDescriptor descriptor, string arg)
 {
     return(ParseDefault(descriptor, arg));
 }
Beispiel #22
0
 public ParseDescriptorItem(CommandMemberDescriptor descriptor)
 {
     this.Descriptor = descriptor;
 }
Beispiel #23
0
 public SubCommandPropertyDescriptor(object instance, CommandMemberDescriptor descriptor)
     : base(new CommandPropertyAttribute(), descriptor.DescriptorName)
 {
     this.instance   = instance;
     this.descriptor = descriptor;
 }