Giter VIP home page Giter VIP logo

opzioni's People

Contributors

dependabot[bot] avatar ggabriel96 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

opzioni's Issues

Rename `Arg::csv_of`

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.

The return of templated Arg?

The idea

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;

The problem

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).

The solution

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 ๐ŸŽ‰

Parsing allows multiple dashes

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:

  • raise error if more than 2 dashes
  • cover "all dashes" case in is_two_dashes (when all coming arguments are considered positional)

Allow users to access Arg attributes in help strings

Using something like: https://godbolt.org/z/85d7oM

Apparently it is not an error to provide fmt::args 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).

Fix opzioni::convert

  1. try to specify an argument which has a std::vector<std::string> as default value
  2. get an error because it is not a built-in type
  3. add std::vector<std::string> as built-in type
  4. no suitable converter will be found
  5. try to add a converter
  6. find out there is one for std::vector<int> already and the code for std::vector<std::string> will be identical except for value_type
  7. try to write a generic templatized convert
  8. fail

Refactor `split_arg`

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.

General checks and error reporting

  • error when trying to add an argument with duplicated name or abbreviation
  • error when trying to add an argument with gather and assign action (this is impossible!?)
  • error when trying to add a required argument with a default value
  • error when default_value and set_value are not of same type
  • check that name of Program and Arg is non-empty and only characters, - or _ (gotta make up the specific rule) - #55

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.