Skip to content

mmmosca/c-cmd-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c-cmd-api

C API for Windows to handle command line parameters with short and long options

Installation (Windows)

  1. Install Visual Studio (e.g. Community) choosing the following single components:

    • MSVC v142+ - C++ Build tools
    • C++/CLI for Build Tools v142+
    • Windows 11 SDK
    • CMake C++ Tools for Windows
  2. Download the repository

  3. Add the following environmental variable as User:

    • VS_DIR: Path to Visual Studio with all folders (e.g. Microsoft Visual Studio\2022\Community)
  4. Run the following in a Command Prompt (no PowerShell) to install it:

    "%VS_DIR%\VC\Auxiliary\Build\vcvarsall.bat" x64_x86
    cd c-cmd-api
    cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder>
    cmake --build build --target install --config Release
    

    Note: Generate shared libraries (.lib + .dll) instead as follows:

    cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder> -DSHARED=ON
    
  5. When using the library in your project make sure to:

    • Include <install_folder>/include
    • Link the library <install_folder>/lib/cmd-api.lib

Load cmd-api as dependency in your cmake file

In your CMakeLists.txt file

...
find_package(cmd-api PATHS "<install_folder>" REQUIRED)
include_directories(${cmd-api_INCLUDE_DIR})
add_executable(${PROJECT_NAME} yourmain.cpp)
target_link_libraries(${PROJECT_NAME} "${cmd-api_LIBRARIES}")
install(FILES ${cmd-api_RUNTIME} DESTINATION bin)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION lib)
...

Usage example

#include "cmd-api.h"
int main(int argc, char* argv[]) {
    optarg_t* optarg;
    while ((optarg = getoptW(argc, argv, "o:|opt1:|opt2:|")) != NULL)
    {
        if (strcmp(optarg->opt, "o") == 0) {
            /*use optarg->arg that has the argument of o*/
            continue;
        }
        if (strcmp(optarg->opt, "opt1") == 0) {
            /*use optarg->arg that has the argument of opt1*/
            continue;
        }
        if (strcmp(optarg->opt, "opt2") == 0) {
            /*use optarg->arg that has the argument of opt2*/
            continue;
        }
        free(optarg);
    }
    ...
}

Tests (only on static build)

cmake -B build -S . -L -DCMAKE_INSTALL_PREFIX=<install_folder>
cmake --build .\build --target install --config Release
cd build
ctest --build-config Release --build-target install

About

C API for Windows to handle command line parameters with short and long options

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published