项目作者: nkh-lab

项目描述 :
Utility library for simplifying using of Boost::program_options.
高级语言: C++
项目地址: git://github.com/nkh-lab/prog-arg-manager.git
创建时间: 2021-08-27T09:49:51Z
项目社区:https://github.com/nkh-lab/prog-arg-manager

开源协议:

下载


Utility library for simplifying using of Boost::program_options where user can concentrate not on options/arguments configuration but on usage of them.

So, just describe the options and use them after parsing the arguments:

  1. int main(int argc, char* argv[])
  2. {
  3. nlab::Option<bool> write{"write,w", "write flag"};
  4. nlab::Option<std::string> file{"file,f", "file path and name"};
  5. if (nlab::ProgArgManager(argc, argv).parse(write, file))
  6. {
  7. // Entry point for user business logic
  8. std::cout << "write: " << write.value << "\n";
  9. std::cout << "file: " << file.value << "\n";
  10. }
  11. return 1;
  12. }

Build Status

CI

Dependencies

How it works

All options are devided to three categories:

  1. Generic: hardcoded help and version options.
  2. Optional: user defined.
  3. Mandatory: user defined.

The following diagram shows the arguments handling logic:

Usage

This project contains a simple example of how to use the library that covers the basic cases of options/arguments usage.

Example source code: test/component_test/main.cpp

How to build example

  1. $ mkdir build && cd build
  2. $ cmake -Dprog-arg-manager_BUILD_TESTS=on ..
  3. $ make

Executable binary prog-arg-manager-ctest is here:

  1. $ cd build/test/component_test

How to run example

Run example without all mandatory options or with “-h [ —help ]”, help text should be shown:

  1. $ ./prog-arg-manager-ctest -h
  2. Allowed options:
  3. Generic:
  4. -h [ --help ] display this help text and exit
  5. -v [ --version ] display version information and exit
  6. Optional:
  7. --num arg to test numeric arguments
  8. -r [ --read ] read flag to test boolean
  9. -w [ --write ] write flag to test boolean
  10. -d [ --delete ] delete flag to test boolean
  11. Mandatory (last option name can be omitted):
  12. -p [ --path ] arg path to test string
  13. -f [ --files ] arg files to test vector<string>

Run example with “-v [ —version ]” to show version:

  1. $ ./prog-arg-manager-ctest -v
  2. 1.0.0

Run example with all mandatory options:

  1. $ ./prog-arg-manager-ctest --num 789 -rw -p /path/to/my/dir file1.txt file2.txt file3.txt
  2. num: 789
  3. read: 1
  4. write: 1
  5. del: 0
  6. path: /path/to/my/dir
  7. files:
  8. file1.txt
  9. file2.txt
  10. file3.txt