PixelLightAPI  .
CommandLine.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: CommandLine.h                                  *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_COMMANDLINE_H__
00024 #define __PLCORE_COMMANDLINE_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/String/String.h"
00032 #include "PLCore/Container/List.h"
00033 #include "PLCore/Container/Array.h"
00034 #include "PLCore/Container/HashMap.h"
00035 
00036 
00037 //[-------------------------------------------------------]
00038 //[ Namespace                                             ]
00039 //[-------------------------------------------------------]
00040 namespace PLCore {
00041 
00042 
00043 //[-------------------------------------------------------]
00044 //[ Forward declarations                                  ]
00045 //[-------------------------------------------------------]
00046 class CommandLineOption;
00047 
00048 
00049 //[-------------------------------------------------------]
00050 //[ Classes                                               ]
00051 //[-------------------------------------------------------]
00052 /**
00053 *  @brief
00054 *    Command line parser
00055 *
00056 *  @remarks
00057 *    This class is used to define the command line arguments your application understands and parse a given
00058 *    command line string passed to the application. Typical problems like quotes and filenames, or errors like
00059 *    missing arguments or unknown options are automatically taken care of. This makes it quite easy to provide
00060 *    nice command line arguments for your application without having to go through parsing hell yourself :-)
00061 */
00062 class CommandLine {
00063 
00064 
00065     //[-------------------------------------------------------]
00066     //[ Public static functions                               ]
00067     //[-------------------------------------------------------]
00068     public:
00069         /**
00070         *  @brief
00071         *    Convert a command array to a string
00072         *
00073         *  @param[in] lstArguments
00074         *    List of arguments
00075         *
00076         *  @return
00077         *    Command string
00078         */
00079         static PLCORE_API String ArgumentsToString(const Array<String> &lstArguments);
00080 
00081         /**
00082         *  @brief
00083         *    Convert a command string to an array
00084         *
00085         *  @param[in] sCmdLine
00086         *    Command string
00087         *
00088         *  @return
00089         *    Command array of arguments
00090         */
00091         static PLCORE_API Array<String> StringToArguments(const String &sCmdLine);
00092 
00093 
00094     //[-------------------------------------------------------]
00095     //[ Public functions                                      ]
00096     //[-------------------------------------------------------]
00097     public:
00098         /**
00099         *  @brief
00100         *    Constructor
00101         */
00102         inline CommandLine();
00103 
00104         /**
00105         *  @brief
00106         *    Destructor
00107         */
00108         inline ~CommandLine();
00109 
00110         /**
00111         *  @brief
00112         *    Get number of registered options
00113         *
00114         *  @return
00115         *    Number of options that have been registered
00116         */
00117         inline uint32 GetNumOfOptions() const;
00118 
00119         /**
00120         *  @brief
00121         *    Get option by index
00122         *
00123         *  @param[in] nIndex
00124         *    Index of the option to retrieve
00125         *
00126         *  @return
00127         *    Pointer to option, or a null pointer
00128         */
00129         inline CommandLineOption *GetOption(uint32 nIndex) const;
00130 
00131         /**
00132         *  @brief
00133         *    Get option by name
00134         *
00135         *  @param[in] sName
00136         *    Name of the option to retrieve (short or long name)
00137         *
00138         *  @return
00139         *    Pointer to option, or a null pointer
00140         */
00141         inline CommandLineOption *GetOption(const String &sName) const;
00142 
00143         /**
00144         *  @brief
00145         *    Delete all options
00146         */
00147         PLCORE_API void Clear();
00148 
00149         /**
00150         *  @brief
00151         *    Add parameter
00152         *
00153         *  @param[in] sName
00154         *    Parameter name (logical name, must *not* start with "-" or "--")
00155         *  @param[in] sShort
00156         *    Short name (must start with "-", e.g. "-a") or ""
00157         *  @param[in] sLong
00158         *    Long name (must start with "--", e.g. "-optiona") or ""
00159         *  @param[in] sDescription
00160         *    Description text for this option
00161         *  @param[in] sDefault
00162         *    Default value
00163         *  @param[in] bRequired
00164         *    Is the option required?
00165         *
00166         *  @return
00167         *    'true' if option could be added, 'false' on error
00168         *
00169         *  @remarks
00170         *    A parameter is an option that can receive a value.
00171         *    Example: command --name <name>
00172         */
00173         PLCORE_API bool AddParameter(const String &sName, const String &sShort, const String &sLong, const String &sDescription, const String &sDefault, bool bRequired = false);
00174 
00175         /**
00176         *  @brief
00177         *    Add flag (on/off)
00178         *
00179         *  @param[in] sName
00180         *    Parameter name (logical name, must *not* start with "-" or "--")
00181         *  @param[in] sShort
00182         *    Short name (must start with "-", e.g. "-a") or ""
00183         *  @param[in] sLong
00184         *    Long name (must start with "--", e.g. "-optiona") or ""
00185         *  @param[in] sDescription
00186         *    Description text for this option
00187         *  @param[in] bRequired
00188         *    Is the option required?
00189         *
00190         *  @return
00191         *    'true' if option could be added, 'false' on error
00192         *
00193         *  @remarks
00194         *    A flag is an option that is either on or off (off as default).
00195         *    Example: command --option
00196         */
00197         PLCORE_API bool AddFlag(const String &sName, const String &sShort, const String &sLong, const String &sDescription, bool bRequired = false);
00198 
00199         /**
00200         *  @brief
00201         *    Add argument
00202         *
00203         *  @param[in] sName
00204         *    Parameter name (logical name, must *not* start with "-" or "--")
00205         *  @param[in] sDescription
00206         *    Description text for this option
00207         *  @param[in] sDefault
00208         *    Default value
00209         *  @param[in] bRequired
00210         *    Is the option required?
00211         *
00212         *  @return
00213         *    'true' if option could be added, 'false' on error
00214         *
00215         *  @remarks
00216         *    An argument is an option that can receive a value, but is not preceded by the option name (like a parameter).
00217         *    Example: command <name>
00218         */
00219         PLCORE_API bool AddArgument(const String &sName, const String &sDescription, const String &sDefault, bool bRequired = false);
00220 
00221         /**
00222         *  @brief
00223         *    Parse command line arguments
00224         *
00225         *  @param[in] lstArgs
00226         *    List of arguments
00227         *
00228         *  @return
00229         *    'true' if the command line could be parsed without error, else 'false'
00230         */
00231         PLCORE_API bool ParseCommandLine(const Array<String> &lstArgs);
00232 
00233         /**
00234         *  @brief
00235         *    Check if there were any errors parsing the command line arguments
00236         *
00237         *  @return
00238         *    'true' if there were errors, else 'false'
00239         */
00240         inline bool HasErrors() const;
00241 
00242         /**
00243         *  @brief
00244         *    Check if an option value is set ('true' for boolean options or any other than "" for string values)
00245         *
00246         *  @param[in] sName
00247         *    Name of option (short or long name)
00248         *
00249         *  @return
00250         *    'true' if the value is set, else 'false'
00251         */
00252         PLCORE_API bool IsValueSet(const String &sName) const;
00253 
00254         /**
00255         *  @brief
00256         *    Get option value
00257         *
00258         *  @param[in] sName
00259         *    Name of option (short or long name)
00260         *
00261         *  @return
00262         *    Value of option ("true"/"false" for boolean values)
00263         */
00264         PLCORE_API String GetValue(const String &sName) const;
00265 
00266         /**
00267         *  @brief
00268         *    Get number of additional arguments that have been defined
00269         *
00270         *  @return
00271         *    Number of additional arguments
00272         *
00273         *  @remarks
00274         *    Additional arguments are values that have been provided on the command line but
00275         *    do not belong to a specific option (for arbitrary number of arguments)
00276         */
00277         inline uint32 GetNumOfAdditionalArguments() const;
00278 
00279         /**
00280         *  @brief
00281         *    Get additional argument
00282         *
00283         *  @param[in] nIndex
00284         *    Index of argument to get
00285         *
00286         *  @return
00287         *    Additional argument, or ""
00288         */
00289         inline String GetAdditionalArgument(uint32 nIndex) const;
00290 
00291         /**
00292         *  @brief
00293         *    Display a help text with all available options on the console
00294         *
00295         *  @param[in] sProgramName
00296         *    Name of the program
00297         */
00298         PLCORE_API void PrintHelp(const String &sProgramName) const;
00299 
00300 
00301     //[-------------------------------------------------------]
00302     //[ Private data                                          ]
00303     //[-------------------------------------------------------]
00304     private:
00305         Array<CommandLineOption*>           m_lstOptions;       /**< List of command line options */
00306         HashMap<String, CommandLineOption*> m_mapOptions;       /**< Map name -> option */
00307         List<String>                        m_lstParameters;    /**< Additional parameters */
00308         bool                                m_bError;           /**< Error indicator */
00309 
00310 
00311 };
00312 
00313 
00314 //[-------------------------------------------------------]
00315 //[ Namespace                                             ]
00316 //[-------------------------------------------------------]
00317 } // PLCore
00318 
00319 
00320 //[-------------------------------------------------------]
00321 //[ Implementation                                        ]
00322 //[-------------------------------------------------------]
00323 #include "PLCore/Tools/CommandLine.inl"
00324 
00325 
00326 #endif // __PLCORE_COMMANDLINE_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:52
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported