Documentation for OpenFLUID 2.2.0
StructuredCommandLineParser.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file StructuredCommandLineParser.hpp
34 
35  @author Dorian GERARDIN <dorian.gerardin@inrae.fr>
36 */
37 
38 
39 #ifndef __OPENFLUID_UTILS_STRUCTUREDCOMMANDLINEPARSER_HPP__
40 #define __OPENFLUID_UTILS_STRUCTUREDCOMMANDLINEPARSER_HPP__
41 
42 
43 #include <list>
44 #include <map>
45 #include <ostream>
46 #include <string>
47 #include <vector>
48 
50 
51 
52 namespace openfluid { namespace utils {
53 
55 {
56  private:
57 
58  std::string m_Name;
59 
60  std::vector<CommandLineCommand> m_Commands;
61 
62 
63  public :
64 
65  /**
66  Instanciates a command section with the given parameters
67  @param[in] Name The long name of the command section
68  */
69  CommandLineSection(const std::string& Name):
70  m_Name(Name)
71  { }
72 
73 
74  // =====================================================================
75  // =====================================================================
76 
77 
78  /**
79  Returns the name of the command
80  @return The name (e.g. "search")
81  */
82  std::string getName() const
83  {
84  return m_Name;
85  }
86 
87 
88  // =====================================================================
89  // =====================================================================
90 
91 
92  /**
93  Returns the commands
94  @return The list of commands
95  */
96  std::vector<CommandLineCommand> getCommands() const
97  {
98  return m_Commands;
99  }
100 
101 
102  // =====================================================================
103  // =====================================================================
104 
105 
106  /**
107  Adds a command to the section
108  @param[in] Command The command to add
109  */
110  void addCommand(const CommandLineCommand& Command)
111  {
112  m_Commands.push_back(Command);
113  }
114 };
115 
116 
117 // =====================================================================
118 // =====================================================================
119 
120 
122 {
123  private:
124 
125  std::vector<CommandLineSection*> m_CommandSections;
126 
127  std::vector<CommandLineCommand> m_NoSectionCommands;
128 
129 
130  public :
131 
132  /**
133  Instanciates a command line parser with the given parameters
134  @param[in] ProgramName The name of the programn
135  @param[in] HelpText The help text associated to the option
136  @param[in] CommandSections List of commands section
137  */
138  StructuredCommandLineParser(const std::string& ProgramName, const std::string& HelpText,
139  const std::vector<CommandLineSection*>& CommandSections) :
140  CommandLineParser(ProgramName, HelpText, true)
141  {
142  addCommandSections(CommandSections);
143  }
144 
145 
146  // =====================================================================
147  // =====================================================================
148 
149 
150  /**
151  Adds a command section to the parser
152  @param[in] CommandSection The command section to add
153  */
154  void addCommandSection(CommandLineSection* CommandSection)
155  {
156  m_CommandSections.push_back(CommandSection);
157  }
158 
159 
160  // =====================================================================
161  // =====================================================================
162 
163 
164  /**
165  Adds a command section to the parser
166  @param[in] CommandSections The command section to add
167  */
168  void addCommandSections(const std::vector<CommandLineSection*>& CommandSections)
169  {
170  for (const auto& CommandSection : CommandSections)
171  {
172  addCommandSection(CommandSection);
173  }
174  }
175 
176 
177  // =====================================================================
178  // =====================================================================
179 
180 
181  /**
182  Adds a command. Can add a command to a command section
183  @param[in] Command The command to add
184  @param[in] CommandSection The section to add the command. Can be null
185  */
186  void addCommand(const CommandLineCommand& Command, CommandLineSection* CommandSection = nullptr)
187  {
188  if(CommandSection)
189  {
190  CommandSection->addCommand(Command);
191  }
192  else
193  {
194  m_NoSectionCommands.push_back(Command);
195  }
196 
198  }
199 
200 
201  // =====================================================================
202  // =====================================================================
203 
204 
205  /**
206  Prints the help text
207  @param[in] OutStm The stream where the help text is printed (e.g. std::cout)
208  */
209  void printHelp(std::ostream& OutStm)
210  {
211  displayUsageMessage(OutStm);
212 
213  int LargestCommandTextLength = getLargestCommandLength();
214  int LargestOptionTextLength = getLargestOptionLength();
215 
216  if (m_ActiveCommand.empty())
217  {
218  OutStm << "\n" << m_AvailableCommandsText << "\n\n";
219 
220  size_t CmdSectionIndex = 0;
221  for (const auto& CmdSection : m_CommandSections)
222  {
223  OutStm << CmdSection->getName() << ": \n";
224  for (auto& Cmd : CmdSection->getCommands())
225  {
226  displayFormattedData(OutStm, Cmd.getName(), Cmd.getHelpText(), LargestCommandTextLength);
227  }
228  if(CmdSectionIndex < m_CommandSections.size() - 1)
229  {
230  OutStm << "\n";
231  }
232  CmdSectionIndex++;
233  }
234 
235  if(m_NoSectionCommands.size() > 0)
236  {
237  OutStm << "\nOthers: \n";
238  for (const auto& Cmd : m_NoSectionCommands)
239  {
240  displayFormattedData(OutStm, Cmd.getName(), Cmd.getHelpText(), LargestCommandTextLength);
241  }
242  }
243  }
244 
245  if (!m_Commands[m_ActiveCommand].getLongHelpText().empty())
246  {
247  OutStm << "\n" << m_Commands[m_ActiveCommand].getLongHelpText() << "\n";
248  }
249 
250  displayOptions(OutStm, getHelpOption(), LargestOptionTextLength);
251  }
252 };
253 
254 } }
255 
256 
257 #endif /* __OPENFLUID_UTILS_STRUCTUREDCOMMANDLINEPARSER_HPP__ */
Definition: CommandLineParser.hpp:302
Class for management of command line arguments.
Definition: CommandLineParser.hpp:651
const std::string m_AvailableCommandsText
Definition: CommandLineParser.hpp:664
int getLargestCommandLength()
Definition: CommandLineParser.hpp:671
void displayUsageMessage(std::ostream &OutStm)
Definition: CommandLineParser.hpp:792
void displayFormattedData(std::ostream &OutStm, std::string Title, std::string HelpText, int LargestTextLength)
Definition: CommandLineParser.hpp:732
std::map< std::string, CommandLineCommand > m_Commands
Definition: CommandLineParser.hpp:658
CommandLineOption getHelpOption()
Definition: CommandLineParser.hpp:722
void addCommand(const CommandLineCommand &Command)
Definition: CommandLineParser.hpp:951
int getLargestOptionLength()
Definition: CommandLineParser.hpp:687
void displayOptions(std::ostream &OutStm, CommandLineOption HelpOption, int LargestTextLength)
Definition: CommandLineParser.hpp:742
std::string m_ActiveCommand
Definition: CommandLineParser.hpp:662
Definition: StructuredCommandLineParser.hpp:55
std::vector< CommandLineCommand > getCommands() const
Definition: StructuredCommandLineParser.hpp:96
CommandLineSection(const std::string &Name)
Definition: StructuredCommandLineParser.hpp:69
std::string getName() const
Definition: StructuredCommandLineParser.hpp:82
void addCommand(const CommandLineCommand &Command)
Definition: StructuredCommandLineParser.hpp:110
Definition: StructuredCommandLineParser.hpp:122
StructuredCommandLineParser(const std::string &ProgramName, const std::string &HelpText, const std::vector< CommandLineSection * > &CommandSections)
Definition: StructuredCommandLineParser.hpp:138
void printHelp(std::ostream &OutStm)
Definition: StructuredCommandLineParser.hpp:209
void addCommand(const CommandLineCommand &Command, CommandLineSection *CommandSection=nullptr)
Definition: StructuredCommandLineParser.hpp:186
void addCommandSection(CommandLineSection *CommandSection)
Definition: StructuredCommandLineParser.hpp:154
void addCommandSections(const std::vector< CommandLineSection * > &CommandSections)
Definition: StructuredCommandLineParser.hpp:168
Definition: ApplicationException.hpp:47