Exemple #1
0
std::vector<std::string> HelpCmd::complete(const std::string& cmdLine) const
{
  // TODO: complete last begun parameter
  std::vector<std::string> commandWithArgs = split(cmdLine);
  std::vector<std::string> result;
  size_t lastIdx = commandWithArgs.size() - 1;

  // indicates that we want to find a new parameter
  if(*(--cmdLine.end()) == ' ')
  {
    ++lastIdx;
    commandWithArgs.push_back("");
  }

  std::vector<std::string> cmds = Commands::getInstance().getAllCommandNames();
  for(size_t i = 0; i < cmds.size(); ++i)
  {
    if(startsWidth(cmds[i], commandWithArgs[lastIdx]))
    {
      std::vector<std::string> v = commandWithArgs;
      v[lastIdx] = cmds[i];
      std::string s = join(v);
      result.push_back(s);
    }
  }

  return result;
}
std::vector<std::string> Commands::complete(const std::string& cmdLine)
{
  std::vector<std::string> commandWithArgs = split(cmdLine);
  std::vector<std::string> result;
  if(commandWithArgs.size() > 1 || (!cmdLine.empty() && *(--cmdLine.end()) == ' '))
  {
    // parameter completion (this is the job of the individual Command instaces)
    std::map<std::string, Command*>::const_iterator i = commands.find(commandWithArgs[0]);
    if(i != commands.end())
    {
      std::vector<std::string> completionResult = i->second->complete(cmdLine);
      result.insert(result.begin(), completionResult.begin(), completionResult.end());
    }

    /* For backward compatibility: Check if the completion strings start with the
     * given command and prepend it if not. Assumes that the prefix is missing
     * in every string if it is missing in the first one.
     */
    if(!result.empty() && !startsWidth(result[0], commandWithArgs[0]))
    {
      for(size_t i = 0; i < result.size(); ++i)
        result[i] = commandWithArgs[0] + " " + result[i];
    }
  }
  else
  {
    // command completion
    for(std::map<std::string, Command*>::const_iterator i = commands.begin();
        i != commands.end(); i++)
    {
      if(startsWidth(i->first, cmdLine))
        result.push_back(i->first);
    }
  }
  return result;
}