Skip to content

Commit

Permalink
Merge pull request #16 from ZhengqiaoWang/15-bug-position-argument-re…
Browse files Browse the repository at this point in the history
…sorted

fix: position argument priority
  • Loading branch information
ZhengqiaoWang authored Nov 2, 2022
2 parents 19a1c4a + ebe577f commit b203ece
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions interface/jargs_parser_api.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @file jargs_parser_api.hpp
* @brief A header-only c++ argument parser lib
* @version 1.0.0
* @date 2022-10-26
* @version 1.0.2
* @date 2022-11-01
*
* @url https://github.com/ZhengqiaoWang/JArgsParser
*
Expand Down Expand Up @@ -239,22 +239,23 @@ namespace Joger
{
case ArgsType::POSITION:
{
m_key_func[key] = std::bind(&JArgsParser::posArgParser, this, key, args_desc);
m_key_func[0][key] = std::bind(&JArgsParser::posArgParser, this, key, args_desc);
m_pos_key_priority_vec.emplace_back(key);
break;
}
case ArgsType::FLAG:
{
m_key_func[key] = std::bind(&JArgsParser::flagArgParser, this, key, args_desc);
m_key_func[1][key] = std::bind(&JArgsParser::flagArgParser, this, key, args_desc);
break;
}
case ArgsType::VALUE:
{
m_key_func[key] = std::bind(&JArgsParser::valArgParser, this, key, args_desc);
m_key_func[1][key] = std::bind(&JArgsParser::valArgParser, this, key, args_desc);
break;
}
case ArgsType::ACTION:
{
m_key_func[key] = std::bind(&JArgsParser::actArgParser, this, key, args_desc);
m_key_func[1][key] = std::bind(&JArgsParser::actArgParser, this, key, args_desc);
break;
}

Expand Down Expand Up @@ -304,9 +305,18 @@ namespace Joger
exit(0);
}

for (auto iter : m_key_func)
for (auto &key : m_pos_key_priority_vec)
{
if (false == iter.second())
if (false == m_key_func[0][key]())
{
showHelp();
return false;
}
}

for (auto &arg_pair : m_key_func[1])
{
if (false == arg_pair.second())
{
showHelp();
return false;
Expand Down Expand Up @@ -586,7 +596,8 @@ namespace Joger
std::vector<std::string> m_argv_vec; ///< 参数存放

std::map<ArgsType, std::map<std::string, ArgsDesc>, std::less<ArgsType>> m_arg_map;
std::map<std::string, std::function<bool(void)>> m_key_func;
std::array<std::map<std::string, std::function<bool(void)>>, 2> m_key_func;
std::vector<std::string> m_pos_key_priority_vec;
std::unordered_map<std::string, std::vector<std::string>> m_key_val;

private:
Expand Down Expand Up @@ -917,4 +928,4 @@ namespace Joger
}
};
};
#endif
#endif

0 comments on commit b203ece

Please sign in to comment.