Skip to content

Latest commit

History

100 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Table of contents


About

Clipper is a simple, header-only library that handles commad line arguments and parsing.
(This library requires C++20 support)

It is a learning project, that I took on to improve my C++ 📈.

Quick Start

Get clipper

Firstly download the library and include clipper.hpp in your .cpp file. Now just use it!

Usage

To begin you need to create an instance of CLI::clipper class.

CLI:clipper

Then you can add flags and options.
set() gives the variable that an option is going to save the value to (for options you have to give the name of the parameter that it is setting).

bool vrbs;
std::filesystem::path inpt;
cli.add_flag("--verbose", "-v")
.set(vrbs)
.doc("Displays verbose information");
cli.add_option<std::filesystem::path>("--input", "-i")
.set("file", inpt)
.doc("Input file")
.req();

You can use the match() or allow() function to give viable values for an option. Also with the set() function you can set the default value of the option.

cli.add_option<std::string>("--encoding", "-e")
.set("charset", inpt, "utf8") // utf8 is the default
.match("utf8", "utf16", "cp1252", "latin1") // could be .allow("utf8", ...)
.doc("Sets the encoding");

The validate() or require() function allow you to put custom restrictions on option values. You simply have to set a predicate function that will check your restrictions (or use a predefined one).

uint32_t itrcount;
cli.add_option<uint32_t>("--iteration-count", "-c")
.set("number", itrcount)
.validate("[0; 100]", CLI::pred::ibetween<0u, 100u>)
.doc("Sets the iteration count");
std::string name;
cli.add_option<std::string>("--custom-name", "-n")
.set("string", name)
.require("max 10 characters", [](const std::string& s) -> bool { return s.length() <= 10; })
.doc("Sets custom output name")
.req();

To parse arguments just use parse().

cli.parse(argc, argv);

Furthermore it is possible to add some information about the program.

CLI::clipper cli;
cli
.name("foo")
.version("1.0.0")
.author("me")
.description("app that does things")
.license("GPLv3")
.web_link("https://github.com/pawelrapacz");

To display help or version info you can do this:

std::cout << cli.make_help();
std::cout << cli.make_version_info();

The wrong() function returns a const std::vector<std::string>& that contains parsing errors like:

  • Unkonown argument
  • Missing required argument
  • Missing option value
  • Value is not allowed
std::cout << cli.wrong().front();

Example

#include"clipper.hpp"
#include<iostream>intmain(CLI::arg_count argc, CLI::args argv) {
CLI::clipper cli("app", "1.0.0", "", "LGPLv3");
bool show_help, show_version, flag;
std::filesystem::path input_file;
int count;
double myvalue;
std::size_t length;
cli.help_flag("--help", "-h")
.set(show_help);
cli.version_flag("--version", "-v")
.set(show_version)
.doc("Custom version documentation");
cli.add_flag("--flag", "-f")
.set(flag)
.doc("Sets Flag");
cli.add_option<std::filesystem::path>("--input", "-i")
.set("file", input_file)
.doc("Input file")
.req();
cli.add_option<int>("--count", "-c")
.doc("Sets count")
.set("number", count, 13)
.match(1, 2, 3, 13, 14);
cli.add_option<double>("--myvalue")
.doc("My value")
.set("value", myvalue, .5)
.validate("(0; 1)", CLI::between<0., 1.>); // could be: CLI::pred::between<0., 1.>// if you want to be specific
cli.add_option<std::size_t>("--length", "-l")
.doc("Output length")
.set("number", length)
.req()
.require(">10", CLI::greater_than<10ull>);
if (not cli.parse(argc, argv)) {
for (auto& i: cli.wrong())
std::cout << i << "\n";
return1;
}
if (show_help) {
std::cout << cli.make_help();
}
if (show_version) {
std::cout << cli.make_version_info();
}
return0;
}

Output:

> app --help
SYNOPSIS
app -i <file> -l <number> [...]
FLAGS
-h, --help displays help
-v, --version Custom version documentation
-f, --flag Sets Flag
OPTIONS
-i, --input <file> Input file
-c, --count (1 2 3 13 14)
Sets count
--myvalue <value> My value (0; 1)
-l, --length <number> Output length >10
LICENSE
LGPLv3
> app --version
app 1.0.0
> app -c
[-c] Missing option value
Missing required argument(s) 2
> app --input file.txt -c 17 -f -j --myvalue 0
[-c] Value 17 is not allowed
{ -c, --count (1 2 3 13 14) Sets count }
[-j] Unkonown argument
[--myvalue] Value 0 is not allowed
{ --myvalue <value> My value (0; 1) }
Missing required argument(s) 1
>

