private async Task DispatchGroup(OperationsSession session, string groupOrRootCommand) { // Commands are classes with the following naming convention // NuCmd.Commands.<CommandName>Command // OR: // NuCmd.Commands.<Group>.<CommandName>Command // Find the group being referenced IReadOnlyDictionary<string, CommandDefinition> groupMembers; string commandName; if (_directory.Groups.TryGetValue(groupOrRootCommand, out groupMembers)) { commandName = _args.FirstOrDefault(); _args = _args.Skip(1); } else { commandName = groupOrRootCommand; groupOrRootCommand = null; groupMembers = _directory.RootCommands; } commandName = commandName ?? String.Empty; CommandDefinition command; if (!groupMembers.TryGetValue(commandName, out command)) { if (String.IsNullOrEmpty(groupOrRootCommand)) { await _console.WriteErrorLine(String.Format( CultureInfo.CurrentCulture, Strings.Program_NoSuchCommand, commandName)); } else { await _console.WriteErrorLine(String.Format( CultureInfo.CurrentCulture, Strings.Program_NoSuchCommandInGroup, commandName, groupOrRootCommand)); } } else { await Dispatch(session, command); } }
private async Task Dispatch(OperationsSession session, CommandDefinition definition) { ICommand cmd = null; Exception thrown = null; try { cmd = Args.Parse(definition.Type, _args.ToArray()) as ICommand; } catch (AggregateException aex) { thrown = aex.InnerException; } catch (Exception ex) { thrown = ex; } if (thrown != null) { await _console.WriteErrorLine(thrown.Message); await new HelpCommand().HelpFor(_console, definition); } else if (cmd == null) { await _console.WriteErrorLine( Strings.Program_CommandNotConvertible, definition.Type.FullName, typeof(ICommand).FullName); } else { thrown = null; try { await cmd.Execute(session, _console, definition, _directory); } catch (AggregateException aex) { thrown = aex.InnerException; } catch (OperationCanceledException) { // Do nothing when this is thrown, it's just used to jump out of the job. } catch (Exception ex) { thrown = ex; } if (thrown != null) { await _console.WriteErrorLine(thrown.ToString()); } } }