ggabriel96 / opzioni Goto Github PK
View Code? Open in Web Editor NEWThe wanna-be-simplest command line arguments library for C++
Home Page: https://ggabriel96.github.io/opzioni/
License: Boost Software License 1.0
The wanna-be-simplest command line arguments library for C++
Home Page: https://ggabriel96.github.io/opzioni/
License: Boost Software License 1.0
Arg::csv_of
should have std::string_view
as default type, but then it makes a little less sense to write csv_of
without a template argument. So it should be only Arg::csv
.
We should be able to detect that a flag got a value from the CLI during parsing, not assignment.
After #1 is merged, we are able to specify the desired type after all parsing and assignment is done, when requesting the argument value.
Additionally, in order for conversion to work for some type T
, we only need the opzioni::convert<T>
function to exist.
We can then add back a type parameter to Arg
, which would default to std::string
, and use std::variant
to allow our users to tell us the type, default value, even the converter (no more inserting into library namespace), etc, in the definition of the argument.
To "support unsupported types" like we did with std::any
, e.g. arbitrary user-defined types, we can do everything as a std::string
argument and, at the end, the user would use something like args["foo"].as<MyType>()
. Users will only have to define the conversion for MyType
(as usual) or they can get its value as a string.
There is a proof-of-concept in variant.cpp
.
We can also use the following snippet to maintain an explicit list of types with built-in support for parsing and conversion:
template <typename ...>
struct type_list;
template <typename ...>
struct variant_of;
template <typename ...Ts>
struct variant_of<type_list<Ts...>> {
using type = std::variant<Ts...>;
};
using builtin_types = type_list<int, float, double>;
using variant = variant_of<builtin_types>::type;
In order to get the value out of the variant without explicitly asking for the type, we need:
constexpr std::variant<...> v = ...;
constexpr auto val = std::get<v.index()>(v);
Note that the variables are constexpr
. But the actual final values of the map will be set at runtime because they will be provided by the user and constexpr
implies const
(so we cannot update it at runtime).
After much thought, I decided to accept the reality that it is not possible to 100% avoid asking the user for the type of the argument in the argument definition and when getting its value out after parsing. So I decided that I will minimize the times the user needs to tell the library the type of an argument in its definition. Instead, it will most likely be necessary when getting its value out.
This has been successfully implemented by #12 ๐
Improve error messages. Example: too many arguments when gather is involved.
We currently allow users to pass an option --opt
as ----opt
or with as many dashes as they like (as long as it is more than 2).
Proposal:
is_two_dashes
(when all coming arguments are considered positional)Using something like: https://godbolt.org/z/85d7oM
Apparently it is not an error to provide fmt::arg
s that do not exist in the format string.
If the user can access these attributes, we should not automatically add them to the arguments description because then they'll duplicate or won't be in the format the user desires. But I wanted to give the option to auto-add them because I like them present and find it tedious to add everything (and be consistent).
std::vector<std::string>
as default valuestd::vector<std::string>
as built-in typestd::vector<int>
already and the code for std::vector<std::string>
will be identical except for value_type
convert
The function split_arg
is a mess and has a not-so-meaningful name. Its name should be clearer about what it does and the code should be simpler.
I think implicit value is a better name than "set value". Inspired by https://github.com/jarro2783/cxxopts
default_value
and set_value
are not of same typename
of Program and Arg is non-empty and only characters, -
or _
(gotta make up the specific rule) - #55Inspiration: https://docs.python.org/3/library/argparse.html#action
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.