Hazelnupp

is a simple, easy to use command line parameter parser.
Hazelnupp does not support windows-, or bsd-style arguments. Only linux-style.

What is the linux-style? This:

# Using a long parameter
a.out --long-parameter 1234
# Using an abbreviated parameter
a.out -lp 1234

# 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:

Hazelnupp args;
args.SetCrashOnFail(false);

# Importing into a project

> How do i actually import this into my existing project?

I am working on a proper way to make this a fast-and-easy include.
I am probably going to make it a single-header–single-cpp file solution. A namespace will obviously also be used.

If you want to use it NOW, the best idea would probably be to either compile a lib from source or set the entire Visual Studio project as a dependency, if you are using VS.

# 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
a.out --foo
# Int
a.out --foo 5
# Float
a.out --foo 5.5
# String
a.out --foo peter
# List (any type above works)
a.out --foo peter jake jeff billy
# List, mixed types
a.out --foo 1 2 3 4 peter willy billy bob 3

These parameters can then be accessed via a simple lookup!

# Minimal working example

So what's the simplest way to use Hazelnupp to work with command-line parameters? See:

#include "Hazelnupp.h"
using namespace Hazelnp;
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?

#include "Hazelnupp.h"
using namespace Hazelnp;
int main(int argc, char** argv)
{
Hazelnupp args(argc, argv);
// Either check via HasParam(), or do a try-catch
try
{
int myInt = args["--my-int"].GetInt32();
double myFlt = args["--my-float"].GetFloat32();
std::string myStr = args["--my-string"].GetString();
}
catch (HazelnuppInvalidKeyException&)
{
return -1;
}
return 0;
}

What about lists?

#include "Hazelnupp.h"
using namespace Hazelnp;
int main(int argc, char** argv)
{
Hazelnupp args(argc, argv);
const auto& myList = args["--my-list"].GetList(); // std::vector<Value*>
for (const auto* it : myList)
{
// Should probably check for type-correctness with it->GetDataType()
std::cout << it->GetString() << std::endl;
}
return 0;
}

# 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:

#include "Hazelnupp.h"
using namespace Hazelnp;
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;
}

# 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?

For exactly this reason, there are constraints. With this, you can control what the data looks like! Constraints serve two main purposes:

## 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.
- If a parameter is not present, and has no default value, an exception will be thrown.

Minimal working example:

#include "Hazelnupp.h"
using namespace Hazelnp;
int main(int argc, char** argv)
{
Hazelnupp args;
// Register constraints
args.RegisterConstraints({
ParamConstraint::Require("--this-is-required"), // This missing throws an exception
ParamConstraint::Require("--also-required-but-defaulted", {"122"}), // This will default to 122
});
// Parse
args.Parse(argc, argv);
return 0;
}

## Type safety

With type safety you can always be certain that you are working with the correct type!
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.
If it is not convertible, an exception will be thrown.

These conversions are:

- int -> [float, string, list, void]
- float ->[int, string, list, void]
- string -> [list, void]
- list -> [void]
- void -> [list]

The conversions `*->list` just create a list with a single entry (except for `void->list` which produces an empty list).
The `*->void` conversions just drop their value.

Minimal working example:

#include "Hazelnupp.h"
using namespace Hazelnp;
int main(int argc, char** argv)
{
Hazelnupp args;
// Register constraints
args.RegisterConstraints({
ParamConstraint::TypeSafety("--this-must-be-int", DATA_TYPE::INT)
});
// 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:

ParamConstraint pc;
pc.key = "--my-key";
pc.constrainType = true;
pc.wantedType = DATA_TYPE::STRING;
pc.defaultValue = {}; // no default value
pc.required = true;
args.RegisterConstraints({pc});

What doesn't work is inserting multiple constraints for one key. It will just discard the oldest one. But that's okay because one can describe all possible constraints for a single key in one struct.

# More examples?

Check out the unit tests! They may help you out!
Also make sure to check out the doxygen docs!

# Further notes

This is still in alpha! There is no guarantee at all that this actually works.
Whilst i did my best do make sure it does, i bet there are still a few flaws i did overlook.
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 do it, but i'm not sure when.
Feel free to submit a PR if you fixed something :)

# What is not supported?

Chaining abbreviated parameters, like this:

# This is not supported. It would think -ltr is one parameter.
a.out -ltr
# Instead do this
a.out -l -t -r

# 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.
Hazelnp
Definition: DataType.h:3
Hazelnp::HazelnuppInvalidKeyException
Gets thrown when an non-existent key gets dereferenced.
Definition: HazelnuppException.h:26
Hazelnp::Hazelnupp
The main class to interface with.
Definition: Hazelnupp.h:11
main
int main(int argc, char **argv)
Definition: main.cpp:7
Hazelnp::Hazelnupp::HasParam
bool HasParam(const std::string &key) const
Will check wether a parameter exists given a key, or not.
Definition: Hazelnupp.cpp:153
Hazelnp::Hazelnupp::Parse
void Parse(const int argc, const char *const *argv)
Will parse command line arguments.
Definition: Hazelnupp.cpp:35
Hazelnp::Hazelnupp::RegisterAbbreviation
void RegisterAbbreviation(const std::string &abbrev, const std::string &target)
Will register an abbreviation (like -f for –force)
Definition: Hazelnupp.cpp:332
Hazelnp::Hazelnupp::RegisterConstraints
void RegisterConstraints(const std::vector< ParamConstraint > &constraints)
Will register parameter constraints.
Definition: Hazelnupp.cpp:354
Hazelnupp.h
Hazelnp::ParamConstraint::Require
static ParamConstraint Require(const std::string &key, const std::vector< std::string > &defaultValue={}, bool required=true)
Constructs a require constraint.
Definition: ParamConstraint.h:16
Hazelnp::DATA_TYPE::INT
@ INT
Hazelnp::ParamConstraint::TypeSafety
static ParamConstraint TypeSafety(const std::string &key, DATA_TYPE wantedType, bool constrainType=true)
Constructs a type-safety constraint.
Definition: ParamConstraint.h:27