Skip to content

Commit 825ade8

Browse files
committed
Initial commit
0 parents  commit 825ade8

8 files changed

Lines changed: 1398 additions & 0 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto eol=lf
2+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Build and Test
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches:
6+
- "**"
7+
8+
jobs:
9+
build-and-test:
10+
name: Build and Test
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v6
16+
17+
- name: Configure CMake project
18+
run: cmake -S . -B build
19+
20+
- name: Build project
21+
run: cmake --build build --config Release -- -j$(nproc)
22+
23+
- name: Run tests
24+
run: ctest --test-dir build --output-on-failure

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
# macOS files
35+
**/.DS_Store
36+
37+
# Cache files for Sublime Text
38+
*.tmlanguage.cache
39+
*.tmPreferences.cache
40+
*.stTheme.cache
41+
42+
# Ignore build folders
43+
**/build
44+
**/cmake-build-*/
45+
build-*/
46+
47+
# Workspace files are user-specific
48+
*.sublime-workspace
49+
50+
# IDE files
51+
**/.vscode
52+
.idea/
53+
.vs/
54+
CMakeSettings.json
55+
56+
# clangd
57+
.cache/

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
project(slic)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
8+
add_library(${PROJECT_NAME} INTERFACE)
9+
target_include_directories(${PROJECT_NAME} INTERFACE include/)
10+
11+
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
12+
include(FetchContent)
13+
FetchContent_Declare(
14+
googletest
15+
GIT_REPOSITORY https://github.com/google/googletest.git
16+
GIT_TAG v1.14.0
17+
)
18+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
19+
FetchContent_MakeAvailable(googletest)
20+
21+
enable_testing()
22+
23+
add_executable(${PROJECT_NAME}_test tests/tests.cpp)
24+
target_link_libraries(${PROJECT_NAME}_test PRIVATE ${PROJECT_NAME} GTest::gtest_main)
25+
26+
include(GoogleTest)
27+
gtest_discover_tests(${PROJECT_NAME}_test)
28+
endif()

LICENSE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The MIT License (MIT)
2+
3+
Copyright © 2026 Prevter
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6+
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
7+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8+
persons to whom the Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11+
Software.
12+
13+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# slic
2+
3+
Simple C++20 command line argument parser.
4+
5+
## Features
6+
7+
- Single header file
8+
- Zero allocations
9+
- No dependencies
10+
- Compile-time configuration and validation
11+
- Supports positional and named arguments
12+
- Variadic arguments support
13+
- Type-safe parsing of arguments
14+
- Help message generation
15+
- Error handling
16+
17+
## Usage
18+
19+
Here's a simple example of how to use `slic` to parse command line arguments:
20+
21+
```cpp
22+
#include <slic.hpp>
23+
24+
// Define your argument struct
25+
struct MyArgs {
26+
std::string_view input;
27+
std::string_view output;
28+
std::string_view test;
29+
int number = 0;
30+
bool version = false;
31+
slic::ArgSpan args;
32+
33+
static constexpr std::string_view Description = "My awesome CLI tool";
34+
static constexpr std::tuple Options = {
35+
slic::Option{"--number", "-n", &MyArgs::number, "Some number"},
36+
slic::Option{"--version", "-v", &MyArgs::version, "Show version"},
37+
slic::Option{"--test", "-t", &MyArgs::test, "Test argument"},
38+
slic::Arg{"INPUT", &MyArgs::input, "Input file"},
39+
slic::Arg{"OUTPUT", &MyArgs::output, "Output file"},
40+
slic::VarArgs{"Extra files", &MyArgs::args}
41+
};
42+
};
43+
44+
int main(int argc, char** argv) {
45+
// Create the parser
46+
slic::ArgParser<MyArgs> parser(argc, argv);
47+
48+
// Parse the arguments and handle errors
49+
auto result = parser.parse();
50+
if (!result) {
51+
result.print();
52+
// you can also print help message if needed
53+
parser.printHelp();
54+
return 1;
55+
}
56+
57+
MyArgs const& args = parser.result(); // parser holds your struct
58+
59+
// access members directly
60+
if (args.version) {
61+
std::cout << "My CLI Tool version 1.0.0\n";
62+
return 0;
63+
}
64+
65+
// access variadic arguments
66+
for (std::string_view arg : args.args) {
67+
std::cout << "Extra arg: " << arg << "\n";
68+
}
69+
70+
return 0;
71+
}
72+
```
73+
74+
Help message output for the above example:
75+
76+
```
77+
My awesome CLI tool
78+
79+
Usage: my_program [OPTIONS] <INPUT> <OUTPUT> [...]
80+
81+
Arguments:
82+
INPUT: Input file
83+
OUTPUT: Output file
84+
[...]: Extra files
85+
86+
Options:
87+
-n, --number <value>: Some number
88+
-v, --version: Show version
89+
-t, --test <value>: Test argument
90+
```
91+
92+
## License
93+
94+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)