Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
302302b308 | ||
|
|
5ce83d014c | ||
|
|
94c09511dd | ||
|
|
56432321b8 | ||
|
|
c4bd30be57 |
@ -1,19 +0,0 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: LLVM
|
||||
AccessModifierOffset: 0
|
||||
AlignConsecutiveAssignments: true
|
||||
AlwaysBreakAfterReturnType: TopLevel
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterEnum: true
|
||||
AfterClass: true
|
||||
AfterStruct: true
|
||||
AfterFunction: true
|
||||
AfterNamespace: true
|
||||
AfterControlStatement: Always
|
||||
BeforeElse: true
|
||||
ConstructorInitializerIndentWidth: 2
|
||||
ContinuationIndentWidth: 2
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Left
|
||||
24
.github/workflows/build.yml
vendored
24
.github/workflows/build.yml
vendored
@ -1,24 +0,0 @@
|
||||
name: Build cxxopts
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ $default-branch ]
|
||||
pull_request:
|
||||
branches: [ $default-branch ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: setup
|
||||
run: mkdir build; cd build
|
||||
- name: cmake
|
||||
run: cmake ..
|
||||
- name: Build
|
||||
run: make -j$(nproc)
|
||||
- name: test
|
||||
run: ctest
|
||||
24
.github/workflows/cifuzz.yml
vendored
24
.github/workflows/cifuzz.yml
vendored
@ -1,24 +0,0 @@
|
||||
name: CIFuzz
|
||||
on: [pull_request]
|
||||
jobs:
|
||||
Fuzzing:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Build Fuzzers
|
||||
id: build
|
||||
uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
|
||||
with:
|
||||
oss-fuzz-project-name: 'cxxopts'
|
||||
language: c++
|
||||
- name: Run Fuzzers
|
||||
uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
|
||||
with:
|
||||
oss-fuzz-project-name: 'cxxopts'
|
||||
language: c++
|
||||
fuzz-seconds: 300
|
||||
- name: Upload Crash
|
||||
uses: actions/upload-artifact@v3
|
||||
if: failure() && steps.build.outcome == 'success'
|
||||
with:
|
||||
name: artifacts
|
||||
path: ./out/artifacts
|
||||
69
.github/workflows/cmake.yml
vendored
69
.github/workflows/cmake.yml
vendored
@ -1,69 +0,0 @@
|
||||
name: CMake
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, main ]
|
||||
pull_request:
|
||||
branches: [ master, main ]
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
|
||||
BUILD_TYPE: Release
|
||||
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
|
||||
jobs:
|
||||
build-ubuntu:
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ ubuntu-20.04, ubuntu-22.04 ]
|
||||
compiler: [ g++-9, g++-10, clang++ ]
|
||||
|
||||
name: Build and Test on Ubuntu
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Configure CMake
|
||||
run: cmake -S "${{github.workspace}}" -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=${{matrix.compiler}}
|
||||
- name: Build
|
||||
run: cmake --build "${{github.workspace}}/build" --config $BUILD_TYPE
|
||||
- name: Test
|
||||
working-directory: ${{github.workspace}}/build/test
|
||||
run: ctest -C $BUILD_TYPE --output-on-failure
|
||||
|
||||
build-macos:
|
||||
name: Build and Test on MacOS
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ macos-11, macos-12 ]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Configure CMake
|
||||
run: cmake -S "${{github.workspace}}" -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
||||
- name: Show compile commands
|
||||
run: cat build/compile_commands.json
|
||||
- name: Build
|
||||
run: cmake --build "${{github.workspace}}/build" --config $BUILD_TYPE
|
||||
- name: Test
|
||||
working-directory: ${{github.workspace}}/build/test
|
||||
shell: bash
|
||||
run: ctest -C $BUILD_TYPE --output-on-failure
|
||||
|
||||
build-windows:
|
||||
name: Build and Test on Windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ilammy/msvc-dev-cmd@v1
|
||||
- name: Configure CMake
|
||||
run: cmake -S "${{github.workspace}}" -B "${{github.workspace}}/build" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -T "ClangCl"
|
||||
- name: Build
|
||||
run: cmake --build "${{github.workspace}}/build" --config $BUILD_TYPE
|
||||
- name: Test
|
||||
working-directory: ${{github.workspace}}/build/test
|
||||
run: cd $BUILD_TYPE && ./link_test && ./options_test
|
||||
77
.gitignore
vendored
77
.gitignore
vendored
@ -1,77 +1,8 @@
|
||||
syntax: glob
|
||||
|
||||
# Temporary, cache, swap files
|
||||
\#\#*
|
||||
*.swp
|
||||
*.bkp
|
||||
|
||||
# Files which "ask" to be hidden
|
||||
*~
|
||||
.*
|
||||
unused/
|
||||
|
||||
# Build artifacts
|
||||
*.a
|
||||
*.o
|
||||
*.so
|
||||
*.ptx
|
||||
bin/*
|
||||
lib/*
|
||||
build/
|
||||
build-*/
|
||||
bazel-*
|
||||
|
||||
# Core dumps
|
||||
core
|
||||
core.*
|
||||
core-*
|
||||
|
||||
# CMake & CTest-generated files
|
||||
build*
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
cmake_install.cmake
|
||||
CMakeScripts/*
|
||||
CMakeTmp/*
|
||||
Makefile
|
||||
CTestTestfile.cmake
|
||||
CMakeFiles/
|
||||
Testing/
|
||||
|
||||
# Eclise IDE-related files
|
||||
.project
|
||||
.cproject
|
||||
.settings
|
||||
|
||||
# CLion IDE-related files
|
||||
.idea/
|
||||
cmake-build-*/
|
||||
|
||||
# Patching
|
||||
*.diff
|
||||
*.rej
|
||||
*.orig
|
||||
|
||||
# Files/folders downloaded from other repositories as part of the build
|
||||
external/*
|
||||
third-party/*
|
||||
|
||||
# Miscellaneous
|
||||
tags
|
||||
log
|
||||
*.log
|
||||
*.v3breakpoints
|
||||
gmon.out
|
||||
.DS_Store
|
||||
|
||||
# Doxygen
|
||||
doxygen.log
|
||||
Doxyfile
|
||||
docs/
|
||||
|
||||
# Archives
|
||||
*.zip
|
||||
*.gz
|
||||
*.bz2
|
||||
*.tgz
|
||||
*.tar
|
||||
*.xz
|
||||
|
||||
CTestTestfile.cmake
|
||||
cmake_install.cmake
|
||||
|
||||
@ -1 +0,0 @@
|
||||
{ }
|
||||
@ -1 +0,0 @@
|
||||
|
||||
38
.travis.yml
38
.travis.yml
@ -6,35 +6,21 @@ os:
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
env: COMPILER=g++-6
|
||||
env: COMPILER=g++-4.9
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-6
|
||||
- g++-4.9
|
||||
sources: &sources
|
||||
- llvm-toolchain-trusty-3.8
|
||||
- llvm-toolchain-trusty-5.0
|
||||
- ubuntu-toolchain-r-test
|
||||
- os: linux
|
||||
env: COMPILER=g++-6 UNICODE_OPTIONS=-DCXXOPTS_USE_UNICODE_HELP=Yes
|
||||
env: COMPILER=g++-4.9 UNICODE_OPTIONS=-DCXXOPTS_USE_UNICODE_HELP=Yes
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-6
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=g++-7
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-7
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=g++-7 UNICODE_OPTIONS=-DCXXOPTS_USE_UNICODE_HELP=Yes
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-7
|
||||
- g++-4.9
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=g++-5
|
||||
@ -50,20 +36,6 @@ matrix:
|
||||
packages:
|
||||
- g++-5
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=g++-4.8
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-4.8
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=g++-4.8 UNICODE_OPTIONS=-DCXXOPTS_USE_UNICODE_HELP=Yes
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- g++-4.8
|
||||
sources: *sources
|
||||
- os: linux
|
||||
env: COMPILER=clang++-3.8 CXXFLAGS=-stdlib=libc++
|
||||
addons:
|
||||
@ -89,7 +61,7 @@ matrix:
|
||||
- g++-5
|
||||
sources: *sources
|
||||
script: >
|
||||
cmake -Werror=dev -DCXXOPTS_BUILD_TESTS=ON -DCMAKE_CXX_COMPILER=$COMPILER
|
||||
cmake -DCXXOPTS_BUILD_TESTS=ON -DCMAKE_CXX_COMPILER=$COMPILER
|
||||
-DCMAKE_CXX_FLAGS=$CXXFLAGS $UNICODE_OPTIONS $CMAKE_OPTIONS .
|
||||
&& make && make ARGS=--output-on-failure test
|
||||
|
||||
|
||||
16
BUILD.bazel
16
BUILD.bazel
@ -1,16 +0,0 @@
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "cxxopts",
|
||||
hdrs = ["include/cxxopts.hpp"],
|
||||
strip_include_prefix = "include",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
|
||||
|
||||
cc_fuzz_test(
|
||||
name = "cxxopts_fuzz_test",
|
||||
srcs = ["test/fuzz.cpp"],
|
||||
deps = [":cxxopts"],
|
||||
)
|
||||
72
CHANGELOG.md
72
CHANGELOG.md
@ -3,81 +3,17 @@
|
||||
This is the changelog for `cxxopts`, a C++11 library for parsing command line
|
||||
options. The project adheres to semantic versioning.
|
||||
|
||||
## 3.2.1
|
||||
|
||||
### Bug fixes
|
||||
|
||||
* Fix compilation with optional on C++20.
|
||||
|
||||
## 3.2
|
||||
|
||||
### Bug fixes
|
||||
|
||||
* Fix unannotated fallthrough.
|
||||
* Fix sign conversion with Unicode output.
|
||||
* Don't initialize regex in static initialiser.
|
||||
* Fix incorrect integer overflow checks.
|
||||
|
||||
### Added
|
||||
|
||||
* Add fuzzing to CI
|
||||
|
||||
### Changed
|
||||
|
||||
* Change quote output to '' to match Windows.
|
||||
* Don't split positional arguments by the list delimiter.
|
||||
* Order help groups by the order they were added.
|
||||
|
||||
## 3.1.1
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Fixed version number in header.
|
||||
* Fixed cast warning in Unicode function.
|
||||
|
||||
## 3.1
|
||||
|
||||
### Added
|
||||
|
||||
* Support for multiple long names for the same option (= multiple long aliases)
|
||||
* Add a `program()` function to retrieve the program name.
|
||||
* Added a .clang-format file.
|
||||
* Added iterator and printing for a ParseResult.
|
||||
|
||||
### Changed
|
||||
|
||||
* Cleanup exception code, add cxxopts::exceptions namespace.
|
||||
* Renamed several exceptions to be more descriptive, and added to a nested namespace.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Fix `arguments()` having no key for options that only have a short name.
|
||||
|
||||
## 3.0
|
||||
## 2.2.1
|
||||
|
||||
### Changed
|
||||
|
||||
* Only search for a C++ compiler in CMakeLists.txt.
|
||||
* Allow for exceptions to be disabled.
|
||||
* Fix duplicate default options when there is a short and long option.
|
||||
* Add `CXXOPTS_NO_EXCEPTIONS` to disable exceptions.
|
||||
* Fix char parsing for space and check for length.
|
||||
* Change argument type in `Options::parse` from `char**` to `const char**`.
|
||||
* Refactor parser to not change its arguments.
|
||||
* `ParseResult` doesn't depend on a reference to the parser.
|
||||
* Fixed several warnings and code quality issues.
|
||||
* Improved formatting for help descriptions.
|
||||
* Improve integer parsing.
|
||||
|
||||
### Added
|
||||
|
||||
* A list of unmatched arguments is available in `ParseResult`.
|
||||
* Support single letter options with argument attached.
|
||||
* Use <optional> if it is present.
|
||||
* Allow installing to be disabled.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Fix missing option name in exception.
|
||||
* Fix error printing long help lines.
|
||||
* Fix duplicate default options when there is a short and long option.
|
||||
|
||||
## 2.2
|
||||
|
||||
|
||||
123
CMakeLists.txt
123
CMakeLists.txt
@ -1,15 +1,15 @@
|
||||
# Copyright (c) 2014 Jarryd Beck
|
||||
#
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@ -17,66 +17,91 @@
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
cmake_minimum_required(VERSION 3.1...3.19)
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
|
||||
include(cxxopts)
|
||||
set("PROJECT_DESCRIPTION" "A header-only lightweight C++ command line option parser")
|
||||
set("PROJECT_HOMEPAGE_URL" "https://github.com/jarro2783/cxxopts")
|
||||
# parse the current version from the cxxopts header
|
||||
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp" cxxopts_version_defines
|
||||
REGEX "#define CXXOPTS__VERSION_(MAJOR|MINOR|PATCH)")
|
||||
foreach(ver ${cxxopts_version_defines})
|
||||
if(ver MATCHES "#define CXXOPTS__VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
|
||||
set(CXXOPTS__VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
|
||||
endif()
|
||||
endforeach()
|
||||
set(VERSION ${CXXOPTS__VERSION_MAJOR}.${CXXOPTS__VERSION_MINOR}.${CXXOPTS__VERSION_PATCH})
|
||||
message(STATUS "cxxopts version ${VERSION}")
|
||||
|
||||
# Get the version of the library
|
||||
cxxopts_getversion(VERSION)
|
||||
project(cxxopts VERSION "${VERSION}" LANGUAGES CXX)
|
||||
|
||||
project(cxxopts
|
||||
VERSION "${VERSION}"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
enable_testing()
|
||||
|
||||
# Must include after the project call due to GNUInstallDirs requiring a language be enabled (IE. CXX)
|
||||
include(GNUInstallDirs)
|
||||
option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON)
|
||||
option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" ON)
|
||||
option(CXXOPTS_ENABLE_INSTALL "Generate the install target" ON)
|
||||
|
||||
# Determine whether this is a standalone project or included by other projects
|
||||
set(CXXOPTS_STANDALONE_PROJECT OFF)
|
||||
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
||||
set(CXXOPTS_STANDALONE_PROJECT ON)
|
||||
# request c++11 without gnu extension for the whole project and enable more warnings
|
||||
if (CXXOPTS_CXX_STANDARD)
|
||||
set(CMAKE_CXX_STANDARD ${CXXOPTS_CXX_STANDARD})
|
||||
else()
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
endif()
|
||||
|
||||
# Establish the project options
|
||||
option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ${CXXOPTS_STANDALONE_PROJECT})
|
||||
option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" ${CXXOPTS_STANDALONE_PROJECT})
|
||||
option(CXXOPTS_ENABLE_INSTALL "Generate the install target" ${CXXOPTS_STANDALONE_PROJECT})
|
||||
option(CXXOPTS_ENABLE_WARNINGS "Add warnings to CMAKE_CXX_FLAGS" ${CXXOPTS_STANDALONE_PROJECT})
|
||||
option(CXXOPTS_USE_UNICODE_HELP "Use ICU Unicode library" OFF)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
if (CXXOPTS_STANDALONE_PROJECT)
|
||||
cxxopts_set_cxx_standard()
|
||||
endif()
|
||||
|
||||
if (CXXOPTS_ENABLE_WARNINGS)
|
||||
cxxopts_enable_warnings()
|
||||
if(MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2")
|
||||
elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow")
|
||||
endif()
|
||||
|
||||
add_library(cxxopts INTERFACE)
|
||||
add_library(cxxopts::cxxopts ALIAS cxxopts)
|
||||
add_subdirectory(include)
|
||||
|
||||
# Link against the ICU library when requested
|
||||
# optionally, enable unicode support using the ICU library
|
||||
set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
|
||||
if(CXXOPTS_USE_UNICODE_HELP)
|
||||
cxxopts_use_unicode()
|
||||
find_package(PkgConfig)
|
||||
pkg_check_modules(ICU REQUIRED icu-uc)
|
||||
|
||||
target_link_libraries(cxxopts INTERFACE ${ICU_LDFLAGS})
|
||||
target_compile_options(cxxopts INTERFACE ${ICU_CFLAGS})
|
||||
target_compile_definitions(cxxopts INTERFACE CXXOPTS_USE_UNICODE)
|
||||
endif()
|
||||
|
||||
# Install cxxopts when requested by the user
|
||||
if (CXXOPTS_ENABLE_INSTALL)
|
||||
cxxopts_install_logic()
|
||||
target_include_directories(cxxopts INTERFACE
|
||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
if(CXXOPTS_ENABLE_INSTALL)
|
||||
include(CMakePackageConfigHelpers)
|
||||
set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING
|
||||
"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
|
||||
set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake")
|
||||
set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake")
|
||||
set(targets_export_name cxxopts-targets)
|
||||
|
||||
# Generate the version, config and target files into the build directory.
|
||||
write_basic_package_version_file(
|
||||
${version_config}
|
||||
VERSION ${VERSION}
|
||||
COMPATIBILITY AnyNewerVersion)
|
||||
configure_package_config_file(
|
||||
${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in
|
||||
${project_config}
|
||||
INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||
export(TARGETS cxxopts NAMESPACE cxxopts::
|
||||
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
|
||||
|
||||
# Install version, config and target files.
|
||||
install(
|
||||
FILES ${project_config} ${version_config}
|
||||
DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||
install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR}
|
||||
NAMESPACE cxxopts::)
|
||||
|
||||
# Install the header file and export the target
|
||||
install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib)
|
||||
install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
|
||||
endif()
|
||||
|
||||
# Build examples when requested by the user
|
||||
if (CXXOPTS_BUILD_EXAMPLES)
|
||||
add_subdirectory(src)
|
||||
endif()
|
||||
|
||||
# Enable testing when requested by the user
|
||||
if (CXXOPTS_BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
add_subdirectory(src)
|
||||
add_subdirectory(test)
|
||||
|
||||
23
INSTALL
23
INSTALL
@ -21,26 +21,3 @@ To run the tests, you have to configure `cxxopts` with another flag:
|
||||
cmake -D CXXOPTS_BUILD_TESTS=On ${CXXOPTS_DIR}
|
||||
make
|
||||
make test
|
||||
|
||||
== Using cxxopts in tipi.build projects ==
|
||||
|
||||
`cxxopts` can be easily used in [tipi.build](https://tipi.build) projects simply by adding the following entry to your `.tipi/deps`:
|
||||
|
||||
```json
|
||||
{
|
||||
"jarro2783/cxxopts": { "@": "v3.0.0" }
|
||||
}
|
||||
```
|
||||
|
||||
To try this you can run the following command in `/src` (change the target name appropriately to `linux` or `macos` or `windows`):
|
||||
|
||||
```bash
|
||||
tipi . -t <target>
|
||||
./build/linux-cxx17/bin/test_package -v
|
||||
```
|
||||
|
||||
To develop `cxxopts` using tipi run the following command at the root of the repository:
|
||||
|
||||
```bash
|
||||
tipi . -t <target> --test all -v
|
||||
```
|
||||
|
||||
183
README.md
183
README.md
@ -5,25 +5,6 @@
|
||||
Note that `master` is generally a work in progress, and you probably want to use a
|
||||
tagged release version.
|
||||
|
||||
## Version 3 breaking changes
|
||||
|
||||
If you have used version 2, there are a couple of breaking changes in version 3
|
||||
that you should be aware of. If you are new to `cxxopts` you can skip this
|
||||
section.
|
||||
|
||||
The parser no longer modifies its arguments, so you can pass a const `argc` and
|
||||
`argv` and expect them not to be changed.
|
||||
|
||||
The `ParseResult` object no longer depends on the parser. So it can be returned
|
||||
from a scope outside the parser and still work. Now that the inputs are not
|
||||
modified, `ParseResult` stores a list of the unmatched arguments. These are
|
||||
retrieved like follows:
|
||||
|
||||
```cpp
|
||||
auto result = options.parse(argc, argv);
|
||||
result.unmatched(); // get the unmatched arguments
|
||||
```
|
||||
|
||||
# Quick start
|
||||
|
||||
This is a lightweight C++ option parser library, supporting the standard GNU
|
||||
@ -44,26 +25,18 @@ Additionally, anything after `--` will be parsed as a positional argument.
|
||||
|
||||
## Basics
|
||||
|
||||
```cpp
|
||||
#include <cxxopts.hpp>
|
||||
```
|
||||
#include <cxxopts.hpp>
|
||||
|
||||
Create a `cxxopts::Options` instance.
|
||||
Create a cxxopts::Options instance.
|
||||
|
||||
```cpp
|
||||
cxxopts::Options options("MyProgram", "One line description of MyProgram");
|
||||
```
|
||||
cxxopts::Options options("MyProgram", "One line description of MyProgram");
|
||||
|
||||
Then use `add_options`.
|
||||
|
||||
```cpp
|
||||
options.add_options()
|
||||
("d,debug", "Enable debugging") // a bool parameter
|
||||
("i,integer", "Int param", cxxopts::value<int>())
|
||||
("f,file", "File name", cxxopts::value<std::string>())
|
||||
("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
|
||||
;
|
||||
```
|
||||
options.add_options()
|
||||
("d,debug", "Enable debugging")
|
||||
("f,file", "File name", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
Options are declared with a long and an optional short option. A description
|
||||
must be provided. The third argument is the value, if omitted it is boolean.
|
||||
@ -71,44 +44,26 @@ Any type can be given as long as it can be parsed, with operator>>.
|
||||
|
||||
To parse the command line do:
|
||||
|
||||
```cpp
|
||||
auto result = options.parse(argc, argv);
|
||||
```
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
To retrieve an option use `result.count("option")` to get the number of times
|
||||
it appeared, and
|
||||
|
||||
```cpp
|
||||
result["opt"].as<type>()
|
||||
```
|
||||
result["opt"].as<type>()
|
||||
|
||||
to get its value. If "opt" doesn't exist, or isn't of the right type, then an
|
||||
exception will be thrown.
|
||||
|
||||
## Unrecognised arguments
|
||||
|
||||
You can allow unrecognised arguments to be skipped. This applies to both
|
||||
positional arguments that are not parsed into another option, and `--`
|
||||
arguments that do not match an argument that you specify. This is done by
|
||||
calling:
|
||||
|
||||
```cpp
|
||||
options.allow_unrecognised_options();
|
||||
```
|
||||
|
||||
and in the result object they are retrieved with:
|
||||
|
||||
```cpp
|
||||
result.unmatched()
|
||||
```
|
||||
Note that the result of `options.parse` should only be used as long as the
|
||||
`options` object that created it is in scope.
|
||||
|
||||
## Exceptions
|
||||
|
||||
Exceptional situations throw C++ exceptions. There are two types of
|
||||
exceptions: errors defining the options, and errors when parsing a list of
|
||||
arguments. All exceptions derive from `cxxopts::exceptions::exception`. Errors
|
||||
defining options derive from `cxxopts::exceptions::specification` and errors
|
||||
parsing arguments derive from `cxxopts::exceptions::parsing`.
|
||||
arguments. All exceptions derive from `cxxopts::OptionException`. Errors
|
||||
defining options derive from `cxxopts::OptionSpecException` and errors
|
||||
parsing arguments derive from `cxxopts::OptionParseException`.
|
||||
|
||||
All exceptions define a `what()` function to get a printable string
|
||||
explaining the error.
|
||||
@ -122,37 +77,13 @@ vector to the `help` function.
|
||||
|
||||
## Positional Arguments
|
||||
|
||||
Positional arguments are those given without a preceding flag and can be used
|
||||
alongside non-positional arguments. There may be multiple positional arguments,
|
||||
and the final positional argument may be a container type to hold a list of all
|
||||
remaining positionals.
|
||||
Positional arguments can be optionally parsed into one or more options.
|
||||
To set up positional arguments, call
|
||||
|
||||
To set up positional arguments, first declare the options, then configure a
|
||||
set of those arguments as positional like:
|
||||
options.parse_positional({"first", "second", "last"})
|
||||
|
||||
```cpp
|
||||
options.add_options()
|
||||
("script", "The script file to execute", cxxopts::value<std::string>())
|
||||
("server", "The server to execute on", cxxopts::value<std::string>())
|
||||
("filenames", "The filename(s) to process", cxxopts::value<std::vector<std::string>>());
|
||||
|
||||
options.parse_positional({"script", "server", "filenames"});
|
||||
|
||||
// Parse options the usual way
|
||||
options.parse(argc, argv);
|
||||
```
|
||||
|
||||
For example, parsing the following arguments:
|
||||
~~~
|
||||
my_script.py my_server.com file1.txt file2.txt file3.txt
|
||||
~~~
|
||||
will result in parsed arguments like the following table:
|
||||
|
||||
| Field | Value |
|
||||
| ------------- | ----------------------------------------- |
|
||||
| `"script"` | `"my_script.py"` |
|
||||
| `"server"` | `"my_server.com"` |
|
||||
| `"filenames"` | `{"file1.txt", "file2.txt", "file3.txt"}` |
|
||||
where "last" should be the name of an option with a container type, and the
|
||||
others should have a single value.
|
||||
|
||||
## Default and implicit values
|
||||
|
||||
@ -161,16 +92,12 @@ An option can be declared with a default or an implicit value, or both.
|
||||
A default value is the value that an option takes when it is not specified
|
||||
on the command line. The following specifies a default value for an option:
|
||||
|
||||
```cpp
|
||||
cxxopts::value<std::string>()->default_value("value")
|
||||
```
|
||||
cxxopts::value<std::string>()->default_value("value")
|
||||
|
||||
An implicit value is the value that an option takes when it is given on the
|
||||
command line without an argument. The following specifies an implicit value:
|
||||
|
||||
```cpp
|
||||
cxxopts::value<std::string>()->implicit_value("implicit")
|
||||
```
|
||||
cxxopts::value<std::string>()->implicit_value("implicit")
|
||||
|
||||
If an option had both, then not specifying it would give the value `"value"`,
|
||||
writing it on the command line as `--option` would give the value `"implicit"`,
|
||||
@ -180,8 +107,6 @@ Note that the default and implicit value is always stored as a string,
|
||||
regardless of the type that you want to store it in. It will be parsed as
|
||||
though it was given on the command line.
|
||||
|
||||
Default values are not counted by `Options::count`.
|
||||
|
||||
## Boolean values
|
||||
|
||||
Boolean options have a default implicit value of `"true"`, which can be
|
||||
@ -193,8 +118,8 @@ therefore, `-o false` does not work.
|
||||
|
||||
## `std::vector<T>` values
|
||||
|
||||
Parsing a list of values into a `std::vector<T>` is also supported, as long as `T`
|
||||
can be parsed. To separate single values in a list the define symbol `CXXOPTS_VECTOR_DELIMITER`
|
||||
Parsing of list of values in form of an `std::vector<T>` is also supported, as long as `T`
|
||||
can be parsed. To separate single values in a list the definition `CXXOPTS_VECTOR_DELIMITER`
|
||||
is used, which is ',' by default. Ensure that you use no whitespaces between values because
|
||||
those would be interpreted as the next command line option. Example for a command line option
|
||||
that can be parsed as a `std::vector<double>`:
|
||||
@ -203,75 +128,21 @@ that can be parsed as a `std::vector<double>`:
|
||||
--my_list=1,-2.1,3,4.5
|
||||
~~~
|
||||
|
||||
## Options specified multiple times
|
||||
|
||||
The same option can be specified several times, with different arguments, which will all
|
||||
be recorded in order of appearance. An example:
|
||||
|
||||
~~~
|
||||
--use train --use bus --use ferry
|
||||
~~~
|
||||
|
||||
this is supported through the use of a vector of value for the option:
|
||||
|
||||
~~~
|
||||
options.add_options()
|
||||
("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())
|
||||
~~~
|
||||
|
||||
## Custom help
|
||||
|
||||
The string after the program name on the first line of the help can be
|
||||
completely replaced by calling `options.custom_help`. Note that you might
|
||||
also want to override the positional help by calling `options.positional_help`.
|
||||
|
||||
|
||||
## Example
|
||||
|
||||
Putting all together:
|
||||
```cpp
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
cxxopts::Options options("test", "A brief description");
|
||||
|
||||
options.add_options()
|
||||
("b,bar", "Param bar", cxxopts::value<std::string>())
|
||||
("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
|
||||
("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
|
||||
("h,help", "Print usage")
|
||||
;
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
if (result.count("help"))
|
||||
{
|
||||
std::cout << options.help() << std::endl;
|
||||
exit(0);
|
||||
}
|
||||
bool debug = result["debug"].as<bool>();
|
||||
std::string bar;
|
||||
if (result.count("bar"))
|
||||
bar = result["bar"].as<std::string>();
|
||||
int foo = result["foo"].as<int>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
# Linking
|
||||
|
||||
This is a header only library.
|
||||
|
||||
# Requirements
|
||||
|
||||
The only build requirement is a C++ compiler that supports C++11 features such as:
|
||||
The only build requirement is a C++ compiler that supports C++11 regular
|
||||
expressions. For example GCC >= 4.9 or clang with libc++.
|
||||
|
||||
* regex
|
||||
* constexpr
|
||||
* default constructors
|
||||
# TODO list
|
||||
|
||||
GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.
|
||||
|
||||
The following compilers are known not to work:
|
||||
|
||||
* MSVC 2013
|
||||
* Allow unrecognised options.
|
||||
|
||||
16
WORKSPACE
16
WORKSPACE
@ -1,16 +0,0 @@
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
|
||||
http_archive(
|
||||
name = "rules_fuzzing",
|
||||
sha256 = "d9002dd3cd6437017f08593124fdd1b13b3473c7b929ceb0e60d317cb9346118",
|
||||
strip_prefix = "rules_fuzzing-0.3.2",
|
||||
urls = ["https://github.com/bazelbuild/rules_fuzzing/archive/v0.3.2.zip"],
|
||||
)
|
||||
|
||||
load("@rules_fuzzing//fuzzing:repositories.bzl", "rules_fuzzing_dependencies")
|
||||
|
||||
rules_fuzzing_dependencies()
|
||||
|
||||
load("@rules_fuzzing//fuzzing:init.bzl", "rules_fuzzing_init")
|
||||
|
||||
rules_fuzzing_init()
|
||||
@ -1,163 +0,0 @@
|
||||
# Copyright (c) 2014 Jarryd Beck
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
if (CMAKE_VERSION VERSION_GREATER 3.10 OR CMAKE_VERSION VERSION_EQUAL 3.10)
|
||||
# Use include_guard() added in cmake 3.10
|
||||
include_guard()
|
||||
endif()
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
function(cxxopts_getversion version_arg)
|
||||
# Parse the current version from the cxxopts header
|
||||
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp" cxxopts_version_defines
|
||||
REGEX "#define CXXOPTS__VERSION_(MAJOR|MINOR|PATCH)")
|
||||
foreach(ver ${cxxopts_version_defines})
|
||||
if(ver MATCHES "#define CXXOPTS__VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
|
||||
set(CXXOPTS__VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
|
||||
endif()
|
||||
endforeach()
|
||||
set(VERSION ${CXXOPTS__VERSION_MAJOR}.${CXXOPTS__VERSION_MINOR}.${CXXOPTS__VERSION_PATCH})
|
||||
|
||||
# Give feedback to the user. Prefer DEBUG when available since large projects tend to have a lot
|
||||
# going on already
|
||||
if (CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15)
|
||||
message(DEBUG "cxxopts version ${VERSION}")
|
||||
else()
|
||||
message(STATUS "cxxopts version ${VERSION}")
|
||||
endif()
|
||||
|
||||
# Return the information to the caller
|
||||
set(${version_arg} ${VERSION} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Optionally, enable unicode support using the ICU library
|
||||
function(cxxopts_use_unicode)
|
||||
find_package(PkgConfig)
|
||||
pkg_check_modules(ICU REQUIRED icu-uc)
|
||||
|
||||
target_link_libraries(cxxopts INTERFACE ${ICU_LDFLAGS})
|
||||
target_compile_options(cxxopts INTERFACE ${ICU_CFLAGS})
|
||||
target_compile_definitions(cxxopts INTERFACE CXXOPTS_USE_UNICODE)
|
||||
endfunction()
|
||||
|
||||
# Request C++11 without gnu extension for the whole project and enable more warnings
|
||||
macro(cxxopts_set_cxx_standard)
|
||||
if (CXXOPTS_CXX_STANDARD)
|
||||
set(CMAKE_CXX_STANDARD ${CXXOPTS_CXX_STANDARD})
|
||||
else()
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
endmacro()
|
||||
|
||||
# Helper function to enable warnings
|
||||
function(cxxopts_enable_warnings)
|
||||
if(MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2")
|
||||
elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 5.0)
|
||||
set(COMPILER_SPECIFIC_FLAGS "-Wsuggest-override")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow -Weffc++ -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion ${COMPILER_SPECIFIC_FLAGS}")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Helper function to ecapsulate install logic
|
||||
function(cxxopts_install_logic)
|
||||
if(CMAKE_LIBRARY_ARCHITECTURE)
|
||||
string(REPLACE "/${CMAKE_LIBRARY_ARCHITECTURE}" "" CMAKE_INSTALL_LIBDIR_ARCHIND "${CMAKE_INSTALL_LIBDIR}")
|
||||
else()
|
||||
# On some systems (e.g. NixOS), `CMAKE_LIBRARY_ARCHITECTURE` can be empty
|
||||
set(CMAKE_INSTALL_LIBDIR_ARCHIND "${CMAKE_INSTALL_LIBDIR}")
|
||||
endif()
|
||||
set(CXXOPTS_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR_ARCHIND}/cmake/cxxopts" CACHE STRING "Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
|
||||
set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake")
|
||||
set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake")
|
||||
set(targets_export_name cxxopts-targets)
|
||||
set(PackagingTemplatesDir "${PROJECT_SOURCE_DIR}/packaging")
|
||||
|
||||
|
||||
if(${CMAKE_VERSION} VERSION_GREATER "3.14")
|
||||
set(OPTIONAL_ARCH_INDEPENDENT "ARCH_INDEPENDENT")
|
||||
endif()
|
||||
|
||||
# Generate the version, config and target files into the build directory.
|
||||
write_basic_package_version_file(
|
||||
${version_config}
|
||||
VERSION ${VERSION}
|
||||
COMPATIBILITY AnyNewerVersion
|
||||
${OPTIONAL_ARCH_INDEPENDENT}
|
||||
)
|
||||
configure_package_config_file(
|
||||
${PackagingTemplatesDir}/cxxopts-config.cmake.in
|
||||
${project_config}
|
||||
INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||
export(TARGETS cxxopts NAMESPACE cxxopts::
|
||||
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
|
||||
|
||||
# Install version, config and target files.
|
||||
install(
|
||||
FILES ${project_config} ${version_config}
|
||||
DESTINATION ${CXXOPTS_CMAKE_DIR})
|
||||
install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR}
|
||||
NAMESPACE cxxopts::)
|
||||
|
||||
# Install the header file and export the target
|
||||
install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
|
||||
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
|
||||
set(CPACK_PACKAGE_VENDOR "cxxopt developers")
|
||||
set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}")
|
||||
set(CPACK_DEBIAN_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
|
||||
set(CPACK_RPM_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
|
||||
set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}")
|
||||
set(CPACK_PACKAGE_MAINTAINER "${CPACK_PACKAGE_VENDOR}")
|
||||
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_MAINTAINER}")
|
||||
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
|
||||
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
|
||||
|
||||
set(CPACK_DEBIAN_PACKAGE_NAME "lib${PROJECT_NAME}-dev")
|
||||
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6-dev")
|
||||
set(CPACK_DEBIAN_PACKAGE_SUGGESTS "cmake, pkg-config, pkg-conf")
|
||||
|
||||
set(CPACK_RPM_PACKAGE_NAME "lib${PROJECT_NAME}-devel")
|
||||
set(CPACK_RPM_PACKAGE_SUGGESTS "${CPACK_DEBIAN_PACKAGE_SUGGESTS}")
|
||||
|
||||
set(CPACK_DEB_COMPONENT_INSTALL ON)
|
||||
set(CPACK_RPM_COMPONENT_INSTALL ON)
|
||||
set(CPACK_NSIS_COMPONENT_INSTALL ON)
|
||||
set(CPACK_DEBIAN_COMPRESSION_TYPE "xz")
|
||||
|
||||
set(PKG_CONFIG_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc")
|
||||
configure_file("${PackagingTemplatesDir}/pkgconfig.pc.in" "${PKG_CONFIG_FILE_NAME}" @ONLY)
|
||||
install(FILES "${PKG_CONFIG_FILE_NAME}"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR_ARCHIND}/pkgconfig"
|
||||
)
|
||||
|
||||
include(CPack)
|
||||
endfunction()
|
||||
@ -1,23 +0,0 @@
|
||||
# Copyright (c) 2014 Jarryd Beck
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
target_include_directories(cxxopts INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
3903
include/cxxopts.hpp
3903
include/cxxopts.hpp
File diff suppressed because it is too large
Load Diff
@ -1,7 +0,0 @@
|
||||
prefix=@CMAKE_INSTALL_PREFIX@
|
||||
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
|
||||
|
||||
Name: @PROJECT_NAME@
|
||||
Description: @PROJECT_DESCRIPTION@
|
||||
Version: @PROJECT_VERSION@
|
||||
Cflags: -I${includedir}
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"jarro2783/cxxopts": { }
|
||||
}
|
||||
@ -1,15 +1,15 @@
|
||||
# Copyright (c) 2014 Jarryd Beck
|
||||
#
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
@ -18,7 +18,7 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example cxxopts)
|
||||
|
||||
set_property(TARGET example PROPERTY CXX_STANDARD 17)
|
||||
if(CXXOPTS_BUILD_EXAMPLES)
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example cxxopts)
|
||||
endif()
|
||||
|
||||
@ -21,18 +21,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
*/
|
||||
#include "cxxopts.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
int
|
||||
parse(int argc, const char* argv[])
|
||||
#include "cxxopts.hpp"
|
||||
|
||||
cxxopts::ParseResult
|
||||
parse(int argc, char* argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
std::unique_ptr<cxxopts::Options> allocated(new cxxopts::Options(argv[0], " - example command line options"));
|
||||
auto& options = *allocated;
|
||||
cxxopts::Options options(argv[0], " - example command line options");
|
||||
options
|
||||
.positional_help("[optional args]")
|
||||
.show_positional_help();
|
||||
@ -40,32 +39,25 @@ parse(int argc, const char* argv[])
|
||||
bool apple = false;
|
||||
|
||||
options
|
||||
.set_width(70)
|
||||
.set_tab_expansion()
|
||||
.allow_unrecognised_options()
|
||||
.add_options()
|
||||
("a,apple,ringo", "an apple", cxxopts::value<bool>(apple))
|
||||
("a,apple", "an apple", cxxopts::value<bool>(apple))
|
||||
("b,bob", "Bob")
|
||||
("char", "A character", cxxopts::value<char>())
|
||||
("t,true", "True", cxxopts::value<bool>()->default_value("true"))
|
||||
("f, file", "File", cxxopts::value<std::vector<std::string>>(), "FILE")
|
||||
("i,input", "Input", cxxopts::value<std::string>())
|
||||
("o,output", "Output file", cxxopts::value<std::string>()
|
||||
->default_value("a.out")->implicit_value("b.def"), "BIN")
|
||||
("x", "A short-only option", cxxopts::value<std::string>())
|
||||
("positional",
|
||||
"Positional arguments: these are the arguments that are entered "
|
||||
"without an option", cxxopts::value<std::vector<std::string>>())
|
||||
("long-description",
|
||||
"thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")
|
||||
("help", "Print help")
|
||||
("tab-expansion", "Tab\texpansion")
|
||||
("int", "An integer", cxxopts::value<int>(), "N")
|
||||
("float", "A floating point number", cxxopts::value<float>())
|
||||
("vector", "A list of doubles", cxxopts::value<std::vector<double>>())
|
||||
("option_that_is_too_long_for_the_help", "A very long option")
|
||||
("l,list", "List all parsed arguments (including default values)")
|
||||
("range", "Use range-for to list arguments")
|
||||
#ifdef CXXOPTS_USE_UNICODE
|
||||
("unicode", u8"A help option with non-ascii: à. Here the size of the"
|
||||
" string should be correct")
|
||||
@ -83,23 +75,7 @@ parse(int argc, const char* argv[])
|
||||
if (result.count("help"))
|
||||
{
|
||||
std::cout << options.help({"", "Group"}) << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(result.count("list"))
|
||||
{
|
||||
if(result.count("range"))
|
||||
{
|
||||
for(const auto &kv: result)
|
||||
{
|
||||
std::cout << kv.key() << " = " << kv.value() << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << result.arguments_string() << std::endl;
|
||||
}
|
||||
return true;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (apple)
|
||||
@ -113,11 +89,6 @@ parse(int argc, const char* argv[])
|
||||
std::cout << "Saw option ‘b’" << std::endl;
|
||||
}
|
||||
|
||||
if (result.count("char"))
|
||||
{
|
||||
std::cout << "Saw a character ‘" << result["char"].as<char>() << "’" << std::endl;
|
||||
}
|
||||
|
||||
if (result.count("f"))
|
||||
{
|
||||
auto& ff = result["f"].as<std::vector<std::string>>();
|
||||
@ -172,31 +143,20 @@ parse(int argc, const char* argv[])
|
||||
|
||||
std::cout << "Arguments remain = " << argc << std::endl;
|
||||
|
||||
auto arguments = result.arguments();
|
||||
std::cout << "Saw " << arguments.size() << " arguments" << std::endl;
|
||||
return result;
|
||||
|
||||
std::cout << "Unmatched options: ";
|
||||
for (const auto& option: result.unmatched())
|
||||
{
|
||||
std::cout << "'" << option << "' ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
catch (const cxxopts::exceptions::exception& e)
|
||||
} catch (const cxxopts::OptionException& e)
|
||||
{
|
||||
std::cout << "error parsing options: " << e.what() << std::endl;
|
||||
return false;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (!parse(argc, argv))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
auto result = parse(argc, argv);
|
||||
auto arguments = result.arguments();
|
||||
std::cout << "Saw " << arguments.size() << " arguments" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
link_a.cpp
|
||||
link_b.cpp
|
||||
@ -1,60 +1,35 @@
|
||||
# Copyright (c) 2014 Jarryd Beck
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
if (CXXOPTS_BUILD_TESTS)
|
||||
add_executable(options_test main.cpp options.cpp)
|
||||
target_link_libraries(options_test cxxopts)
|
||||
add_test(options options_test)
|
||||
|
||||
add_executable(options_test main.cpp options.cpp)
|
||||
target_link_libraries(options_test cxxopts)
|
||||
add_test(options options_test)
|
||||
# test if the targets are findable from the build directory
|
||||
add_test(find-package-test ${CMAKE_CTEST_COMMAND}
|
||||
-C ${CMAKE_BUILD_TYPE}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||
--build-options
|
||||
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||
"-Dcxxopts_DIR=${PROJECT_BINARY_DIR}"
|
||||
)
|
||||
|
||||
# test if the targets are findable from the build directory
|
||||
add_test(find-package-test ${CMAKE_CTEST_COMMAND}
|
||||
-C ${CMAKE_BUILD_TYPE}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/find-package-test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||
--build-options
|
||||
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||
"-Dcxxopts_DIR=${PROJECT_BINARY_DIR}"
|
||||
)
|
||||
# test if the targets are findable when add_subdirectory is used
|
||||
add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
|
||||
-C ${CMAKE_BUILD_TYPE}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||
--build-options
|
||||
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||
)
|
||||
|
||||
# test if the targets are findable when add_subdirectory is used
|
||||
add_test(add-subdirectory-test ${CMAKE_CTEST_COMMAND}
|
||||
-C ${CMAKE_BUILD_TYPE}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/add-subdirectory-test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/add-subdirectory-test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM}
|
||||
--build-options
|
||||
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
|
||||
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
|
||||
)
|
||||
|
||||
add_executable(link_test link_a.cpp link_b.cpp)
|
||||
target_link_libraries(link_test cxxopts)
|
||||
|
||||
if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND ("${CMAKE_SYSTEM}" MATCHES "Linux"))
|
||||
add_executable(fuzzer fuzz.cpp)
|
||||
target_link_libraries(fuzzer PRIVATE cxxopts)
|
||||
target_compile_options(fuzzer PRIVATE -fsanitize=fuzzer)
|
||||
target_link_options(fuzzer PRIVATE -fsanitize=fuzzer)
|
||||
add_executable(link_test link_a.cpp link_b.cpp)
|
||||
target_link_libraries(link_test cxxopts)
|
||||
endif()
|
||||
|
||||
24782
test/catch.hpp
24782
test/catch.hpp
File diff suppressed because it is too large
Load Diff
107
test/fuzz.cpp
107
test/fuzz.cpp
@ -1,107 +0,0 @@
|
||||
#include <cassert>
|
||||
#include <cxxopts.hpp>
|
||||
#include <fuzzer/FuzzedDataProvider.h>
|
||||
|
||||
constexpr int kMaxOptions = 1024;
|
||||
constexpr int kMaxArgSize = 1024;
|
||||
|
||||
enum class ParseableTypes
|
||||
{
|
||||
kInt,
|
||||
kString,
|
||||
kVectorString,
|
||||
kFloat,
|
||||
kDouble,
|
||||
|
||||
// Marker for fuzzer.
|
||||
kMaxValue,
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
add_fuzzed_option(cxxopts::Options* options, FuzzedDataProvider* provider)
|
||||
{
|
||||
assert(options);
|
||||
assert(provider);
|
||||
|
||||
options->add_options()(provider->ConsumeRandomLengthString(kMaxArgSize),
|
||||
provider->ConsumeRandomLengthString(kMaxArgSize),
|
||||
cxxopts::value<T>());
|
||||
}
|
||||
|
||||
extern "C" int
|
||||
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
|
||||
{
|
||||
try
|
||||
{
|
||||
FuzzedDataProvider provider(data, size);
|
||||
|
||||
// Randomly generate a usage string.
|
||||
cxxopts::Options options(provider.ConsumeRandomLengthString(kMaxArgSize),
|
||||
provider.ConsumeRandomLengthString(kMaxArgSize));
|
||||
|
||||
// Randomly generate a set of flags configurations.
|
||||
for (int i = 0; i < provider.ConsumeIntegralInRange<int>(0, kMaxOptions);
|
||||
i++)
|
||||
{
|
||||
switch (provider.ConsumeEnum<ParseableTypes>())
|
||||
{
|
||||
case ParseableTypes::kInt:
|
||||
add_fuzzed_option<int>(&options, &provider);
|
||||
break;
|
||||
case ParseableTypes::kString:
|
||||
add_fuzzed_option<std::string>(&options, &provider);
|
||||
break;
|
||||
case ParseableTypes::kVectorString:
|
||||
add_fuzzed_option<std::vector<std::string>>(&options, &provider);
|
||||
break;
|
||||
case ParseableTypes::kFloat:
|
||||
add_fuzzed_option<float>(&options, &provider);
|
||||
break;
|
||||
case ParseableTypes::kDouble:
|
||||
add_fuzzed_option<double>(&options, &provider);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Sometimes allow unrecognised options.
|
||||
if (provider.ConsumeBool())
|
||||
{
|
||||
options.allow_unrecognised_options();
|
||||
}
|
||||
// Sometimes allow trailing positional arguments.
|
||||
if (provider.ConsumeBool())
|
||||
{
|
||||
std::string positional_option_name =
|
||||
provider.ConsumeRandomLengthString(kMaxArgSize);
|
||||
options.add_options()(positional_option_name,
|
||||
provider.ConsumeRandomLengthString(kMaxArgSize),
|
||||
cxxopts::value<std::vector<std::string>>());
|
||||
options.parse_positional({positional_option_name});
|
||||
}
|
||||
|
||||
// Build command line input.
|
||||
const int argc = provider.ConsumeIntegralInRange<int>(1, kMaxOptions);
|
||||
|
||||
std::vector<std::string> command_line_container;
|
||||
command_line_container.reserve(argc);
|
||||
|
||||
std::vector<const char*> argv;
|
||||
argv.reserve(argc);
|
||||
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
command_line_container.push_back(
|
||||
provider.ConsumeRandomLengthString(kMaxArgSize));
|
||||
argv.push_back(command_line_container[i].c_str());
|
||||
}
|
||||
|
||||
// Parse command line;
|
||||
auto result = options.parse(argc, argv.data());
|
||||
} catch (...)
|
||||
{
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
524
test/options.cpp
524
test/options.cpp
@ -1,5 +1,4 @@
|
||||
#include "catch.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include <initializer_list>
|
||||
|
||||
@ -9,8 +8,8 @@ class Argv {
|
||||
public:
|
||||
|
||||
Argv(std::initializer_list<const char*> args)
|
||||
: m_argv(new const char*[args.size()])
|
||||
, m_argc(static_cast<int>(args.size()))
|
||||
: m_argv(new char*[args.size()])
|
||||
, m_argc(args.size())
|
||||
{
|
||||
int i = 0;
|
||||
auto iter = args.begin();
|
||||
@ -27,7 +26,7 @@ class Argv {
|
||||
}
|
||||
}
|
||||
|
||||
const char** argv() const {
|
||||
char** argv() const {
|
||||
return m_argv.get();
|
||||
}
|
||||
|
||||
@ -37,8 +36,8 @@ class Argv {
|
||||
|
||||
private:
|
||||
|
||||
std::vector<std::unique_ptr<char[]>> m_args{};
|
||||
std::unique_ptr<const char*[]> m_argv;
|
||||
std::vector<std::unique_ptr<char[]>> m_args;
|
||||
std::unique_ptr<char*[]> m_argv;
|
||||
int m_argc;
|
||||
};
|
||||
|
||||
@ -50,14 +49,10 @@ TEST_CASE("Basic options", "[options]")
|
||||
options.add_options()
|
||||
("long", "a long option")
|
||||
("s,short", "a short option")
|
||||
("quick,brown", "An option with multiple long names and no short name")
|
||||
("f,ox,jumped", "An option with multiple long names and a short name")
|
||||
("over,z,lazy,dog", "An option with multiple long names and a short name, not listed first")
|
||||
("value", "an option with a value", cxxopts::value<std::string>())
|
||||
("a,av", "a short option with a value", cxxopts::value<std::string>())
|
||||
("6,six", "a short number option")
|
||||
("p, space", "an option with space between short and long")
|
||||
("period.delimited", "an option with a period in the long name")
|
||||
("nothing", "won't exist", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
@ -72,18 +67,9 @@ TEST_CASE("Basic options", "[options]")
|
||||
"-6",
|
||||
"-p",
|
||||
"--space",
|
||||
"--quick",
|
||||
"--ox",
|
||||
"-f",
|
||||
"--brown",
|
||||
"-z",
|
||||
"--over",
|
||||
"--dog",
|
||||
"--lazy",
|
||||
"--period.delimited",
|
||||
});
|
||||
|
||||
auto** actual_argv = argv.argv();
|
||||
char** actual_argv = argv.argv();
|
||||
auto argc = argv.argc();
|
||||
|
||||
auto result = options.parse(argc, actual_argv);
|
||||
@ -97,13 +83,9 @@ TEST_CASE("Basic options", "[options]")
|
||||
CHECK(result.count("6") == 1);
|
||||
CHECK(result.count("p") == 2);
|
||||
CHECK(result.count("space") == 2);
|
||||
CHECK(result.count("quick") == 2);
|
||||
CHECK(result.count("f") == 2);
|
||||
CHECK(result.count("z") == 4);
|
||||
CHECK(result.count("period.delimited") == 1);
|
||||
|
||||
auto& arguments = result.arguments();
|
||||
REQUIRE(arguments.size() == 16);
|
||||
REQUIRE(arguments.size() == 7);
|
||||
CHECK(arguments[0].key() == "long");
|
||||
CHECK(arguments[0].value() == "true");
|
||||
CHECK(arguments[0].as<bool>() == true);
|
||||
@ -111,10 +93,8 @@ TEST_CASE("Basic options", "[options]")
|
||||
CHECK(arguments[1].key() == "short");
|
||||
CHECK(arguments[2].key() == "value");
|
||||
CHECK(arguments[3].key() == "av");
|
||||
|
||||
CHECK_THROWS_AS(result["nothing"].as<std::string>(), cxxopts::exceptions::option_has_no_value);
|
||||
|
||||
CHECK(options.program() == "tester");
|
||||
|
||||
CHECK_THROWS_AS(result["nothing"].as<std::string>(), std::domain_error&);
|
||||
}
|
||||
|
||||
TEST_CASE("Short options", "[options]")
|
||||
@ -122,11 +102,9 @@ TEST_CASE("Short options", "[options]")
|
||||
cxxopts::Options options("test_short", " - test short options");
|
||||
|
||||
options.add_options()
|
||||
("a", "a short option", cxxopts::value<std::string>())
|
||||
("b", "b option")
|
||||
("c", "c option", cxxopts::value<std::string>());
|
||||
("a", "a short option", cxxopts::value<std::string>());
|
||||
|
||||
Argv argv({"test_short", "-a", "value", "-bcfoo=something"});
|
||||
Argv argv({"test_short", "-a", "value"});
|
||||
|
||||
auto actual_argv = argv.argv();
|
||||
auto argc = argv.argc();
|
||||
@ -136,18 +114,8 @@ TEST_CASE("Short options", "[options]")
|
||||
CHECK(result.count("a") == 1);
|
||||
CHECK(result["a"].as<std::string>() == "value");
|
||||
|
||||
auto& arguments = result.arguments();
|
||||
REQUIRE(arguments.size() == 3);
|
||||
CHECK(arguments[0].key() == "a");
|
||||
CHECK(arguments[0].value() == "value");
|
||||
|
||||
CHECK(result.count("b") == 1);
|
||||
CHECK(result.count("c") == 1);
|
||||
|
||||
CHECK(result["c"].as<std::string>() == "foo=something");
|
||||
|
||||
REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
|
||||
cxxopts::exceptions::invalid_option_format);
|
||||
cxxopts::invalid_option_format_error&);
|
||||
}
|
||||
|
||||
TEST_CASE("No positional", "[positional]")
|
||||
@ -157,7 +125,7 @@ TEST_CASE("No positional", "[positional]")
|
||||
|
||||
Argv av({"tester", "a", "b", "def"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
@ -186,7 +154,7 @@ TEST_CASE("All positional", "[positional]")
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(result.unmatched().size() == 0);
|
||||
REQUIRE(argc == 1);
|
||||
REQUIRE(positional.size() == 3);
|
||||
|
||||
CHECK(positional[0] == "a");
|
||||
@ -209,12 +177,12 @@ TEST_CASE("Some positional explicit", "[positional]")
|
||||
|
||||
Argv av({"tester", "--output", "a", "b", "c", "d"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(result.unmatched().size() == 0);
|
||||
CHECK(argc == 1);
|
||||
CHECK(result.count("output"));
|
||||
CHECK(result["input"].as<std::string>() == "b");
|
||||
CHECK(result["output"].as<std::string>() == "a");
|
||||
@ -235,13 +203,17 @@ TEST_CASE("No positional with extras", "[positional]")
|
||||
|
||||
Argv av({"extras", "--", "a", "b", "c", "d"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
auto old_argv = argv;
|
||||
auto old_argc = argc;
|
||||
|
||||
auto& unmatched = result.unmatched();
|
||||
CHECK((unmatched == std::vector<std::string>{"a", "b", "c", "d"}));
|
||||
options.parse(argc, argv);
|
||||
|
||||
REQUIRE(argc == old_argc - 1);
|
||||
CHECK(argv[0] == std::string("extras"));
|
||||
CHECK(argv[1] == std::string("a"));
|
||||
}
|
||||
|
||||
TEST_CASE("Positional not valid", "[positional]") {
|
||||
@ -254,66 +226,10 @@ TEST_CASE("Positional not valid", "[positional]") {
|
||||
|
||||
Argv av({"foobar", "bar", "baz"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option);
|
||||
}
|
||||
|
||||
TEST_CASE("Positional with empty arguments", "[positional]") {
|
||||
cxxopts::Options options("positional_with_empty_arguments", "positional with empty argument");
|
||||
options.add_options()
|
||||
("long", "a long option", cxxopts::value<std::string>())
|
||||
("program", "program to run", cxxopts::value<std::string>())
|
||||
("programArgs", "program arguments", cxxopts::value<std::vector<std::string>>())
|
||||
;
|
||||
|
||||
options.parse_positional("program", "programArgs");
|
||||
|
||||
Argv av({"foobar", "--long", "long_value", "--", "someProgram", "ab", "-c", "d", "--ef", "gh", "--ijk=lm", "n", "", "o", });
|
||||
std::vector<std::string> expected({"ab", "-c", "d", "--ef", "gh", "--ijk=lm", "n", "", "o", });
|
||||
|
||||
auto** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
auto actual = result["programArgs"].as<std::vector<std::string>>();
|
||||
|
||||
REQUIRE(result.count("program") == 1);
|
||||
REQUIRE(result["program"].as<std::string>() == "someProgram");
|
||||
REQUIRE(result.count("programArgs") == expected.size());
|
||||
REQUIRE(actual == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Positional with list delimiter", "[positional]") {
|
||||
std::string single;
|
||||
std::vector<std::string> positional;
|
||||
|
||||
cxxopts::Options options("test_all_positional_list_delimiter", " - test all positional with list delimiters");
|
||||
options.add_options()
|
||||
("single", "Single positional param",
|
||||
cxxopts::value<std::string>(single))
|
||||
("positional", "Positional parameters vector",
|
||||
cxxopts::value<std::vector<std::string>>(positional))
|
||||
;
|
||||
|
||||
Argv av({"tester", "a,b", "c,d", "e"});
|
||||
|
||||
auto argc = av.argc();
|
||||
auto argv = av.argv();
|
||||
|
||||
std::vector<std::string> pos_names = {"single", "positional"};
|
||||
|
||||
options.parse_positional(pos_names.begin(), pos_names.end());
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(result.unmatched().size() == 0);
|
||||
REQUIRE(positional.size() == 2);
|
||||
|
||||
CHECK(single == "a,b");
|
||||
CHECK(positional[0] == "c,d");
|
||||
CHECK(positional[1] == "e");
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
|
||||
}
|
||||
|
||||
TEST_CASE("Empty with implicit value", "[implicit]")
|
||||
@ -325,7 +241,7 @@ TEST_CASE("Empty with implicit value", "[implicit]")
|
||||
|
||||
Argv av({"implicit", "--implicit="});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -344,16 +260,16 @@ TEST_CASE("Boolean without implicit value", "[implicit]")
|
||||
SECTION("When no value provided") {
|
||||
Argv av({"no_implicit", "--bool"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::missing_argument);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
|
||||
}
|
||||
|
||||
SECTION("With equal-separated true") {
|
||||
Argv av({"no_implicit", "--bool=true"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -364,7 +280,7 @@ TEST_CASE("Boolean without implicit value", "[implicit]")
|
||||
SECTION("With equal-separated false") {
|
||||
Argv av({"no_implicit", "--bool=false"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -375,7 +291,7 @@ TEST_CASE("Boolean without implicit value", "[implicit]")
|
||||
SECTION("With space-separated true") {
|
||||
Argv av({"no_implicit", "--bool", "true"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -386,7 +302,7 @@ TEST_CASE("Boolean without implicit value", "[implicit]")
|
||||
SECTION("With space-separated false") {
|
||||
Argv av({"no_implicit", "--bool", "false"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -407,7 +323,7 @@ TEST_CASE("Default values", "[default]")
|
||||
SECTION("Sets defaults") {
|
||||
Argv av({"implicit"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -423,7 +339,7 @@ TEST_CASE("Default values", "[default]")
|
||||
SECTION("When values provided") {
|
||||
Argv av({"implicit", "--default", "5"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -435,12 +351,10 @@ TEST_CASE("Default values", "[default]")
|
||||
TEST_CASE("Parse into a reference", "[reference]")
|
||||
{
|
||||
int value = 0;
|
||||
bool b_value = true;
|
||||
|
||||
cxxopts::Options options("into_reference", "parses into a reference");
|
||||
options.add_options()
|
||||
("ref", "A reference", cxxopts::value(value))
|
||||
("bool", "A bool", cxxopts::value(b_value));
|
||||
("ref", "A reference", cxxopts::value(value));
|
||||
|
||||
Argv av({"into_reference", "--ref", "42"});
|
||||
|
||||
@ -450,8 +364,6 @@ TEST_CASE("Parse into a reference", "[reference]")
|
||||
auto result = options.parse(argc, argv);
|
||||
CHECK(result.count("ref") == 1);
|
||||
CHECK(value == 42);
|
||||
CHECK(result.count("bool") == 0);
|
||||
CHECK(b_value == true);
|
||||
}
|
||||
|
||||
TEST_CASE("Integers", "[options]")
|
||||
@ -462,7 +374,7 @@ TEST_CASE("Integers", "[options]")
|
||||
|
||||
Argv av({"ints", "--", "5", "6", "-6", "0", "0xab", "0xAf", "0x0"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
@ -489,7 +401,7 @@ TEST_CASE("Leading zero integers", "[options]")
|
||||
|
||||
Argv av({"ints", "--", "05", "06", "0x0ab", "0x0001"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
@ -513,11 +425,11 @@ TEST_CASE("Unsigned integers", "[options]")
|
||||
|
||||
Argv av({"ints", "--", "-2"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
|
||||
}
|
||||
|
||||
TEST_CASE("Integer bounds", "[integer]")
|
||||
@ -552,32 +464,14 @@ TEST_CASE("Overflow on boundary", "[integer]")
|
||||
using namespace cxxopts::values;
|
||||
|
||||
int8_t si;
|
||||
int16_t si16;
|
||||
int64_t si64;
|
||||
uint8_t ui;
|
||||
uint16_t ui16;
|
||||
uint64_t ui64;
|
||||
|
||||
CHECK_THROWS_AS((integer_parser("128", si)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-129", si)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("256", ui)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-0x81", si)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("0x80", si)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("0x100", ui)), cxxopts::exceptions::incorrect_argument_type);
|
||||
|
||||
CHECK_THROWS_AS((integer_parser("65536", ui16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("75536", ui16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("32768", si16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-32769", si16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-42769", si16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-75536", si16)), cxxopts::exceptions::incorrect_argument_type);
|
||||
|
||||
CHECK_THROWS_AS((integer_parser("18446744073709551616", ui64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("28446744073709551616", ui64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("9223372036854775808", si64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-9223372036854775809", si64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-10223372036854775809", si64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("-28446744073709551616", si64)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("128", si)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("-129", si)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("256", ui)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("-0x81", si)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("0x80", si)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("0x100", ui)), cxxopts::argument_incorrect_type&);
|
||||
}
|
||||
|
||||
TEST_CASE("Integer overflow", "[options]")
|
||||
@ -594,11 +488,11 @@ TEST_CASE("Integer overflow", "[options]")
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
|
||||
|
||||
int integer = 0;
|
||||
CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&);
|
||||
CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&);
|
||||
}
|
||||
|
||||
TEST_CASE("Floats", "[options]")
|
||||
@ -610,7 +504,7 @@ TEST_CASE("Floats", "[options]")
|
||||
|
||||
Argv av({"floats", "--double", "0.5", "--", "4", "-4", "1.5e6", "-1.5e6"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
@ -635,11 +529,11 @@ TEST_CASE("Invalid integers", "[integer]") {
|
||||
|
||||
Argv av({"ints", "--", "Ae"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char **argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse_positional("positional");
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::incorrect_argument_type);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
|
||||
}
|
||||
|
||||
TEST_CASE("Booleans", "[boolean]") {
|
||||
@ -660,7 +554,7 @@ TEST_CASE("Booleans", "[boolean]") {
|
||||
|
||||
Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "--verbose=1", "--dry-run=0", "extra"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
@ -694,7 +588,7 @@ TEST_CASE("std::vector", "[vector]") {
|
||||
|
||||
Argv av({"vector", "--vector", "1,-2.1,3,4.5"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse(argc, argv);
|
||||
@ -709,23 +603,19 @@ TEST_CASE("std::vector", "[vector]") {
|
||||
#ifdef CXXOPTS_HAS_OPTIONAL
|
||||
TEST_CASE("std::optional", "[optional]") {
|
||||
std::optional<std::string> optional;
|
||||
std::optional<bool> opt_bool;
|
||||
cxxopts::Options options("optional", " - tests optional");
|
||||
options.add_options()
|
||||
("optional", "an optional option", cxxopts::value<std::optional<std::string>>(optional))
|
||||
("optional_bool", "an boolean optional", cxxopts::value<std::optional<bool>>(opt_bool)->default_value("false"));
|
||||
("optional", "an optional option", cxxopts::value<std::optional<std::string>>(optional));
|
||||
|
||||
Argv av({"optional", "--optional", "foo", "--optional_bool", "true"});
|
||||
Argv av({"optional", "--optional", "foo"});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
options.parse(argc, argv);
|
||||
|
||||
REQUIRE(optional.has_value());
|
||||
CHECK(*optional == "foo");
|
||||
CHECK(opt_bool.has_value());
|
||||
CHECK(*opt_bool);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -742,21 +632,20 @@ TEST_CASE("Unrecognised options", "[options]") {
|
||||
"--long",
|
||||
"-su",
|
||||
"--another_unknown",
|
||||
"-a",
|
||||
});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
SECTION("Default behaviour") {
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
|
||||
}
|
||||
|
||||
SECTION("After allowing unrecognised options") {
|
||||
options.allow_unrecognised_options();
|
||||
auto result = options.parse(argc, argv);
|
||||
auto& unmatched = result.unmatched();
|
||||
CHECK((unmatched == std::vector<std::string>{"--unknown", "-u", "--another_unknown", "-a"}));
|
||||
CHECK_NOTHROW(options.parse(argc, argv));
|
||||
REQUIRE(argc == 3);
|
||||
CHECK_THAT(argv[1], Catch::Equals("--unknown"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -768,22 +657,22 @@ TEST_CASE("Allow bad short syntax", "[options]") {
|
||||
("s,short", "a short option");
|
||||
|
||||
Argv av({
|
||||
"--ab?",
|
||||
"-?b?#@"
|
||||
"unknown_options",
|
||||
"-some_bad_short",
|
||||
});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
SECTION("Default behaviour") {
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::invalid_option_syntax);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
|
||||
}
|
||||
|
||||
SECTION("After allowing unrecognised options") {
|
||||
options.allow_unrecognised_options();
|
||||
CHECK_NOTHROW(options.parse(argc, argv));
|
||||
REQUIRE(argc == 2);
|
||||
CHECK_THAT(argv[1], Catch::Equals("-?b?#@"));
|
||||
CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -795,285 +684,10 @@ TEST_CASE("Invalid option syntax", "[options]") {
|
||||
"--a",
|
||||
});
|
||||
|
||||
auto** argv = av.argv();
|
||||
char** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
SECTION("Default behaviour") {
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::invalid_option_syntax);
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Options empty", "[options]") {
|
||||
cxxopts::Options options("Options list empty", " - test empty option list");
|
||||
options.add_options();
|
||||
options.add_options("");
|
||||
options.add_options("", {});
|
||||
options.add_options("test");
|
||||
|
||||
Argv argv_({
|
||||
"test",
|
||||
"--unknown"
|
||||
});
|
||||
auto argc = argv_.argc();
|
||||
auto** argv = argv_.argv();
|
||||
|
||||
CHECK(options.groups().empty());
|
||||
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::exceptions::no_such_option);
|
||||
}
|
||||
|
||||
#ifdef CXXOPTS_HAS_OPTIONAL
|
||||
TEST_CASE("Optional value", "[optional]")
|
||||
{
|
||||
cxxopts::Options options("options", "query as std::optional");
|
||||
options.add_options()
|
||||
("int", "Integer", cxxopts::value<int>())
|
||||
("float", "Float", cxxopts::value<float>())
|
||||
("string", "String", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
SECTION("Available") {
|
||||
Argv av({
|
||||
"--int",
|
||||
"42",
|
||||
"--float",
|
||||
"3.141",
|
||||
"--string",
|
||||
"Hello"
|
||||
});
|
||||
|
||||
auto** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(result.as_optional<int>("int"));
|
||||
CHECK(result.as_optional<float>("float"));
|
||||
CHECK(result.as_optional<string>("string"));
|
||||
|
||||
CHECK(*result.as_optional<int>("int") == 42);
|
||||
CHECK(*result.as_optional<float>("float") == 3.141);
|
||||
CHECK(*result.as_optional<string>("string") == "Hello");
|
||||
}
|
||||
|
||||
SECTION("Unavailable") {
|
||||
Argv av({
|
||||
});
|
||||
|
||||
auto** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(!result.as_optional<int>("int"));
|
||||
CHECK(!result.as_optional<float>("float"));
|
||||
CHECK(!result.as_optional<string>("string"));
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("Initializer list with group", "[options]") {
|
||||
cxxopts::Options options("Initializer list group", " - test initializer list with group");
|
||||
|
||||
options.add_options("", {
|
||||
{"a, address", "server address", cxxopts::value<std::string>()->default_value("127.0.0.1")},
|
||||
{"p, port", "server port", cxxopts::value<std::string>()->default_value("7110"), "PORT"},
|
||||
});
|
||||
|
||||
cxxopts::Option help{"h,help", "Help"};
|
||||
|
||||
options.add_options("TEST_GROUP", {
|
||||
{"t, test", "test option"},
|
||||
help
|
||||
});
|
||||
|
||||
Argv argv({
|
||||
"test",
|
||||
"--address",
|
||||
"10.0.0.1",
|
||||
"-p",
|
||||
"8000",
|
||||
"-t",
|
||||
});
|
||||
auto** actual_argv = argv.argv();
|
||||
auto argc = argv.argc();
|
||||
auto result = options.parse(argc, actual_argv);
|
||||
|
||||
CHECK(options.groups().size() == 2);
|
||||
CHECK(result.count("address") == 1);
|
||||
CHECK(result.count("port") == 1);
|
||||
CHECK(result.count("test") == 1);
|
||||
CHECK(result.count("help") == 0);
|
||||
CHECK(result["address"].as<std::string>() == "10.0.0.1");
|
||||
CHECK(result["port"].as<std::string>() == "8000");
|
||||
CHECK(result["test"].as<bool>() == true);
|
||||
}
|
||||
|
||||
TEST_CASE("Option add with add_option(string, Option)", "[options]") {
|
||||
cxxopts::Options options("Option add with add_option", " - test Option add with add_option(string, Option)");
|
||||
|
||||
cxxopts::Option option_1("t,test", "test option", cxxopts::value<int>()->default_value("7"), "TEST");
|
||||
|
||||
options.add_option("", option_1);
|
||||
options.add_option("TEST", {"a,aggregate", "test option 2", cxxopts::value<int>(), "AGGREGATE"});
|
||||
options.add_option("TEST", {"multilong,m,multilong-alias", "test option 3", cxxopts::value<int>(), "An option with multiple long names"});
|
||||
|
||||
Argv argv_({
|
||||
"test",
|
||||
"--test",
|
||||
"5",
|
||||
"-a",
|
||||
"4",
|
||||
"--multilong-alias",
|
||||
"6"
|
||||
});
|
||||
auto argc = argv_.argc();
|
||||
auto** argv = argv_.argv();
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
CHECK(result.arguments().size() == 3);
|
||||
CHECK(options.groups().size() == 2);
|
||||
CHECK(result.count("address") == 0);
|
||||
CHECK(result.count("aggregate") == 1);
|
||||
CHECK(result.count("test") == 1);
|
||||
CHECK(result["aggregate"].as<int>() == 4);
|
||||
CHECK(result["multilong"].as<int>() == 6);
|
||||
CHECK(result["multilong-alias"].as<int>() == 6);
|
||||
CHECK(result["m"].as<int>() == 6);
|
||||
CHECK(result["test"].as<int>() == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Const array", "[const]") {
|
||||
const char* const option_list[] = {"empty", "options"};
|
||||
cxxopts::Options options("Empty options", " - test constness");
|
||||
auto result = options.parse(2, option_list);
|
||||
}
|
||||
|
||||
TEST_CASE("Parameter follow option", "[parameter]") {
|
||||
cxxopts::Options options("param_follow_opt", " - test parameter follow option without space.");
|
||||
options.add_options()
|
||||
("j,job", "Job", cxxopts::value<std::vector<unsigned>>());
|
||||
Argv av({"implicit",
|
||||
"-j", "9",
|
||||
"--job", "7",
|
||||
"--job=10",
|
||||
"-j5",
|
||||
});
|
||||
|
||||
auto ** argv = av.argv();
|
||||
auto argc = av.argc();
|
||||
|
||||
auto result = options.parse(argc, argv);
|
||||
|
||||
REQUIRE(result.count("job") == 4);
|
||||
|
||||
auto job_values = result["job"].as<std::vector<unsigned>>();
|
||||
CHECK(job_values[0] == 9);
|
||||
CHECK(job_values[1] == 7);
|
||||
CHECK(job_values[2] == 10);
|
||||
CHECK(job_values[3] == 5);
|
||||
}
|
||||
|
||||
TEST_CASE("Iterator", "[iterator]") {
|
||||
cxxopts::Options options("tester", " - test iterating over parse result");
|
||||
|
||||
options.add_options()
|
||||
("long", "a long option")
|
||||
("s,short", "a short option")
|
||||
("a", "a short-only option")
|
||||
("value", "an option with a value", cxxopts::value<std::string>())
|
||||
("default", "an option with default value", cxxopts::value<int>()->default_value("42"))
|
||||
("nothing", "won't exist", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
Argv argv({
|
||||
"tester",
|
||||
"--long",
|
||||
"-s",
|
||||
"-a",
|
||||
"--value",
|
||||
"value",
|
||||
});
|
||||
|
||||
auto** actual_argv = argv.argv();
|
||||
auto argc = argv.argc();
|
||||
|
||||
auto result = options.parse(argc, actual_argv);
|
||||
|
||||
auto iter = result.begin();
|
||||
|
||||
REQUIRE(iter != result.end());
|
||||
CHECK(iter->key() == "long");
|
||||
CHECK(iter->value() == "true");
|
||||
|
||||
REQUIRE(++iter != result.end());
|
||||
CHECK(iter->key() == "short");
|
||||
CHECK(iter->value() == "true");
|
||||
|
||||
REQUIRE(++iter != result.end());
|
||||
CHECK(iter->key() == "a");
|
||||
CHECK(iter->value() == "true");
|
||||
|
||||
REQUIRE(++iter != result.end());
|
||||
CHECK(iter->key() == "value");
|
||||
CHECK(iter->value() == "value");
|
||||
|
||||
REQUIRE(++iter != result.end());
|
||||
CHECK(iter->key() == "default");
|
||||
CHECK(iter->value() == "42");
|
||||
|
||||
REQUIRE(++iter == result.end());
|
||||
}
|
||||
|
||||
TEST_CASE("Iterator no args", "[iterator]") {
|
||||
cxxopts::Options options("tester", " - test iterating over parse result");
|
||||
|
||||
options.add_options()
|
||||
("value", "an option with a value", cxxopts::value<std::string>())
|
||||
("default", "an option with default value", cxxopts::value<int>()->default_value("42"))
|
||||
("nothing", "won't exist", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
Argv argv({
|
||||
"tester",
|
||||
});
|
||||
|
||||
auto** actual_argv = argv.argv();
|
||||
auto argc = argv.argc();
|
||||
|
||||
auto result = options.parse(argc, actual_argv);
|
||||
|
||||
auto iter = result.begin();
|
||||
|
||||
REQUIRE(iter != result.end());
|
||||
CHECK(iter->key() == "default");
|
||||
CHECK(iter->value() == "42");
|
||||
|
||||
++iter;
|
||||
CHECK(iter == result.end());
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("No Options help", "[options]")
|
||||
{
|
||||
std::vector<std::string> positional;
|
||||
|
||||
cxxopts::Options options("test", "test no options help");
|
||||
|
||||
// explicitly setting custom help empty to overwrite
|
||||
// default "[OPTION...]" when there are no options
|
||||
options.positional_help("<posArg1>...<posArgN>")
|
||||
.custom_help("")
|
||||
.add_options()
|
||||
("positional", "", cxxopts::value<std::vector<std::string>>(positional));
|
||||
|
||||
Argv av({"test", "posArg1", "posArg2", "posArg3"});
|
||||
|
||||
auto argc = av.argc();
|
||||
auto** argv = av.argv();
|
||||
|
||||
options.parse_positional({"positional"});
|
||||
|
||||
CHECK_NOTHROW(options.parse(argc, argv));
|
||||
CHECK(options.help().find("test <posArg1>...<posArgN>") != std::string::npos);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user