https://github.com/raftlib/cmdargs
https://github.com/raftlib/cmdargs
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/raftlib/cmdargs
- Owner: RaftLib
- Created: 2015-06-30T18:11:25.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-06-15T15:48:49.000Z (almost 5 years ago)
- Last Synced: 2025-04-14T17:46:15.005Z (about 1 year ago)
- Language: C++
- Size: 700 KB
- Stars: 6
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Command Args
=====================
Super simple command arguments parser for C++.
Yeah, there are quite a few already in existence, but I made this for a
particular purpose, ended up using it quite a
bit so it's worth it at this point to put it
up on GitHub so I can download it from multiple
points.
# Build Status
[](https://travis-ci.org/RaftLib/cmdargs)
# Notes
There are a few bugs here and there, notably
there are a few instances where there are
null arguments that I'd like to pass but
it's easy enough to get around by adding
an explicit boolean.
# Compiling is super simple
```bash
mkdir build
cd build
cmake ../cmdargs
make
sudo make install
```
which installs all the
library code and header files in the appropriate
/usr/local prefix. I've used on both OS X and Linux,
I'll check to see if it works on Windows when I get a chance.
## Simple Example
```cpp
CmdArgs cmd( argv[ 0 ],
std::cout,
std::cerr );
bool help( false );
cmd.addOption( new Option< bool >( help,
"-h",
"Print this message",
false ) );
bool printHeader( false );
cmd.addOption( new Option< bool >( printHeader,
"-header",
"Print the header and exit" ) );
cmd.addOption( new Option< std::string >( filename,
"-f",
"Set the output file name",
true /** make this arg mandatory **/ ) );
cmd.addOption( new Option< int64_t >( length,
"-l",
"Length of run" ) );
cmd.addOption( new Option< int64_t >( buff_size,
"-b",
"Buffer size" ) );
cmd.addOption( new Option< int64_t >( repeat,
"-r",
"Number of times to repeat simulation" ) );
/** give object command line **/
cmd.processArgs( argc, argv );
/** handle the user needing help **/
if( help )
{
cmd.printArgs();
exit( EXIT_SUCCESS );
}
/**
* to check and see if all mandatory args are set, add this code
* which will print all mandatory args that are missing.
*/
if(! cmd.allMandatorySet() )
{
//exit since well, mandatory args missing
exit( EXIT_FAILURE );
}
```
Enjoy, feel free to update, copy or fix as you feel the need to.