public CommandMethodUsagePrinter(string name, object instance, string[] aliases)
        {
            var provider = CommandDescriptor.GetUsageDescriptionProvider(instance.GetType());

            this.Name        = name ?? throw new ArgumentNullException(nameof(name));
            this.Instance    = instance ?? throw new ArgumentNullException(nameof(instance));
            this.Aliases     = aliases ?? throw new ArgumentNullException(nameof(aliases));
            this.Summary     = provider.GetSummary(instance);
            this.Description = provider.GetDescription(instance);
            this.Example     = provider.GetExample(instance);
        }
Ejemplo n.º 2
0
        protected virtual void PrintUsage(CommandUsage usage)
        {
            var descriptors = CommandDescriptor.GetMemberDescriptors(this);
            var printer     = new CommandMemberUsagePrinter(this.ExecutionName, this, this.Aliases)
            {
                Usage           = usage,
                IsAnsiSupported = this.IsAnsiSupported
            };

            printer.Print(this.Out, descriptors.ToArray());
        }
Ejemplo n.º 3
0
 public void Invoke(string[] args)
 {
     try
     {
         var first    = args.FirstOrDefault() ?? string.Empty;
         var rest     = args.Count() > 0 ? args.Skip(1).ToArray() : new string[] { };
         var instance = this.Instance;
         if (instance is ICommandHierarchy hierarchy && hierarchy.Commands.ContainsKey(first) == true)
         {
             var command = hierarchy.Commands[first];
             var parser  = new CommandLineParser(first, command);
             parser.Parse(rest);
             if (command is IExecutable executable1)
             {
                 this.Invoke(executable1);
             }
             else if (command is IExecutableAsync executable2)
             {
                 throw new InvalidOperationException(Resources.Exception_InvokeAsyncInstead);
             }
         }
         else if (instance is IExecutable executable1)
         {
             this.Parse(args);
             this.Invoke(executable1);
         }
         else if (instance is IExecutableAsync executable2)
         {
             this.Parse(args);
             throw new InvalidOperationException(Resources.Exception_InvokeAsyncInstead);
         }
         else if (CommandDescriptor.GetMethodDescriptor(instance.GetType(), first) is CommandMethodDescriptor descriptor)
         {
             if (descriptor.IsAsync == true)
             {
                 throw new InvalidOperationException(Resources.Exception_InvokeAsyncInstead);
             }
             else
             {
                 this.Invoke(descriptor, instance, rest);
             }
         }
     }
 private string[] GetCompletion(ICommand item, string[] args, string find)
 {
     if (item is ICommandCompletor completor)
     {
         var members = CommandDescriptor.GetMemberDescriptors(item);
         var context = CommandCompletionContext.Create(item, members, args, find);
         if (context is CommandCompletionContext completionContext)
         {
             var completion = completor.GetCompletions(completionContext);
             if (completion != null)
             {
                 return(completion);
             }
         }
         else if (context is string[] completions)
         {
             return(completions);
         }
     }
     return(null);
 }
Ejemplo n.º 5
0
 protected CommandMemberDescriptor GetStaticDescriptor(Type type, string propertyName)
 {
     return(CommandDescriptor.GetStaticMemberDescriptors(type)[propertyName]);
 }
Ejemplo n.º 6
0
 protected CommandMemberDescriptor GetDescriptor(string propertyName)
 {
     return(CommandDescriptor.GetMemberDescriptors(this)[propertyName]);
 }