40 #ifndef __OPENFLUID_UTILS_COMMANDLINEPARSER_HPP__
41 #define __OPENFLUID_UTILS_COMMANDLINEPARSER_HPP__
59 std::string m_LongName;
61 std::string m_ShortName;
67 std::string m_HelpText;
75 m_ValueRequired(false),m_Active(false)
91 const std::string& HelpText,
bool ValueRequired =
false):
92 m_LongName(LongName),m_ShortName(ShortName), m_ValueRequired(ValueRequired), m_HelpText(HelpText),
166 OptionDetail +=
"=<arg>";
174 OptionDetail +=
" <arg>";
205 return m_ValueRequired;
221 if ((!m_ValueRequired) || (m_ValueRequired && !m_Value.empty()))
267 m_Name(Name), m_Required(Required)
307 std::string m_HelpText;
309 std::string m_LongHelpText;
311 std::map<std::string,CommandLineOption> m_Options;
313 std::map<std::string,CommandLineOption*> m_ShortOptions;
315 std::vector<CommandLineOption> m_OptionsOrdered;
317 std::vector<CommandLineArg> m_Args;
336 CommandLineCommand(
const std::string& Name,
const std::string& HelpText,
const std::string& LongHelpText =
""):
337 m_Name(Name), m_HelpText(HelpText)
339 m_LongHelpText = LongHelpText.empty() ? HelpText : LongHelpText;
367 return m_LongHelpText;
393 const std::map<std::string,CommandLineOption>&
options()
409 return m_OptionsOrdered;
425 m_OptionsOrdered.push_back(Option);
442 void addOptions(
const std::vector<CommandLineOption>& Options)
444 for (
const auto& Opt : Options)
462 auto it = m_Options.find(LongName);
464 if (it == m_Options.end())
469 (*it).second.activate(Value);
470 return (*it).second.isActive();
485 auto it = m_ShortOptions.find(ShortName);
487 if (it == m_ShortOptions.end())
492 return (*it).second->getLongName();
507 return (m_Options.find(LongName) != m_Options.end());
522 auto it = m_Options.find(LongName);
524 if (it == m_Options.end())
529 return (*it).second.isValueRequired();
544 auto it = m_Options.find(LongName);
546 return (it != m_Options.end() && (*it).second.isActive());
561 auto it = m_Options.find(LongName);
563 if (it == m_Options.end())
568 return ((*it).second.getValue());
581 for (
auto& Opt : m_Options)
599 m_Args.push_back(Arg);
611 void addArgs(
const std::vector<CommandLineArg>& Args)
613 for (
const auto& Arg : Args)
628 const std::vector<CommandLineArg>&
args()
673 int LargestCommandLength = std::numeric_limits<int>::min();
676 LargestCommandLength = std::max(LargestCommandLength, (
int)Cmd.first.size());
679 return LargestCommandLength;
691 AllOptions.insert({HelpOption.
getShortName(), HelpOption});
693 int LargestOptionLength = std::numeric_limits<int>::min();
694 for (
auto& Opt : AllOptions)
696 LargestOptionLength = std::max(LargestOptionLength, (
int)Opt.second.getDetails().size());
699 return LargestOptionLength;
707 std::string
getCustomIndent(
int CommandLength,
int LargestCommandLength, std::string minimalSpace =
" ")
709 int Delta = LargestCommandLength - CommandLength;
712 minimalSpace.append(Delta,
' ');
732 void displayFormattedData(std::ostream& OutStm, std::string Title, std::string HelpText,
int LargestTextLength)
734 OutStm <<
" " << Title <<
getCustomIndent(Title.size(), LargestTextLength) << HelpText <<
"\n";
744 OutStm <<
"\nAvailable options:\n";
759 displayFormattedData(OutStm, Opt.second.getDetails(), Opt.second.getHelpText(), LargestTextLength);
771 std::string Args =
"";
776 Args +=
"<" + Arg.getName() +
"> ";
780 Args +=
"[<" + Arg.getName() +
">] ";
798 CmdName =
"[<command>]";
807 std::vector<std::string> m_ExtraArgs;
809 std::vector<std::string> m_ThirdPartyArgs;
811 std::string m_ParsingMessage;
815 bool m_UseCustomOrder;
820 m_HelpAsked(false), m_UseCustomOrder(false)
836 CommandLineParser(
const std::string& ProgramName,
const std::string& HelpText,
bool UseCustomOrder =
false) :
868 return m_ThirdPartyArgs;
910 return m_ParsingMessage;
981 bool parse(std::list<std::string> ArgValues)
985 while (!ArgValues.empty())
987 std::string Arg = ArgValues.front();
988 ArgValues.pop_front();
992 m_ThirdPartyArgs = { std::make_move_iterator(std::begin(ArgValues)),
993 std::make_move_iterator(std::end(ArgValues)) };
1006 m_ExtraArgs.push_back(Arg);
1021 if (Arg ==
"-h" || Arg ==
"--help")
1025 else if (Arg.size() < 2)
1027 m_ParsingMessage =
"wrong format for option \"" + Arg +
"\"";
1032 std::string LongOptName;
1033 std::string OptValue;
1034 bool IsFromShort =
false;
1038 if (Arg.size() != 2)
1040 m_ParsingMessage =
"wrong format for short option \"" + Arg +
"\"";
1051 std::string TmpName = Arg.substr(2,Arg.size()-2);
1053 std::string::size_type EqualPos = TmpName.find(
"=");
1055 if (EqualPos != std::string::npos)
1057 LongOptName = TmpName.substr(0,EqualPos);
1058 OptValue = TmpName.substr(EqualPos+1,TmpName.size()-EqualPos-1);
1062 LongOptName = TmpName;
1069 m_ParsingMessage =
"unknown option \"" + Arg +
"\"";
1079 if (IsFromShort && !ArgValues.empty())
1081 OptValue = ArgValues.front();
1082 ArgValues.pop_front();
1085 if (OptValue.empty())
1087 m_ParsingMessage =
"missing value for option \"" + Arg +
"\"";
1095 m_ParsingMessage =
"unknown error for option \"" + Arg +
"\"";
1117 std::list<std::string> ArgValues;
1119 for (
int i=1; i< ArgC; i++)
1121 ArgValues.push_back(std::string(ArgV[i]));
1124 return parse(ArgValues);
1138 m_ParsingMessage.clear();
1139 m_ExtraArgs.clear();
1140 m_ThirdPartyArgs.clear();
1182 if(m_UseCustomOrder)
1186 if (!Cmd.getName().empty())
1196 if (!Cmd.first.empty())
1225 if (!Cmd.first.empty())
1236 OutStm <<
" " << Cmd.first <<
" : " << Cmd.second.getHelpText() <<
"\n";
1238 for (
auto& Opt : Cmd.second.options())
1242 if (Opt.second.isActive())
1250 OutStm <<
" " << Opt.first;
1252 if (!Opt.second.getShortName().empty())
1254 OutStm <<
"," << Opt.second.getShortName();
1257 if (Opt.second.isValueRequired())
1259 OutStm <<
"[" << Opt.second.getValue() <<
"]";
1262 OutStm <<
" : " << Opt.second.getHelpText() <<
"\n";
1268 if (!m_ExtraArgs.empty())
1270 OutStm <<
"Extra arguments :";
1272 for (
auto& Arg : m_ExtraArgs)
1274 OutStm <<
" " << Arg;
Definition: CommandLineParser.hpp:253
std::string getName() const
Definition: CommandLineParser.hpp:279
CommandLineArg(const std::string &Name, bool Required=true)
Definition: CommandLineParser.hpp:266
bool isRequired() const
Definition: CommandLineParser.hpp:293
Definition: CommandLineParser.hpp:302
bool isOptionActive(const std::string &LongName) const
Definition: CommandLineParser.hpp:542
const std::vector< CommandLineArg > & args()
Definition: CommandLineParser.hpp:628
const std::map< std::string, CommandLineOption > & options()
Definition: CommandLineParser.hpp:393
void addArg(const CommandLineArg &Arg)
Definition: CommandLineParser.hpp:597
bool isOptionExists(const std::string &LongName) const
Definition: CommandLineParser.hpp:505
std::string getName() const
Definition: CommandLineParser.hpp:379
std::string getOptionValue(const std::string &LongName) const
Definition: CommandLineParser.hpp:559
void addOption(const CommandLineOption &Option)
Definition: CommandLineParser.hpp:421
CommandLineCommand(const std::string &Name, const std::string &HelpText, const std::string &LongHelpText="")
Definition: CommandLineParser.hpp:336
const std::vector< CommandLineOption > & optionsOrdered()
Definition: CommandLineParser.hpp:407
CommandLineCommand()
Definition: CommandLineParser.hpp:322
std::string getHelpText() const
Definition: CommandLineParser.hpp:351
void reset()
Definition: CommandLineParser.hpp:579
std::string getOptionNameFromShortName(const std::string &ShortName) const
Definition: CommandLineParser.hpp:483
void addArgs(const std::vector< CommandLineArg > &Args)
Definition: CommandLineParser.hpp:611
bool isOptionRequiresValue(const std::string &LongName) const
Definition: CommandLineParser.hpp:520
void addOptions(const std::vector< CommandLineOption > &Options)
Definition: CommandLineParser.hpp:442
bool activateOption(const std::string &LongName, const std::string &Value="")
Definition: CommandLineParser.hpp:460
std::string getLongHelpText() const
Definition: CommandLineParser.hpp:365
Definition: CommandLineParser.hpp:56
std::string getValue() const
Definition: CommandLineParser.hpp:147
CommandLineOption(const std::string &LongName, const std::string &ShortName, const std::string &HelpText, bool ValueRequired=false)
Definition: CommandLineParser.hpp:90
void reset()
Definition: CommandLineParser.hpp:239
std::string getDetails() const
Definition: CommandLineParser.hpp:161
std::string getLongName() const
Definition: CommandLineParser.hpp:119
void activate(const std::string &Value="")
Definition: CommandLineParser.hpp:217
std::string getHelpText() const
Definition: CommandLineParser.hpp:105
bool isValueRequired() const
Definition: CommandLineParser.hpp:203
bool isActive() const
Definition: CommandLineParser.hpp:189
std::string getShortName() const
Definition: CommandLineParser.hpp:133
CommandLineOption()
Definition: CommandLineParser.hpp:74
Class for management of command line arguments.
Definition: CommandLineParser.hpp:651
void reset()
Definition: CommandLineParser.hpp:1135
std::string m_ProgramName
Definition: CommandLineParser.hpp:654
bool parse(std::list< std::string > ArgValues)
Definition: CommandLineParser.hpp:981
const std::string m_AvailableCommandsText
Definition: CommandLineParser.hpp:664
CommandLineParser(const std::string &ProgramName, const std::string &HelpText, bool UseCustomOrder=false)
Definition: CommandLineParser.hpp:836
int getLargestCommandLength()
Definition: CommandLineParser.hpp:671
void displayUsageMessage(std::ostream &OutStm)
Definition: CommandLineParser.hpp:792
std::string m_HelpText
Definition: CommandLineParser.hpp:656
std::string getActiveCommand() const
Definition: CommandLineParser.hpp:922
std::vector< CommandLineCommand > m_CommandsOrdered
Definition: CommandLineParser.hpp:660
void displayFormattedData(std::ostream &OutStm, std::string Title, std::string HelpText, int LargestTextLength)
Definition: CommandLineParser.hpp:732
std::string getHelpText() const
Definition: CommandLineParser.hpp:880
bool parse(int ArgC, char **ArgV)
Definition: CommandLineParser.hpp:1115
bool isHelpAsked()
Definition: CommandLineParser.hpp:1157
std::map< std::string, CommandLineCommand > m_Commands
Definition: CommandLineParser.hpp:658
const std::vector< std::string > & thirdPartyArgs() const
Definition: CommandLineParser.hpp:866
CommandLineOption getHelpOption()
Definition: CommandLineParser.hpp:722
void printHelp(std::ostream &OutStm)
Definition: CommandLineParser.hpp:1171
std::string getCustomIndent(int CommandLength, int LargestCommandLength, std::string minimalSpace=" ")
Definition: CommandLineParser.hpp:707
std::string getArgsDisplay()
Definition: CommandLineParser.hpp:769
void addOption(const CommandLineOption &Option)
Definition: CommandLineParser.hpp:966
void addCommand(const CommandLineCommand &Command)
Definition: CommandLineParser.hpp:951
int getLargestOptionLength()
Definition: CommandLineParser.hpp:687
void printState(std::ostream &OutStm)
Definition: CommandLineParser.hpp:1221
void displayOptions(std::ostream &OutStm, CommandLineOption HelpOption, int LargestTextLength)
Definition: CommandLineParser.hpp:742
std::string getParsingMessage() const
Definition: CommandLineParser.hpp:908
std::string m_ActiveCommand
Definition: CommandLineParser.hpp:662
const std::vector< std::string > & extraArgs() const
Definition: CommandLineParser.hpp:851
CommandLineParser()
Definition: CommandLineParser.hpp:819
std::string getProgramName() const
Definition: CommandLineParser.hpp:894
const CommandLineCommand & command(const std::string &Name) const
Definition: CommandLineParser.hpp:937
Definition: ApplicationException.hpp:47