size_t OptionValueArray::GetArgs(Args &args) const { const uint32_t size = m_values.size(); std::vector<const char *> argv; for (uint32_t i = 0; i < size; ++i) { const char *string_value = m_values[i]->GetStringValue(); if (string_value) argv.push_back(string_value); } if (argv.empty()) args.Clear(); else args.SetArguments(argv.size(), &argv[0]); return args.GetArgumentCount(); }
void CommandInterpreter::BuildAliasCommandArgs ( CommandObject *alias_cmd_obj, const char *alias_name, Args &cmd_args, CommandReturnObject &result ) { OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); if (option_arg_vector_sp.get()) { // Make sure that the alias name is the 0th element in cmd_args std::string alias_name_str = alias_name; if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) cmd_args.Unshift (alias_name); Args new_args (alias_cmd_obj->GetCommandName()); if (new_args.GetArgumentCount() == 2) new_args.Shift(); OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); int old_size = cmd_args.GetArgumentCount(); int *used = (int *) malloc ((old_size + 1) * sizeof (int)); memset (used, 0, (old_size + 1) * sizeof (int)); used[0] = 1; for (int i = 0; i < option_arg_vector->size(); ++i) { OptionArgPair option_pair = (*option_arg_vector)[i]; std::string option = option_pair.first; std::string value = option_pair.second; if (option.compare ("<argument>") == 0) new_args.AppendArgument (value.c_str()); else { new_args.AppendArgument (option.c_str()); if (value.compare ("<no-argument>") != 0) { int index = GetOptionArgumentPosition (value.c_str()); if (index == 0) // value was NOT a positional argument; must be a real value new_args.AppendArgument (value.c_str()); else if (index >= cmd_args.GetArgumentCount()) { result.AppendErrorWithFormat ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", index); result.SetStatus (eReturnStatusFailed); return; } else { new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); used[index] = 1; } } } } for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) { if (!used[j]) new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); } cmd_args.Clear(); cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); } else { result.SetStatus (eReturnStatusSuccessFinishNoResult); // This alias was not created with any options; nothing further needs to be done. return; } result.SetStatus (eReturnStatusSuccessFinishNoResult); return; }