2021-06-03 12:31:16 +02:00
[](https://github.com/Leonetienne/Hazelnupp)
2021-06-03 00:37:02 +02:00
2021-06-03 12:25:35 +02:00
# [Documentation](https://leonetienne.github.io/Hazelnupp/)
## [Direct link to docs of the main class](https://leonetienne.github.io/Hazelnupp/classHazelnupp.html)
2021-06-03 00:37:02 +02:00
# Hazelnupp
is a simple, easy to use command line parameter parser.
2021-06-03 11:43:20 +02:00
Hazelnupp does not support windows-, or bsd-style arguments. Only linux-style.
2021-06-03 00:37:02 +02:00
2021-06-03 11:43:20 +02:00
What is the linux-style? This:
2021-06-03 00:37:02 +02:00
```
# Using a long parameter
2021-06-04 13:51:06 +02:00
$ a.out --long-parameter 1234
2021-06-03 00:37:02 +02:00
# Using an abbreviated parameter
2021-06-04 13:51:06 +02:00
$ a.out -lp 1234
2021-06-03 00:37:02 +02:00
```
## Note
These examples reference exceptions. These are not enabled by default. The default behaviour for user-fault exceptions is to produce output to `stderr` and kill the process.
To enable exceptions, call this method:
```cpp
Hazelnupp args;
args.SetCrashOnFail(false);
```
2021-06-04 14:13:32 +02:00
## Index
1. [Importing into a project ](#importing-into-a-project )
2021-06-04 15:58:51 +02:00
2. [What's the concept? ](#whats-the-concept )
3. [Minimal working example ](#minimal-working-example )
4. [Abbreviations ](#abbreviations )
5. [Constraints ](#constraints )
6. [Automatic parameter documentation ](#automatic-parameter-documentation )
7. [More examples? ](#more-examples )
8. [What is not supported? ](#what-is-not-supported )
9. [Further notes ](#further-notes )
10. [Contributing ](#contributing )
11. [LICENSE ](#license )
2021-06-04 14:13:32 +02:00
2021-06-04 15:52:43 +02:00
< span id = "importing-into-a-project" > < / span >
2021-06-03 00:56:18 +02:00
## Importing into a project
> How do i actually import this into my existing project?
2021-06-03 21:50:40 +02:00
Super easily! Just grab the latest files (2) from [/INCLUDE ](https://github.com/Leonetienne/Hazelnupp/tree/master/INCLUDE ) and put them into your project!
2021-06-03 21:44:29 +02:00
You may have to add the .cpp to your compile list, but most IDEs should do this automatically.
2021-06-03 00:56:18 +02:00
2021-06-04 15:52:43 +02:00
< span id = "whats-the-concept" > < / span >
2021-06-03 00:37:02 +02:00
## What's the concept?
The concept is that each parameter must be one of five types. These are:
* Void
* Int
* Float
* String
* List (non-recursive)
Here are examples on how to create them
```
# Void
2021-06-04 13:51:06 +02:00
$ a.out --foo
2021-06-03 00:37:02 +02:00
# Int
2021-06-04 13:51:06 +02:00
$ a.out --foo 5
2021-06-03 00:37:02 +02:00
# Float
2021-06-04 13:51:06 +02:00
$ a.out --foo 5.5
2021-06-03 00:37:02 +02:00
# String
2021-06-04 13:51:06 +02:00
$ a.out --foo peter
2021-06-03 00:37:02 +02:00
2021-06-05 12:39:59 +02:00
# List (any type above works, except void)
2021-06-04 13:51:06 +02:00
$ a.out --foo peter jake jeff billy
2021-06-03 00:37:02 +02:00
# List, mixed types
2021-06-04 13:51:06 +02:00
$ a.out --foo 1 2 3 4 peter willy billy bob 3
2021-06-03 00:37:02 +02:00
```
These parameters can then be accessed via a simple lookup!
2021-06-04 15:56:51 +02:00
< span id = "minimal-working-example" > < / span >
2021-06-03 00:37:02 +02:00
## Minimal working example
So what's the simplest way to use Hazelnupp to work with command-line parameters? See:
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args(argc, argv);
if (args.HasParam("--force"))
// do forced
else
// be gentle
return 0;
}
```
Looks super easy! But what about actual values?
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args(argc, argv);
// Either check via HasParam(), or do a try-catch
try
{
2021-06-03 00:38:55 +02:00
int myInt = args["--my-int"].GetInt32();
2021-06-03 16:39:43 +02:00
double myFlt = args["--my-float"].GetFloat32();
2021-06-03 00:37:02 +02:00
std::string myStr = args["--my-string"].GetString();
}
catch (HazelnuppInvalidKeyException& )
{
return -1;
}
return 0;
}
```
2021-06-03 11:43:20 +02:00
What about lists?
2021-06-03 00:37:02 +02:00
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args(argc, argv);
2021-06-03 11:43:20 +02:00
const auto& myList = args["--my-list"].GetList(); // std::vector< Value * >
2021-06-03 00:37:02 +02:00
for (const auto* it : myList)
{
// Should probably check for type-correctness with it->GetDataType()
std::cout < < it- > GetString() < < std::endl ;
}
return 0;
}
```
2021-06-04 15:56:51 +02:00
< span id = "abbreviations" > < / span >
2021-06-03 00:37:02 +02:00
## Abbreviations
Abbreviations are a very important part of command line arguments. Like, typing `-f` instead of `--force` .
Here's how to use them in Hazelnupp:
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args;
// Register abbreviations
args.RegisterAbbreviation("-f", "--force");
// Parse
args.Parse(argc, argv);
if (args.HasParam("--force")) // This key will be present, even if the user passed '-f'
// do forced
else
// be gentle
return 0;
}
```
2021-06-04 15:56:51 +02:00
< span id = "constraints" > < / span >
2021-06-03 00:37:02 +02:00
## Constraints
> That's all cool and stuff, but this looks like a **LOT** of error-checking and not elegant at all! How would i *actually* use this?
2021-06-03 11:43:20 +02:00
For exactly this reason, there are constraints. With this, you can control what the data looks like! Constraints serve two main purposes:
2021-06-03 00:37:02 +02:00
### Requiring data
With `ParamConstraint::Require()` you can declare that a paramater must either always be present, or provide a default value.
* If a parameter is not present, but has a default value, it will be automatically created.
2021-06-03 11:43:20 +02:00
* If a parameter is not present, and has no default value, an exception will be thrown.
2021-06-03 00:37:02 +02:00
Minimal working example:
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args;
// Register constraints
2021-06-05 12:19:41 +02:00
args.RegisterConstraint("--this-is-required", ParamConstraint::Require()); // This missing throws an exception
args.RegisterConstraint("--also-required-but-defaulted", ParamConstraint::Require({"122"})); // This will default to 122
2021-06-03 00:37:02 +02:00
// Parse
args.Parse(argc, argv);
return 0;
}
```
### Type safety
2021-06-03 11:43:20 +02:00
With type safety you can always be certain that you are working with the correct type!
2021-06-03 00:37:02 +02:00
By creating a type-constraint you force Hazelnupp to use a certain type.
If a supplied type does not match, but is convertible, it will be converted.
2021-06-03 11:43:20 +02:00
If it is not convertible, an exception will be thrown.
2021-06-03 00:37:02 +02:00
These conversions are:
2021-06-03 11:43:20 +02:00
* int -> [float, string, list, void]
* float ->[int, string, list, void]
* string -> [list, void]
* list -> [void]
2021-06-05 12:39:59 +02:00
* void -> [list, string]
2021-06-03 00:37:02 +02:00
2021-06-03 11:43:20 +02:00
The conversions `*->list` just create a list with a single entry (except for `void->list` which produces an empty list).
2021-06-05 12:39:59 +02:00
The `*->void` conversions just drop their value.
`void->string` just produces an empty string.
2021-06-03 00:37:02 +02:00
Minimal working example:
```cpp
#include "Hazelnupp.h"
2021-06-03 16:39:43 +02:00
using namespace Hazelnp;
2021-06-03 00:37:02 +02:00
int main(int argc, char** argv)
{
Hazelnupp args;
// Register constraints
2021-06-05 12:19:41 +02:00
args.RegisterConstraint("--this-must-be-int", ParamConstraint::TypeSafety(DATA_TYPE::INT));
2021-06-03 00:37:02 +02:00
// Parse
args.Parse(argc, argv);
return 0;
}
```
If `--this-must-be-int` would be passed as a float, it would be converted to int.
If it was passed, for example, as a string, it would throw an exception.
---
Note that you can also combine these two constraint-types by populating the struct yourself:
2021-06-03 00:38:55 +02:00
```cpp
2021-06-03 00:37:02 +02:00
ParamConstraint pc;
pc.constrainType = true;
pc.wantedType = DATA_TYPE::STRING;
pc.defaultValue = {}; // no default value
pc.required = true;
2021-06-05 12:47:26 +02:00
args.RegisterConstraint("--my-key", pc);
2021-06-03 00:37:02 +02:00
```
2021-06-05 12:47:26 +02:00
What doesn't work is inserting multiple constraints for one key. It will just discard the older one. But that's okay because one can describe all possible constraints for a single key in **one** struct.
2021-06-03 00:37:02 +02:00
2021-06-04 15:56:51 +02:00
< span id = "automatic-parameter-documentation" > < / span >
2021-06-04 02:30:15 +02:00
## Automatic parameter documentation
2021-06-04 02:43:38 +02:00
Hazelnupp does automatically create a parameter documentation, accessible via `--help` .
2021-06-04 02:30:15 +02:00
If you want to use `--help` yourself, just turn it off.
```cpp
Hazelnupp args;
args.SetCatchHelp(false);
```
What does this automatically generated documentation look like?
```
$ a.out --help
This is the testing application for Hazelnupp.
==== AVAILABLE PARAMETERS ====
--help This will display the parameter documentation.
2021-06-04 13:51:06 +02:00
--names LIST default=['peter' 'hannes'] The names to target
2021-06-04 02:30:15 +02:00
--force -f Just forces it.
--width -w FLOAT The width of something...
--fruit STRING [[REQUIRED]] The fruit to use
--height -h
```
This documentation is automatically fed by any information provided on parameters.
You have to set the brief descriptions yourself though.
```cpp
Hazelnupp args;
args.RegisterDescription("--force", "Just forces it.");
```
Additionally you can provide a brief description of your application to be added right above the parameter list.
```cpp
Hazelnupp args;
args.SetBriefDescription("This is the testing application for Hazelnupp.");
```
2021-06-04 02:43:38 +02:00
If you want to display this information somewhere else, you can always access it as a string via `args.GenerateDocumentation()` .
2021-06-04 02:30:15 +02:00
2021-06-04 15:56:51 +02:00
< span id = "more-examples" > < / span >
2021-06-03 00:37:02 +02:00
## More examples?
2021-06-04 13:51:06 +02:00
Check out the [tests ](https://github.com/Leonetienne/Hazelnupp/tree/master/Test_Hazelnupp )! They may help you out!
2021-06-03 12:25:35 +02:00
Also make sure to check out the [doxygen docs ](https://leonetienne.github.io/Hazelnupp/ )!
2021-06-03 00:37:02 +02:00
2021-06-04 15:56:51 +02:00
< span id = "what-is-not-supported" > < / span >
2021-06-03 00:37:02 +02:00
## What is not supported?
Chaining abbreviated parameters, like this:
```
# This is not supported. It would think -ltr is one parameter.
2021-06-04 13:51:06 +02:00
$ a.out -ltr
2021-06-03 00:37:02 +02:00
# Instead do this
2021-06-04 13:51:06 +02:00
$ a.out -l -t -r
```
2021-06-03 00:37:02 +02:00
2021-06-04 13:51:06 +02:00
Using parameters multiple times
2021-06-03 00:37:02 +02:00
```
2021-06-04 13:51:06 +02:00
# This is not supported.
# Let's say -i is short for --input
$ a.out -i hello.txt -i shoe.txt -i somsang.txt
# Instead do this
$ a.out -i hello.txt shoe.txt somsang.txt
```
2021-06-04 15:56:51 +02:00
< span id = "further-notes" > < / span >
2021-06-04 13:51:06 +02:00
## Further notes
This is still in alpha! There is no guarantee at all that this actually works.
2021-06-06 14:12:36 +02:00
Whilst i did my best to make sure it does, i bet there are still a few flaws i've overlooked.
2021-06-04 13:51:06 +02:00
Please know that i am not obliged to work on fixes. I do have other stuff to do.
This does not mean that i won't, but i'm not sure when.
Feel free to submit a PR if you think you improved it in any way :)
2021-06-04 15:56:51 +02:00
< span id = " #contributing " ></ span >
2021-06-04 13:51:06 +02:00
## Contributing
If you want to contribute, feel free to fork the repository, and submit a pull request.
2021-06-06 14:12:36 +02:00
Bugfixes and tests are almost certain to be accepted, features should be agreed upon and come with tests.
2021-06-04 14:13:32 +02:00
Just create an issue with the tag `feature request` . Don't forget to update the UML (`Hazelnupp.vpp` )! The (free) modelling software used is [Visual Paradigm ](https://www.visual-paradigm.com ).
2021-06-04 13:51:06 +02:00
Any code added must match the existing style!
* Objects begin with a lowercase initial
* Classifiers and Functions/Methods begin with an uppercase initial
* Classifiers are camel-case
* Classifiers get documented via `/** */` for doxygen. See existing classifiers
2021-06-06 14:12:36 +02:00
* Members (methods and objects) get documented via `//!` for doxygen. See existing definitions
2021-06-04 13:51:06 +02:00
* `{` always gets a new line
* Enumerations (and their values) and macros are all-upper case snake-case
* No `using namespace std`
* Do `using namespace Hazelnp` in cpp files. Don't do `Hazelnp::` if possible
* Files outside the project (like STL) have to be included with `#include <>` . Not `""`
2021-06-03 00:37:02 +02:00
2021-06-04 15:52:43 +02:00
< span id = "license" > < / span >
2021-06-03 00:37:02 +02:00
## LICENSE
```
Copyright (c) 2021, Leon Etienne
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
```