Details

For the most detailed documentation see the documentation generated by doxygen.

⚠ important

  • Internally, this utility stores option and flag names as std::string_view, so you must ensure that the referenced name values remain valid throughout its lifetime. The most convenient way to do this is to use C-style string literals.
  • If an option is repeated, the value is overwritten.

clipper class

This class is practically the only interface of the clipper library, that is meant to be directly used. It holds the neccessary and optional information about the application, options and flags.

MemberDescriptionReturn value
clipper()constructor
clipper(app_name)constructor
clipper(app_name, version, author, license_notice)constructor
~clipper()destructor
name(name)sets the nameclipper&
name()gets the namestd::string_view
description(description)sets the descriptionclipper&
description()gets the descriptionstd::string_view
version(version)sets the versionclipper&
version()gets the versionstd::string_view
author(author)sets the authorclipper&
author()gets the authorstd::string_view
license(license_notice)sets the license noticeclipper&
license()gets the license noticestd::string_view
web_link(web_link)sets the web linkclipper&
web_link()gets the web linkstd::string_view
add_option<type>(name)adds a option of a given typeoption&
add_option<type>(name, alt_name)adds a option of a given type with an alternative nameoption&
add_flag(name)adds a flagoption<bool>&
add_flag(name, alt_name)adds a flag with an alternative nameoption<bool>&
help_flag(name, alt_name = "")sets the help flag name/namesoption<bool>&
version_flag(name, alt_name = "")sets the help flag name/nameoption<bool>&
make_help()returns help pagestd::string
make_version_info()returns version informationstd::string
allow_no_args()allows the app to be used without any argumentsvoid
no_args()checks if no arguments were givenbool
parse(argc, argv)parses command line argumentsbool (true if successful, false otherwise)
wrong()gets a list of parsing errorsconst std::vector<std::string>&

flag class

To be precise it is option class.

MemberDescriptionReturn value
option(nm)constructs and sets the name reference
option(nm, anm)constructs and sets the name and alt_name reference
~option()destructor
set(ref)sets the variable to write tooption<bool>&
req()sets the flag to be requiredoption<bool>&
doc(doc)sets the flag descriptionoption<bool>&
doc()gets the flag descriptionconst std::string&

option class

It is a template class that allows integral types, floating point types, std::string and std::filesystem::path (CLI::option_types concept).

MemberDescriptionReturn value
option(nm)constructs and sets the name reference
option(nm, anm)constructs and sets the name and alt_name reference
~option()destructor
set(value_name, ref)sets the variable to write to and the value name (e.g. file, charset)option&
set(value_name, ref, def)sets the variable to write to with default value and the value name (e.g. file, charset)option&
req()sets the option to be requiredoption&
match(...) or allow()sets allowed valuesoption&
validate(doc, pred) or require()sets a function that validates the valueoption&
doc(doc)sets the option descriptionoption&
doc()gets the option descriptionconst std::string&

predicates

Predicate is a function that allows you to set custom restrictions on option values. These are set with the validate() or require() methods.

CLI::pred namespace contains a set of predefined template predicates to validate numeric options. This namespace is marked as inline so all of predicate functions can be accessed directly trough the CLI namespace. Type of a predicate is strictly set for each option variant: bool (*)(const Tp&) where Tp is the type of the option (template argument). That`s why when using these predefined predications you have to be precise with the types of given values:

CLI::between<1, 2>; // intCLI::ibetween<1ul, 2ul>; // unsigned longCLI::less_than<100.0l>; // long doubleCLI::igreater_than<5.0f>; // float
...
PredicateDescription
between<V1 , V2>checks whether a value is between bounds (excludes the bounds
ibetween<V1 , V2>checks whether a value is between bounds (includes the bounds)
greater_than<V>checks whether a value is greater than a number (excludes the number)
igreater_than<V>checks whether a value is greater than a number (includes the number)
less_than<V>checks whether a value is less than a number (excludes the number))
iless_than<V>checks whether a value is less than a number (includes the number)

Upcoming changes

  • Positional values
  • Option repetition policy
  • Subcommands

License

This project is licensed under the MIT License.

About

Header-only library that allows simple commad line arguments parsing

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Contributors

Languages