Exemple #1
0
        public bool Interpret(string command, Register <CommandWrapper <CommandState> > set, IOutput output, out CommandState state)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (set == null)
            {
                throw new ArgumentNullException(nameof(set));
            }
            state = new CommandState(output);
            InputCommand <CommandState>   inputcommand   = Parse(command);
            CommandWrapper <CommandState> commandwrapper = set.Find(inputcommand.Name);

            if (commandwrapper == null)
            {
                state.WriteLine(string.Format(CommandMessagPack["Command/Missing"], commandwrapper.Name));
                state.ParseError = true;
                return(state.Success);
            }
            return(Execute(inputcommand, commandwrapper.Command, state));
        }
Exemple #2
0
        private void SearchArguments(InputCommand <CommandState> inputcommand, Command <CommandState> command, CommandState state)
        {
            List <string> requiered = new List <string>(command.GetRquieredArguments());

            foreach (InputArgument <CommandState> arg in inputcommand.Args)
            {
                state.WriteLine(string.Format(CommandMessagPack["Arg/Name"], arg.Name));
                Argument <CommandState> argument = null;
                if (arg.IsFullname)
                {
                    argument = command.Find(arg.Name);
                }
                else
                {
                    argument = command.Find(arg.Name[0]);
                }
                if (argument == null)
                {
                    state.WriteLine(string.Format(CommandMessagPack["Arg/Missing"], arg.Name));
                    state.ParseError = true;
                }
                else if (string.IsNullOrEmpty(arg.Parameter) && !argument.Empty)
                {
                    state.WriteLine(string.Format(CommandMessagPack["Arg/Parameter/Missing"], argument.Name));
                    state.ParseError = true;
                }
                else if (argument.Empty)
                {
                    state.WriteLine(string.Format(CommandMessagPack["Arg/Parameter/none"], arg.Name));
                    state.ParseError = true;
                }
                else
                {
                    state.WriteLine(string.Format(CommandMessagPack["Arg/Parameter"], arg.Name, arg.Parameter));
                    arg.Attached = argument;
                    if (argument.Requiered && !requiered.Remove(argument.Name))
                    {
                        state.WriteLine(string.Format(CommandMessagPack["Arg/Missing/InList"], argument.Name));
                        state.ParseError = true;
                    }
                }
            }
            if (requiered.Count > 0)
            {
                state.WriteLine(string.Format(CommandMessagPack["Command/Args/Missing"], command.Name));
                foreach (string missingarg in requiered)
                {
                    state.WriteLine(string.Format("[{0}]", missingarg));
                }
                state.ParseError = true;
            }
        }
Exemple #3
0
 public bool Execute(InputCommand <CommandState> inputcommand, Command <CommandState> command, CommandState state)
 {
     if (inputcommand == null)
     {
         throw new ArgumentNullException(nameof(inputcommand));
     }
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     state.WriteLine(string.Format(CommandMessagPack["Command/Name"], command.Name));
     try
     {
         command.PreExecute?.Invoke(state);
     }
     catch (Exception exception)
     {
         state.RuntimeError = true;
         state.WriteLine(exception.Message);
     }
     if (state.Error)
     {
         state.WriteLine(string.Format(CommandMessagPack["Command/Skipping"], command.Name));
     }
     else if (command.TotalArguments == 0 && inputcommand.Args.Count > 0)
     {
         state.WriteLine(string.Format(CommandMessagPack["Command/NoArgs"], command.Name));
     }
     else
     {
         SearchArguments(inputcommand, command, state);
     }
     if (state.Success)
     {
         try
         {
             state.WriteLine(string.Format(CommandMessagPack["Command/Executing"], command.Name));
             foreach (InputArgument <CommandState> args in inputcommand.Args)
             {
                 args.Attached?.Action.Invoke(state, args.Parameter);
                 if (state.Error)
                 {
                     break;
                 }
             }
             if (state.Success)
             {
                 command.PostExecute.Invoke(state);
             }
         }
         catch (Exception exception)
         {
             state.RuntimeError = true;
             state.WriteLine(exception.Message);
         }
         if (state.Error)
         {
             state.WriteLine(string.Format(CommandMessagPack["Command/Failed"], command.Name));
         }
         else
         {
             state.WriteLine(string.Format(CommandMessagPack["Command/Executed"], command.Name));
         }
     }
     return(state.Success);
 }