private SwitchDefinition FindSwitch(CommandDefinition cdef, string tok)
 {
     SwitchDefinition swdef = null;
     var swname = tok.Substring(SwitchPrefix.Length);
     if (cdef.Switches.TryGetValue(swname, out swdef))
     {
         return swdef;
     }
     return null;
 }
 public void AddLegacyAlias(string alias, string commandName)
 {
     var newCmdDef = Commands[commandName];
     var oldCmdDef = new CommandDefinition(alias, newCmdDef.MinArgs, newCmdDef.MaxArgs, newCmdDef.Switches.Values.ToArray());
     oldCmdDef.Description =
         String.Format("This command is deprecated. Please use the command {0}.", commandName); // TODO move this into a resource.
     oldCmdDef.Category = newCmdDef.Category;
     oldCmdDef.Action = (args, switches) =>
         {
             Output.CommandDeprecated(oldCmdDef, newCmdDef);
             return newCmdDef.Action(args, switches);
         };
     Alias.Add(alias, oldCmdDef);
 }
 public void CommandDeprecated(CommandDefinition old, CommandDefinition newCmd)
 {
     LogWarning("The command {0} is deprecated. Please use the command {1} instead.", old.Name, newCmd.Name);
 }
 public void CommandDeprecated(CommandDefinition old, CommandDefinition newCmd)
 {
     LogWarning("The command {0} is deprecated. Please use the command {1} instead.", old.Name, newCmd.Name);
 }
        public void CommandUsage(CommandDefinition commandDefiniton, Func<SwitchDefinition,string> switchUsage)
        {
            var indent = "    ";
            var switchSyntax = 
                from sw in commandDefiniton.Switches.Values
                orderby sw.Name
                where sw.Undocumented == false
                select switchUsage(sw);

            LogMessage("NAME");
            LogMessage(indent + commandDefiniton.Name);
            LogMessage("");
            if(commandDefiniton.Category != CommandCategory.None)
            {
                LogMessage("CATEGORY");
                LogMessage(indent + commandDefiniton.Category);
                LogMessage("");
            }
            LogMessage("SYNOPSIS");
            LogMessage(indent + commandDefiniton.Description);
            LogMessage("");
            LogMessage("SYNTAX");
            LogMessage(indent + commandDefiniton.Name + " " + String.Join(" ", switchSyntax.ToArray()));
            LogMessage("");
        }
 private bool TryGetCommand(string cmd, out CommandDefinition cmdDef)
 {
     if(Commands.TryGetValue(cmd, out cmdDef))
     {
         return true;
     }
     return Alias.TryGetValue(cmd,out cmdDef);
 }
 private bool ParseCommand(CommandDefinition cmdDef, IEnumerable<string> commandArgs)
 {
     List<string> toks = new List<string>(commandArgs);
     List<string> unnamedArgs = new List<string>();
     Dictionary<string, string> switches = new Dictionary<string, string>();
     if (ParseArguments(cmdDef, toks, unnamedArgs, switches) &&
         ValidateArguments(cmdDef, unnamedArgs, switches))
     {
         if (cmdDef.Action != null)
         {
             return cmdDef.Action(unnamedArgs, switches);
         }
         else
         {
             return true;
         }
     }
     else
     {
         return false;
     }
     
 }
        private bool ValidateArguments(
            CommandDefinition cmdDef,
            List<string> unnamedArgs, Dictionary<string, string> switches)
        {
            bool success = true;
            int found = unnamedArgs.Count;
            if (found < cmdDef.MinArgs)
            {
                Output.CommandTooFewArguments(cmdDef.Name, cmdDef.MinArgs, found, unnamedArgs);
                success = false;
            }
            else if (cmdDef.MaxArgs.HasValue)
            {
                int expected = cmdDef.MaxArgs.Value;
                if (found > expected)
                {
                    Output.CommandTooManyArguments(cmdDef.Name, expected, found , unnamedArgs);
                    success = false;
                }
            }

            foreach (var sw in cmdDef.Switches.Values)
            {
                if (!switches.ContainsKey(sw.Name))
                {
                    if (sw.Required)
                    {
                        Output.CommandMissingParam(cmdDef.Name, SwitchUsage(sw));
                        success = false;
                    }
                    else
                    {
                        switches.Add(sw.Name,null);
                    }
                }
            }
            return success;
        }
 private bool ParseArguments(
     CommandDefinition cmdDef, List<string> toks,
     List<string> unnamedArgs, Dictionary<string, string> switches)
 {
     while (toks.Count > 0)
     {
         var tok = Shift(toks);
         if (IsSwitch(tok))
         {
             var swdef = FindSwitch(cmdDef, tok);
             if (swdef == null)
             {
                 Output.CommandParamUnknown(cmdDef.Name, tok);
                 return false;
             }
             if (switches.ContainsKey(swdef.Name))
             {
                 var keyValue = switches[swdef.Name];
                 Output.CommandDuplicateParam(SwitchUsage(swdef));
                 return false;
             }
             if (swdef.IsFlag)
             {
                 switches.Add(swdef.Name, true.ToString());
             }
             else if (toks.Count > 0)
             {
                 var switchArg = Shift(toks);
                 switches.Add(swdef.Name, switchArg);
             }
             else
             {
                 Output.CommandParamMissingArgument(cmdDef.Name, SwitchUsage(swdef));
                 return false;
             }
         }
         else
         {
             unnamedArgs.Add(tok);
         }
     }
     return true;
 }