项目作者: StefanoLusardi

项目描述 :
Lightweight Command Line Input Parser for modern C++
高级语言: C++
项目地址: git://github.com/StefanoLusardi/clip.git
创建时间: 2020-02-09T20:16:35Z
项目社区:https://github.com/StefanoLusardi/clip

开源协议:MIT License

下载


clip

Linux: Build Status
Windows: Build Status

Codacy Badge
contributions welcome
GitHub
HitCount

GitHub top language
GitHub language count
GitHub code size in bytes
GitHub repo size

GitHub last commit
GitHub release (latest by date)
GitHub Release Date

An extremely simple to use, yet fully fledged, C++ Command Line Parser library (compiled and single-header-only).

Integration

Header-Only

Copy the single header file clip.hpp within your project and just use it.
Requires a C++17 compiler.

Compile

Building the library is the preferrable approach since it leads to faster compile and link times.
Requires CMake (> 3.8) and a C++17 compiler.

  1. $ git clone https://github.com/stefanolusardi/clip.git
  2. $ cd clip && mkdir build && cd build
  3. $ cmake --build . --config Release
  4. $ cmake --build . --config Debug
  5. $ cmake --install . --prefix <ANYWHERE_YOU_LIKE>

Platforms & Compilers

  • Windows 10 - MSVC 16
  • Ubuntu 18.04 - GCC 9

Basic Usage

  1. // Create clip::CommandLineParser object
  2. clip::CommandLineParser clp;
  3. // Create and add clip::PositionalArgument
  4. clip::PositionalArgument posArg("positional_arg");
  5. clp.addPositionalArgument(posArg);
  6. // Create and add clip::OptionalArgument
  7. clip::OptionalArgument optArg({ "a", "a-opt", "a-opt-arg" }, "Opt Arg Description");
  8. clp.addOptionalArgument(optArg);
  9. // Create and add clip::OptionalArgument with:
  10. // expected type (e.g. std::string), default value (e.g. "initial_value") and other options (e.g. isRequired)
  11. clip::OptionalArgument optArgValue({ "c" }, "Description", clip::value<std::string>("initial_value").isRequired(true));
  12. clp.addOptionalArgument(optArgValue);
  13. // Invoke parse method
  14. clp.parse(argc, argv);
  15. if (clp.isSet(posArg))
  16. {
  17. std::cout << "positional_arg is set." << std::endl;
  18. }
  19. if (clp.isSet(optArgValue))
  20. {
  21. auto v = clp.getOptionValue(optArgValue);
  22. std::cout << "Value: " << v << std::endl;
  23. }

Argument Value Usage

  1. // -- clip::ArgumentValue<T> constructor is made private by design.
  2. //clip::ArgumentValue<std::string> argValue = clip::ArgumentValue<std::string>(); // This line will not compile.
  3. //clip::ArgumentValue<std::string>* argValue = new clip::ArgumentValue<std::string>(); // This line will not compile.
  4. // -- It is possible to create a clip::ArgumentValue<T> using the clip::value<T>() function.
  5. clip::ArgumentValue<std::string> argValue = clip::value<std::string>();
  6. // -- C++17 automatic template deduction in declaration.
  7. clip::ArgumentValue argValue = clip::value<int>();
  8. // -- Default argument value can be provided using clip::value<T>(t) override.
  9. clip::ArgumentValue argValue = clip::value<double>(3.14);
  10. // -- Fluent API to specify further options (set required options and override default value).
  11. clip::ArgumentValue argValue = clip::value<std::string>("initial_value").isRequired(true).value("some_value");

Optional Argument Usage

  1. // -- Specify a list of option names to be used during parsing to recognize this option
  2. clip::OptionalArgument optArg({ "a" });
  3. clip::OptionalArgument optArg({ "a", "option", "another-long-option", "too-much-options" });
  4. // -- Add an option description
  5. clip::OptionalArgument optArg({ "a", "option" }, "This is a brief option description");
  6. // -- Give the option a clip::ArgumentValue
  7. // -- Note that since clip::OptionalArgument<T> is a template class, when a clip::ArgumentValue is not given, the default T used is std::nullptr_t
  8. clip::OptionalArgument<std::string> optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello"));
  9. // -- C++17 automatic template deduction in declaration.
  10. clip::OptionalArgument optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello"));
  11. // -- Exploit clip::ArgumentValue fluent API to specify argument value directly inline.
  12. clip::OptionalArgument optArg({ "a", "option" }, "Option Description", clip::value<std::string>("hello").isRequired(true).value("some value"););

Examples

Examples are located within the examples folder.
It is possible to build all the examples using:

  1. $ git clone https://github.com/stefanolusardi/clip.git
  2. $ cd clip && mkdir build && cd build
  3. $ cmake --build . --config Release -DBUILD_EXAMPLES=ON

Tests

Coming soon