Coder Social home page Coder Social logo

sailormoon / flags Goto Github PK

View Code? Open in Web Editor NEW
226.0 17.0 18.0 40 KB

⛳ Simple, extensible, header-only C++17 argument parser released into the public domain.

License: The Unlicense

C++ 88.21% Python 2.37% CMake 9.42%
parse c-plus-plus-17 c-plus-plus public-domain publicdomain free free-software simple header-only argument-parser

flags's People

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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

flags's Issues

Argument validation + shortcuts

Now that there are some basic unit tests, flag validation and multiple arguments should be added.

Currently thinking of a syntax similar to:

const flags::args args(argc, argv);
if (const auto opt = args.get_multiple<bool>("flag", "f", "no-flag", "other")) {
  const auto [argument, value] = *opt;
  // do something -- *argument will be the first of "flag", "f", or "no-flag" that matched
  // and *value will be its value.
}

for multiple arguments.

Boolean options return false if no value was specified in the command line

Steps to reproduce:

  1. Copy the example to a file named src.cpp
#include "flags.h"
#include <iostream>

int main(int argc, char** argv) {
  const flags::args args(argc, argv);

  const auto count = args.get<int>("count");
  if (!count) {
    std::cerr << "No count supplied. :(\n";
    return 1;
  }
  std::cout << "That's " << *count << " incredible, colossal credits!\n";

  if (args.get<bool>("laugh", false)) {
    std::cout << "Ha ha ha ha!\n";
  }
  return 0;
}
  1. Add flags.h to the same folder.
  2. Compile using clang src.cpp -std=c++17 -o program.exe
  3. Run using .\program.exe --count 5 --laugh

Expected result:

That's 5 incredible, colossal credits!
Ha ha ha ha!

Actual result:

That's 5 incredible, colossal credits!

Environment:

  • OS: Windows 10
  • Compiler: Clang 9.0.0

Reason behind the issue:

template <>
std::optional<bool> get(const argument_map& options,
                        const std::string_view& option) {
  if (const auto value = get_value(options, option)) {
    return std::none_of(falsities.begin(), falsities.end(),
                        [&value](auto falsity) { return *value == falsity; });
  }
  return std::nullopt;
}

In the aforementioned code, the function cannot differentiate between the two scenarios:

  1. .\program.exe --count 5 --laugh
  2. .\program.exe --count 5

Since in both cases, args.get<bool>("laugh") will return std::nullopt.

Suggested fix:
Check if the option exists in the options map.

template <>
std::optional<bool> get(const argument_map& options,
                        const std::string_view& option) {
  if (const auto value = get_value(options, option)) {
    return std::none_of(falsities.begin(), falsities.end(),
                        [&value](auto falsity) { return *value == falsity; });
  }
  if (options.find(option) != options.end())
      return true;
  return std::nullopt;
}

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.