|
SimpleOpt
|
A cross-platform command line library which can parse almost any of the standard command line formats in use today. It is designed explicitly to be portable to any platform and has been tested on Windows and Linux. See CSimpleOptTempl for the class definition. More...
#include <stdlib.h>#include <string.h>Go to the source code of this file.
Classes | |
| class | CSimpleOptTempl< SOCHAR > |
| Implementation of the SimpleOpt class. More... | |
| struct | CSimpleOptTempl< SOCHAR >::SOption |
| Structure used to define all known options. More... | |
Defines | |
| #define | SO_END_OF_OPTIONS { -1, NULL, SO_NONE } |
| this option definition must be the last entry in the table | |
| #define | SO_ASSERT(b) |
| assertion used to test input data | |
| #define | CSimpleOpt CSimpleOptA |
| TCHAR version dependent on if _UNICODE is defined. | |
Typedefs | |
| typedef enum _ESOError | ESOError |
| Error values. | |
| typedef enum _ESOArgType | ESOArgType |
| typedef CSimpleOptTempl< char > | CSimpleOptA |
| ASCII/MBCS version of CSimpleOpt. | |
| typedef CSimpleOptTempl< wchar_t > | CSimpleOptW |
| wchar_t version of CSimpleOpt | |
Enumerations | |
| enum | _ESOError { SO_SUCCESS = 0, SO_OPT_INVALID = -1, SO_OPT_MULTIPLE = -2, SO_ARG_INVALID = -3, SO_ARG_INVALID_TYPE = -4, SO_ARG_MISSING = -5, SO_ARG_INVALID_DATA = -6 } |
Error values. More... | |
| enum | _ESOFlags { SO_O_EXACT = 0x0001, SO_O_NOSLASH = 0x0002, SO_O_SHORTARG = 0x0004, SO_O_CLUMP = 0x0008, SO_O_USEALL = 0x0010, SO_O_NOERR = 0x0020, SO_O_PEDANTIC = 0x0040, SO_O_ICASE_SHORT = 0x0100, SO_O_ICASE_LONG = 0x0200, SO_O_ICASE_WORD = 0x0400, SO_O_ICASE = 0x0700 } |
Option flags. More... | |
| enum | _ESOArgType { SO_NONE, SO_REQ_SEP, SO_REQ_CMB, SO_OPT, SO_MULTI } |
A cross-platform command line library which can parse almost any of the standard command line formats in use today. It is designed explicitly to be portable to any platform and has been tested on Windows and Linux. See CSimpleOptTempl for the class definition.
| - | switch character only (e.g. use stdin for input) |
| -o | short (single character) |
| -long | long (multiple character, single switch character) |
| --longer | long (multiple character, multiple switch characters) |
| --option | short/long option flag (no argument) |
| --option ARG | short/long option with separate required argument |
| --option=ARG | short/long option with combined required argument |
| --option[=ARG] | short/long option with combined optional argument |
| -oARG | short option with combined required argument |
| -o[ARG] | short option with combined optional argument |
| --multi ARG1 ARG2 | Multiple arguments |
| --multi N ARG-1 ARG-2 ... ARG-N | Variable number of arguments |
The SimpleOpt class is used by following these steps:
Include the SimpleOpt.h header file
#include "SimpleOpt.h"
Define an array of valid options for your program.
CSimpleOpt::SOption g_rgOptions[] = { { OPT_FLAG, _T("-a"), SO_NONE }, // "-a" { OPT_FLAG, _T("-b"), SO_NONE }, // "-b" { OPT_ARG, _T("-f"), SO_REQ_SEP }, // "-f ARG" { OPT_HELP, _T("-?"), SO_NONE }, // "-?" { OPT_HELP, _T("--help"), SO_NONE }, // "--help" SO_END_OF_OPTIONS // END };
Note that all options must start with a hyphen even if the slash will be accepted. This is because the slash character is automatically converted into a hyphen to test against the list of options. For example, the following line matches both "-?" and "/?" (on Windows).
{ OPT_HELP, _T("-?"), SO_NONE }, // "-?"
Instantiate a CSimpleOpt object supplying argc, argv and the option table
CSimpleOpt args(argc, argv, g_rgOptions);
Process the arguments by calling Next() until it returns false. On each call, first check for an error by calling LastError(), then either handle the error or process the argument.
while (args.Next()) {
if (args.LastError() == SO_SUCCESS) {
handle option: use OptionId(), OptionText() and OptionArg()
}
else {
handle error: see ESOError enums
}
}
Process all non-option arguments with File(), Files() and FileCount()
ShowFiles(args.FileCount(), args.Files());
The licence text below is the boilerplate "MIT Licence" used from: http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2006-2007, Brodie Thiesfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Definition in file SimpleOpt.h.
| typedef enum _ESOArgType ESOArgType |
Types of arguments that options may have. Note that some of the _ESOFlags are not compatible with all argument types. SO_O_SHORTARG requires that relevant options use either SO_REQ_CMB or SO_OPT. SO_O_CLUMP requires that relevant options use only SO_NONE.
| enum _ESOArgType |
Types of arguments that options may have. Note that some of the _ESOFlags are not compatible with all argument types. SO_O_SHORTARG requires that relevant options use either SO_REQ_CMB or SO_OPT. SO_O_CLUMP requires that relevant options use only SO_NONE.
Definition at line 293 of file SimpleOpt.h.
| enum _ESOError |
Error values.
Definition at line 211 of file SimpleOpt.h.
| enum _ESOFlags |
Option flags.
Definition at line 241 of file SimpleOpt.h.
1.7.3