├── .github ├── FUNDING.yml └── workflows │ ├── ci.yml │ ├── codeql.yml │ └── win_ci.yml.disabled ├── .gitignore ├── CHANGELOG.md ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── cli.pc.in ├── cliConfig.cmake.in ├── doc ├── doxy │ └── Doxyfile └── uml │ ├── cli.uml │ ├── cmdhandlers.png │ └── implementation.png ├── examples ├── CMakeLists.txt ├── Makefile.boostasio ├── Makefile.noasio ├── Makefile.standaloneasio ├── asyncsession.cpp ├── boostasio_nonowner_iocontext.cpp ├── boostasio_nonowner_iocontext_vsprj │ └── boostasio_nonowner_iocontext.vcxproj ├── complete.cpp ├── complete_vsprj │ ├── common.props │ └── complete.vcxproj ├── examples.sln ├── filesession.cpp ├── filesession_vsprj │ └── filesession.vcxproj ├── input.txt ├── makefile.boostasio.win ├── makefile.noasio.win ├── makefile.standaloneasio.win ├── pluginmanager.cpp ├── pluginmanager_vsprj │ └── pluginmanager.vcxproj ├── simplelocalsession.cpp ├── simplelocalsession_vsprj │ └── simplelocalsession.vcxproj ├── standaloneasio_nonowner_iocontext.cpp └── standaloneasio_nonowner_iocontext_vsprj │ └── standaloneasio_nonowner_iocontext.vcxproj ├── include └── cli │ ├── boostasiocliasyncsession.h │ ├── boostasioremotecli.h │ ├── boostasioscheduler.h │ ├── cli.h │ ├── clifilesession.h │ ├── clilocalsession.h │ ├── colorprofile.h │ ├── detail │ ├── boostasiolib.h │ ├── commandprocessor.h │ ├── commonprefix.h │ ├── fromstring.h │ ├── genericasioremotecli.h │ ├── genericasioscheduler.h │ ├── genericcliasyncsession.h │ ├── history.h │ ├── inputdevice.h │ ├── keyboard.h │ ├── linuxkeyboard.h │ ├── newboostasiolib.h │ ├── newstandaloneasiolib.h │ ├── oldboostasiolib.h │ ├── oldstandaloneasiolib.h │ ├── platform.h │ ├── rang.h │ ├── screen.h │ ├── server.h │ ├── split.h │ ├── standaloneasiolib.h │ ├── telnetscreen.h │ ├── terminal.h │ ├── winkeyboard.h │ └── winscreen.h │ ├── filehistorystorage.h │ ├── historystorage.h │ ├── loopscheduler.h │ ├── scheduler.h │ ├── standaloneasiocliasyncsession.h │ ├── standaloneasioremotecli.h │ ├── standaloneasioscheduler.h │ └── volatilehistorystorage.h └── test ├── CMakeLists.txt ├── Makefile ├── cli_test_history ├── driver.cpp ├── makefile.win ├── scheduler_test_templates.h ├── test_boostasioscheduler.cpp ├── test_cli.cpp ├── test_commonprefix.cpp ├── test_filehistorystorage.cpp ├── test_history.cpp ├── test_loopscheduler.cpp ├── test_menu.cpp ├── test_split.cpp ├── test_standaloneasioscheduler.cpp └── test_volatilehistorystorage.cpp /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: daniele77 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push,pull_request] 4 | 5 | jobs: 6 | Test: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: false 10 | 11 | matrix: 12 | os: [ubuntu-22.04, macos-13, windows-2022, windows-2019] 13 | compiler: [llvm-17.0.2, clang++-15, gcc-11, msvc] 14 | standard: [14, 17, 20, 23] 15 | build_type: [Release, Debug] 16 | exclude: 17 | # Exclude msvc on non-Windows platforms 18 | - os: ubuntu-22.04 19 | compiler: msvc 20 | - os: ubuntu-latest 21 | compiler: msvc 22 | - os: macos-13 23 | compiler: msvc 24 | # Exclude gcc on Windows platforms 25 | - os: windows-2022 26 | compiler: gcc-11 27 | - os: windows-2019 28 | compiler: gcc-11 29 | # Exclude gcc on mac platforms 30 | - os: macos-13 31 | compiler: gcc-11 32 | # Exclude llvm on Windows platforms 33 | - os: windows-2022 34 | compiler: llvm-17.0.2 35 | - os: windows-2019 36 | compiler: llvm-17.0.2 37 | 38 | steps: 39 | - uses: actions/checkout@v4 40 | 41 | # - name: Set up MSVC 42 | # if: runner.os == 'Windows' 43 | # uses: ilammy/msvc-dev-cmd@v1 44 | 45 | - name: Setup Cpp 46 | uses: aminya/setup-cpp@v1 47 | with: 48 | compiler: ${{ matrix.compiler }} 49 | vcvarsall: ${{ contains(matrix.os, 'windows' )}} 50 | 51 | cmake: true 52 | ninja: true 53 | vcpkg: false 54 | ccache: true 55 | clangtidy: true 56 | 57 | cppcheck: true 58 | 59 | gcovr: true 60 | opencppcoverage: true 61 | 62 | - name: setup dependencies - Unix 63 | if: runner.os == 'Linux' 64 | run: | 65 | sudo apt-get -y update 66 | sudo apt-get -y install -y ninja-build libboost-all-dev libasio-dev 67 | - name: setup dependencies - macOS 68 | if: runner.os == 'macOS' 69 | run: | 70 | brew install ninja boost asio 71 | - name: setup dependencies - Windows 72 | if: runner.os == 'Windows' 73 | run: | 74 | choco install ninja 75 | choco install boost-msvc-14.1 76 | # # Download and extract Asio 77 | curl -L -o asio.zip https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-18-2.zip 78 | tar -xf asio.zip 79 | move asio-asio-1-18-2 asio 80 | 81 | - name: Configure CMake - Unix 82 | if: runner.os != 'Windows' 83 | run: | 84 | cmake -S . -B ./build -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DCMAKE_CXX_STANDARD=${{matrix.standard}} -DCLI_BuildTests=ON -DCLI_BuildExamples=ON -DCLI_UseBoostAsio=ON 85 | 86 | - name: Configure CMake - Windows 87 | # windows need asio library path 88 | if: runner.os == 'Windows' 89 | run: | 90 | cmake -S . -B ./build -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DCMAKE_CXX_STANDARD=${{matrix.standard}} -DCLI_BuildTests=ON -DCLI_BuildExamples=ON -DCLI_UseBoostAsio=ON -DASIO_INCLUDEDIR=${{ github.workspace }}/asio/asio/include 91 | 92 | - name: Build 93 | run: | 94 | # cd /home/runner/work/cli/cli/build 95 | # make all 96 | cmake --build ./build --config ${{matrix.build_type}} 97 | 98 | - name: run tests 99 | # on windows test_suite throws an exception, but only on the CI environment! 100 | if: runner.os != 'Windows' 101 | # working-directory: ./build/test/${{matrix.build_type}} 102 | working-directory: ./build 103 | run: | 104 | # cd /home/runner/work/cli/cli/build/test/ 105 | # ./test_suite 106 | ctest -C ${{matrix.build_type}} --output-on-failure 107 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ "master" ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ "master" ] 20 | schedule: 21 | - cron: '20 0 * * 6' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | permissions: 28 | actions: read 29 | contents: read 30 | security-events: write 31 | 32 | strategy: 33 | fail-fast: false 34 | matrix: 35 | language: [ 'cpp' ] 36 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 37 | # Use only 'java' to analyze code written in Java, Kotlin or both 38 | # Use only 'javascript' to analyze code written in JavaScript, TypeScript or both 39 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 40 | 41 | steps: 42 | - name: Checkout repository 43 | uses: actions/checkout@v3 44 | 45 | # Initializes the CodeQL tools for scanning. 46 | - name: Initialize CodeQL 47 | uses: github/codeql-action/init@v2 48 | with: 49 | languages: ${{ matrix.language }} 50 | # If you wish to specify custom queries, you can do so here or in a config file. 51 | # By default, queries listed here will override any specified in a config file. 52 | # Prefix the list here with "+" to use these queries and those in the config file. 53 | 54 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 55 | # queries: security-extended,security-and-quality 56 | 57 | 58 | # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). 59 | # If this step fails, then you should remove it and run the build manually (see below) 60 | #- name: Autobuild 61 | # uses: github/codeql-action/autobuild@v2 62 | 63 | # ℹ️ Command-line programs to run using the OS shell. 64 | # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun 65 | 66 | # If the Autobuild fails above, remove it and uncomment the following three lines. 67 | # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. 68 | 69 | - name: Build 70 | run: | 71 | mkdir build 72 | cd build 73 | cmake .. -DCLI_BuildTests=OFF -DCLI_BuildExamples=ON 74 | make all 75 | 76 | - name: Perform CodeQL Analysis 77 | uses: github/codeql-action/analyze@v2 78 | with: 79 | category: "/language:${{matrix.language}}" 80 | -------------------------------------------------------------------------------- /.github/workflows/win_ci.yml.disabled: -------------------------------------------------------------------------------- 1 | name: Windows CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | Test: 7 | # runs-on: windows-latest 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | 12 | matrix: 13 | os: 14 | - windows-2022 15 | - windows-2019 16 | build_type: 17 | - Release 18 | - Debug 19 | standard: [14, 17, 20, 23] 20 | 21 | steps: 22 | - name: Checkout repository 23 | uses: actions/checkout@v3 24 | 25 | - name: Set up MSVC 26 | uses: ilammy/msvc-dev-cmd@v1 27 | 28 | - name: Install Boost 29 | run: choco install boost-msvc-14.1 30 | 31 | - name: Install ASIO standalone 32 | run: | 33 | curl -L -o asio.zip https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-18-2.zip 34 | tar -xf asio.zip 35 | move asio-asio-1-18-2 asio 36 | 37 | - name: Configure CMake 38 | run: cmake -S . -B build -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DASIO_INCLUDEDIR=${{ github.workspace }}/asio/asio/include -DCLI_BuildTests=ON -DCLI_BuildExamples=ON -DCLI_UseBoostAsio=ON -DCMAKE_CXX_STANDARD=${{matrix.standard}} 39 | 40 | - name: Build 41 | run: cmake --build build --config ${{matrix.build_type}} 42 | 43 | # - name: Run tests 44 | # working-directory: build/test/${{matrix.build_type}} 45 | # run: ./test_suite -l all 46 | # working-directory: build 47 | # run: ctest -C ${{matrix.build_type}} --output-on-failure --debug 48 | 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Cmake Files 8 | CMakeLists.txt.user 9 | CMakeCache.txt 10 | CMakeSettings.json 11 | CMakeFiles 12 | CMakeScripts 13 | Testing 14 | cmake_install.cmake 15 | install_manifest.txt 16 | compile_commands.json 17 | CTestTestfile.cmake 18 | _deps 19 | build 20 | cmake-build-* 21 | 22 | # Precompiled Headers 23 | *.gch 24 | *.pch 25 | 26 | # Compiled Dynamic libraries 27 | *.so 28 | *.dylib 29 | *.dll 30 | 31 | # Fortran module files 32 | *.mod 33 | *.smod 34 | 35 | # Compiled Static libraries 36 | *.lai 37 | *.la 38 | *.a 39 | *.lib 40 | 41 | # Executables 42 | *.exe 43 | *.out 44 | *.app 45 | Debug 46 | Release 47 | test/test_suite 48 | asyncsession 49 | complete 50 | filesession 51 | pluginmanager 52 | simplelocalsession 53 | boostasio_nonowner_iocontext 54 | standaloneasio_nonowner_iocontext 55 | 56 | # Project files 57 | .cproject 58 | .project 59 | .vscode/ 60 | .vs 61 | *.opensdf 62 | *.vcxproj.filters 63 | *.sdf 64 | *.user 65 | *.db 66 | .svn 67 | 68 | # Doxygen 69 | doc/doxy/html 70 | 71 | # Test files 72 | cli_test_history 73 | 74 | # Other 75 | *.sh -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 5 | 6 | ## Unreleased 7 | 8 | - Fix mutex initialization issue in cli library (pr [#249](https://github.com/daniele77/cli/pull/249)) 9 | 10 | ## [2.2.0] - 2024-10-25 11 | 12 | - Add clear screen command using ctrl + L (issue [#229](https://github.com/daniele77/cli/issues/229)) 13 | - Add history view and execute an history command with bang + id (issue [#235](https://github.com/daniele77/cli/issues/235)) 14 | - Custom handler for wrong commands (issue [#223](https://github.com/daniele77/cli/issues/223)) 15 | - Parent menus does not chain (issue [#234](https://github.com/daniele77/cli/issues/234)) 16 | - Parent menu shortcut (issue [#233](https://github.com/daniele77/cli/issues/233)) 17 | - Integer overflow warning detected by clang++ (issue [#236](https://github.com/daniele77/cli/issues/236)) 18 | - Enable Keyboard Handling in Command Handlers on Linux Platform (issue [#239](https://github.com/daniele77/cli/issues/239)) 19 | - Fix issue with remote session on ARM platforms (issue [#220](https://github.com/daniele77/cli/issues/220)) 20 | 21 | ## [2.1.0] - 2023-06-29 22 | 23 | - Nest namespace rang (issue [#167](https://github.com/daniele77/cli/issues/167)) 24 | - Add ascii value 8 for backspace (issue [#124](https://github.com/daniele77/cli/issues/124)) 25 | - Add check for CMAKE_SKIP_INSTALL_RULES (issue [#160](https://github.com/daniele77/cli/issues/160)) 26 | - Add enter action (issue [#177](https://github.com/daniele77/cli/issues/177) - PR [#180](https://github.com/daniele77/cli/pull/177)) 27 | - Fix missing echo after paste of command (issue [#185](https://github.com/daniele77/cli/issues/185)) 28 | - Fix asio::io_context::work has no member named reset in old asio lib (issue [#188](https://github.com/daniele77/cli/issues/188)) 29 | - Shown in the `complete` and `pluginmanager` examples that the issue #96 was already fixed (issue [#96](https://github.com/daniele77/cli/issues/96)) 30 | - Allow custom prompt for menu (issue [#101](https://github.com/daniele77/cli/issues/101)) 31 | 32 | ## [2.0.2] - 2022-08-18 33 | 34 | - Specify signed for char parameters (issue [#149](https://github.com/daniele77/cli/issues/149)) 35 | - CliSession can call OutStream::UnRegister() when it's already destroyed (issue [#153](https://github.com/daniele77/cli/issues/153)) 36 | 37 | ## [2.0.1] - 2022-04-19 38 | 39 | - Add a non-blocking exec method to schedulers (issue [#127](https://github.com/daniele77/cli/issues/127)) 40 | - Add a Menu::Insert method working with free functions as handler 41 | - Cli::cout() returns a class derived from std::ostream 42 | - Fix address sanitizer issue with GenericAsioScheduler dtor 43 | - Fix teardown problem with linux (issue [#132](https://github.com/daniele77/cli/issues/132)) 44 | - Fix teardown problem with windows 45 | - The prompt is no more shown after exit 46 | - Telnet server now works on MobaXTerm 47 | 48 | ## [2.0.0] - 2021-08-25 49 | 50 | - Remove boost dependency for local only sessions (issue [#83](https://github.com/daniele77/cli/issues/83)) 51 | - Now you can use standalone asio library instead of boost asio for remote sessions (issue [#41](https://github.com/daniele77/cli/issues/41)) 52 | - Fix missing echo after ctrl-v paste of command (issue [#72](https://github.com/daniele77/cli/issues/72)) 53 | - Remove the symbol BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT (issue [#89](https://github.com/daniele77/cli/issues/89)) 54 | - Fix unused parameters warning in release mode (issue [#90](https://github.com/daniele77/cli/issues/90)) 55 | - Cli constructor revision (issue [#75](https://github.com/daniele77/cli/issues/75)) 56 | - Special keys not recognized behaves like backspace in windows (issue [#111](https://github.com/daniele77/cli/issues/111)) 57 | - Fix cmake install dependencies (issue [#117](https://github.com/daniele77/cli/issues/117)) 58 | 59 | ## [1.2.1] - 2020-08-27 60 | 61 | - With Boost >= 1.74 use TS executor by default (issue [#79](https://github.com/daniele77/cli/issues/79)) 62 | - Standard and custom exception handler for cli commands (issue [#74](https://github.com/daniele77/cli/issues/74)) 63 | 64 | ## [1.2.0] - 2020-06-27 65 | 66 | - History persistence (issue [#39](https://github.com/daniele77/cli/issues/39)) 67 | - Escaping and sentence support with quote and double quote 68 | - Remove boost dependency from cli.h: now, if you just use CliFileSession you don't need boost anymore 69 | - Support commands with variable number of arguments through `vector` 70 | - Handle CTRL-D (EOF) on linux 71 | - Handle CTRL-D, CTRL-Z and CTRL-C on windows 72 | - Consecutive identical commands are not stored in the history (issue [#55](https://github.com/daniele77/cli/issues/55)) 73 | - Change message in case of wrong command 74 | - Fix Backspace from remote terminal (issue [#52](https://github.com/daniele77/cli/issues/52)) 75 | - Fix duplicate autocompletion (issue [#67](https://github.com/daniele77/cli/issues/67)) 76 | - Add a namespace and folder "detail" (issue [#17](https://github.com/daniele77/cli/issues/17)) 77 | 78 | ## [1.1.1] - 2019-09-16 79 | 80 | - Specify binding IP address in CliTelnetServer (issue [#44](https://github.com/daniele77/cli/issues/44)) 81 | - Fix compilation with boost v. 1.70.0 (issue [#40](https://github.com/daniele77/cli/issues/40)) 82 | - Fix assertion in History::Next() when buffer is empty (issue [#47](https://github.com/daniele77/cli/issues/47)) 83 | - Fix compilation with older boost versions (< 1.66.0) 84 | - CMake generates cli.pc configuration file for pkg-config 85 | 86 | ## [1.1.0] - 2019-04-14 87 | 88 | - Dynamically Remove/Disable/Enable commands and submenus (issue [#15](https://github.com/daniele77/cli/issues/15)) 89 | - New variadic template method to add commands and menu (makes Add() deprecated) 90 | - Optionally delimitate string parameters by " (issue [#38](https://github.com/daniele77/cli/issues/38)) 91 | - Explicitly set the names of parameters in help description 92 | - Unit tests 93 | - CMake support 94 | - Vcpkg support 95 | 96 | ## [1.0.0] - 2019-02-16 97 | 98 | - Fix Clang 3.6 - 7.0 undefined reference bug 99 | - Fix infinite loop on EoF 100 | - Fix issue #27 (odd history behaviour): the history now works like in bash 101 | - Fix issue #28 (check for invalid stream in CliFileSession class) 102 | - Add doxygen basic configuration 103 | - Add session recordings in README file 104 | - Add CHANGELOG.md, CODE_OF_CONDUCT.md, CONTRIBUTING.md files 105 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | cmake_minimum_required(VERSION 3.8) 31 | 32 | project( 33 | cli 34 | VERSION 2.2.0 35 | # DESCRIPTION "A library for interactive command line interfaces in modern C++" 36 | LANGUAGES CXX 37 | ) 38 | 39 | include(GNUInstallDirs) 40 | 41 | option(CLI_BuildExamples "Build the examples." OFF) 42 | option(CLI_BuildTests "Build the unit tests." OFF) 43 | option(CLI_UseBoostAsio "Use the boost asio library." OFF) 44 | option(CLI_UseStandaloneAsio "Use the standalone asio library." OFF) 45 | 46 | 47 | if(WIN32) 48 | macro(get_WIN32_WINNT version) 49 | if(CMAKE_SYSTEM_VERSION) 50 | set(ver ${CMAKE_SYSTEM_VERSION}) 51 | string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver}) 52 | string(REGEX MATCH "^([0-9]+)" verMajor ${ver}) 53 | # Check for Windows 10, b/c we'll need to convert to hex 'A'. 54 | if("${verMajor}" MATCHES "10") 55 | set(verMajor "A") 56 | string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver}) 57 | endif() 58 | # Remove all remaining '.' characters. 59 | string(REPLACE "." "" ver ${ver}) 60 | # Prepend each digit with a zero. 61 | string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver}) 62 | set(${version} "0x${ver}") 63 | endif() 64 | endmacro() 65 | 66 | get_WIN32_WINNT(ver) 67 | add_definitions(-D_WIN32_WINNT=${ver}) 68 | endif() 69 | 70 | if (CLI_UseBoostAsio) 71 | set(Boost_NO_BOOST_CMAKE ON) 72 | add_definitions( -DBOOST_ALL_NO_LIB ) # for windows 73 | find_package(Boost 1.66 REQUIRED COMPONENTS system) 74 | endif() 75 | 76 | if (CLI_UseStandaloneAsio) 77 | find_path(STANDALONE_ASIO_INCLUDE_PATH NAMES "asio.hpp" HINTS ${ASIO_INCLUDEDIR}) 78 | mark_as_advanced(STANDALONE_ASIO_INCLUDE_PATH) 79 | endif() 80 | 81 | find_package(Threads REQUIRED) 82 | 83 | # Add Library 84 | add_library(cli INTERFACE) 85 | # Add target alias 86 | add_library(cli::cli ALIAS cli) 87 | 88 | target_include_directories(cli 89 | INTERFACE 90 | $ 91 | $ 92 | ) 93 | 94 | target_link_libraries(cli INTERFACE Threads::Threads) 95 | if (CLI_UseBoostAsio) 96 | target_link_libraries(cli INTERFACE Boost::system) 97 | target_compile_definitions(cli INTERFACE BOOST_ASIO_NO_DEPRECATED=1) 98 | endif() 99 | if (CLI_UseStandaloneAsio) 100 | add_library(standalone_asio INTERFACE IMPORTED) 101 | target_include_directories(standalone_asio SYSTEM INTERFACE ${STANDALONE_ASIO_INCLUDE_PATH}) 102 | target_link_libraries(standalone_asio INTERFACE Threads::Threads) 103 | target_link_libraries(cli INTERFACE standalone_asio) 104 | 105 | # alternative way: 106 | # target_include_directories(cli SYSTEM INTERFACE ${STANDALONE_ASIO_INCLUDE_PATH}) 107 | endif() 108 | 109 | if(NOT DEFINED CMAKE_CXX_STANDARD) 110 | set(CMAKE_CXX_STANDARD 14) 111 | endif() 112 | target_compile_features(cli INTERFACE cxx_std_${CMAKE_CXX_STANDARD}) 113 | 114 | # Examples 115 | if (CLI_BuildExamples) 116 | add_subdirectory(examples) 117 | endif() 118 | 119 | # Tests 120 | if (CLI_BuildTests) 121 | enable_testing() 122 | add_subdirectory(test) 123 | endif() 124 | 125 | # Install 126 | if(NOT CMAKE_SKIP_INSTALL_RULES) 127 | install(DIRECTORY include/cli DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 128 | 129 | include(CMakePackageConfigHelpers) 130 | configure_package_config_file( 131 | "cliConfig.cmake.in" 132 | "${CMAKE_CURRENT_BINARY_DIR}/cliConfig.cmake" 133 | INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cli" 134 | ) 135 | 136 | # Generate pkg-config .pc file 137 | set(PKGCONFIG_INSTALL_DIR 138 | ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig 139 | CACHE PATH "Installation directory for pkg-config (cli.pc) file" 140 | ) 141 | configure_file( 142 | "cli.pc.in" 143 | "cli.pc" 144 | @ONLY 145 | ) 146 | 147 | install(TARGETS cli EXPORT cliTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) 148 | install(EXPORT cliTargets FILE cliTargets.cmake NAMESPACE cli:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cli) 149 | 150 | install( 151 | FILES "${CMAKE_CURRENT_BINARY_DIR}/cliConfig.cmake" 152 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/cli" 153 | ) 154 | install( 155 | FILES "${CMAKE_CURRENT_BINARY_DIR}/cli.pc" 156 | DESTINATION ${PKGCONFIG_INSTALL_DIR} 157 | ) 158 | endif() 159 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | * Be respectful of others 4 | 5 | * Please use professional conduct 6 | 7 | * Treat others the way you want to be treated 8 | 9 | Thank you! 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to cli 2 | 3 | Thank you for taking the time to read this document! 4 | 5 | :heart_eyes: **please star the cli repository!** :heart_eyes: 6 | 7 | First make sure you're on the 8 | [official repository page](https://github.com/daniele77/cli/blob/master/CONTRIBUTING.md), 9 | then just press the button labeled "star" in the top right of the page 10 | to give cli a star! Your stars create generate additional visibility, 11 | which leads to more users, more bug reports, more fixes, more testing, 12 | more features, and a better product! 13 | 14 | ## Ways to Contribute 15 | 16 | No contribution to cli is too big or too small! We are 17 | always happy to see user participation in all of its forms. 18 | Here are some of the ways that you can contribute (this is 19 | by no means an exhaustive list): 20 | 21 | * Submit a 22 | [bug report](https://github.com/daniele77/cli/issues). 23 | We love hearing about broken things, so 24 | that we can fix them. Any problem is fair game, this includes 25 | the documentation examples, tests, and repository as well as the 26 | library itself. Bug reports should be opened on the 27 | [Issues](https://github.com/daniele77/cli/issues) page. 28 | 29 | * Provide 30 | [feedback](https://github.com/daniele77/cli/issues). 31 | about the library. If you have used, are using, 32 | or are thinking about using the library we want to hear about it! 33 | The more information you provide, the better we will be able 34 | to ensure that cli meets your needs. You can provide feedback 35 | as an issue, or email the author directly. 36 | 37 | * Test cli. 38 | 39 | * Use cli in your next application! At the end of the day, it is the 40 | goal of the library to provide utility to users so the best way you 41 | can contribute is simply to use the library! 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /cli.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | includedir=${prefix}/include 3 | 4 | Name: cli 5 | Description: A cross-platform header only C++14 library for interactive command line interfaces (Cisco style) 6 | URL: https://github.com/daniele77/cli 7 | Version: @cli_VERSION@ 8 | Cflags: -I${includedir} 9 | -------------------------------------------------------------------------------- /cliConfig.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include(CMakeFindDependencyMacro) 4 | 5 | if (CLI_UseBoostAsio) 6 | find_dependency(Boost REQUIRED COMPONENTS system) 7 | endif() 8 | 9 | find_dependency(Threads REQUIRED) 10 | 11 | if(NOT TARGET cli::cli) 12 | include(${CMAKE_CURRENT_LIST_DIR}/cliTargets.cmake) 13 | endif() -------------------------------------------------------------------------------- /doc/doxy/Doxyfile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | #--------------------------------------------------------------------------- 31 | PROJECT_NAME = "Interactive CLI" 32 | PROJECT_NUMBER = 2.2 33 | #--------------------------------------------------------------------------- 34 | FULL_PATH_NAMES = NO 35 | #--------------------------------------------------------------------------- 36 | EXTRACT_ALL = YES 37 | EXTRACT_PRIVATE = NO 38 | EXTRACT_STATIC = NO 39 | #--------------------------------------------------------------------------- 40 | INPUT = ../../include/cli 41 | #--------------------------------------------------------------------------- 42 | SOURCE_BROWSER = YES 43 | VERBATIM_HEADERS = NO 44 | INLINE_SOURCES = NO 45 | #--------------------------------------------------------------------------- 46 | GENERATE_HTML = YES 47 | HTML_COLORSTYLE_HUE = 220 48 | HTML_COLORSTYLE_SAT = 100 49 | HTML_COLORSTYLE_GAMMA = 80 50 | SEARCHENGINE = YES 51 | #--------------------------------------------------------------------------- 52 | GENERATE_LATEX = NO 53 | GENERATE_RTF = NO 54 | GENERATE_MAN = NO 55 | GENERATE_XML = NO 56 | GENERATE_DOCBOOK = NO 57 | GENERATE_AUTOGEN_DEF = NO 58 | GENERATE_PERLMOD = NO 59 | #--------------------------------------------------------------------------- 60 | HAVE_DOT = YES 61 | CLASS_DIAGRAMS = YES 62 | CLASS_GRAPH = YES 63 | COLLABORATION_GRAPH = NO 64 | INCLUDE_GRAPH = YES 65 | INCLUDED_BY_GRAPH = YES 66 | CALL_GRAPH = NO 67 | CALLER_GRAPH = YES 68 | GRAPHICAL_HIERARCHY = YES 69 | DIRECTORY_GRAPH = YES 70 | 71 | -------------------------------------------------------------------------------- /doc/uml/cmdhandlers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daniele77/cli/9b0b99f746fa9de3f5ad78a695b5d650738e0676/doc/uml/cmdhandlers.png -------------------------------------------------------------------------------- /doc/uml/implementation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daniele77/cli/9b0b99f746fa9de3f5ad78a695b5d650738e0676/doc/uml/implementation.png -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | 31 | # pluginmanager, filesession and simplelocalsession can be compiled w/o asio 32 | set(SOURCES pluginmanager filesession simplelocalsession) 33 | 34 | if (CLI_UseBoostAsio OR CLI_UseStandaloneAsio) 35 | if (CLI_UseBoostAsio) 36 | add_definitions( -DCLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER ) 37 | # boostasio_nonowner_iocontext can only be compiled with boost asio 38 | list(APPEND SOURCES boostasio_nonowner_iocontext) 39 | endif() 40 | if (CLI_UseStandaloneAsio) 41 | add_definitions( -DCLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER ) 42 | # standaloneasio_nonowner_iocontext can only be compiled with standalone asio 43 | list(APPEND SOURCES standaloneasio_nonowner_iocontext) 44 | endif() 45 | # asyncsession, and complete can be compiled if asio is available (either standalone or boost) 46 | list(APPEND SOURCES complete) 47 | # asyncsession only compiles on POSIX platforms 48 | if(NOT WIN32) 49 | list(APPEND SOURCES asyncsession) 50 | else() 51 | message("example `asyncsession` not built as it's not supported on Windows") 52 | endif() 53 | else() 54 | add_definitions( -DCLI_EXAMPLES_USE_LOOP_SCHEDULER ) 55 | message("examples `nonowner_iocontext` and `complete` are not built because asio library is not available") 56 | endif() 57 | 58 | foreach(example ${SOURCES}) 59 | add_executable(${example} ${example}.cpp) 60 | target_link_libraries(${example} PRIVATE cli::cli) 61 | endforeach(example) 62 | -------------------------------------------------------------------------------- /examples/Makefile.boostasio: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | override CXXFLAGS += -O3 -Werror -Wall -Wextra -Wpedantic -std=c++1y -I../include -DCLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER 31 | override LDLIBS += -lboost_system -lpthread 32 | 33 | EXAMPLES := complete asyncsession filesession simplelocalsession pluginmanager boostasio_nonowner_iocontext 34 | 35 | .PHONY: clean all 36 | 37 | all: $(EXAMPLES) 38 | 39 | clean: 40 | @- $(RM) *.o *~ core $(EXAMPLES) 41 | 42 | -------------------------------------------------------------------------------- /examples/Makefile.noasio: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | override CXXFLAGS += -O3 -Werror -Wall -Wextra -Wpedantic -std=c++1y -I../include 31 | override LDLIBS += -lpthread 32 | 33 | EXAMPLES := filesession simplelocalsession pluginmanager 34 | 35 | .PHONY: clean all 36 | 37 | all: $(EXAMPLES) 38 | 39 | clean: 40 | @- $(RM) *.o *~ core $(EXAMPLES) 41 | 42 | -------------------------------------------------------------------------------- /examples/Makefile.standaloneasio: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | override CXXFLAGS += -O3 -Werror -Wall -Wextra -Wpedantic -std=c++1y -I../include -DCLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER 31 | override LDLIBS += -lpthread 32 | 33 | EXAMPLES := complete asyncsession filesession simplelocalsession pluginmanager standaloneasio_nonowner_iocontext 34 | 35 | .PHONY: clean all 36 | 37 | all: $(EXAMPLES) 38 | 39 | clean: 40 | @- $(RM) *.o *~ core $(EXAMPLES) 41 | 42 | -------------------------------------------------------------------------------- /examples/asyncsession.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #if defined(WIN32) || defined(_WIN32) || defined(_WIN64) 31 | #error "AsyncSession only works on POSIX platforms." 32 | #endif 33 | 34 | 35 | #ifdef CLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER 36 | #include 37 | #include 38 | namespace cli 39 | { 40 | using MainScheduler = StandaloneAsioScheduler; 41 | using CliAsyncSession = StandaloneAsioCliAsyncSession; 42 | } // namespace cli 43 | #elif defined(CLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER) 44 | #include 45 | #include 46 | namespace cli 47 | { 48 | using MainScheduler = BoostAsioScheduler; 49 | using CliAsyncSession = BoostAsioCliAsyncSession; 50 | } 51 | #else 52 | #error either CLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER or CLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER must be defined 53 | #endif 54 | 55 | #include 56 | 57 | using namespace cli; 58 | using namespace std; 59 | 60 | int main() 61 | { 62 | try 63 | { 64 | 65 | // setup cli 66 | 67 | auto rootMenu = make_unique< Menu >( "cli" ); 68 | rootMenu -> Insert( 69 | "hello", 70 | [](std::ostream& out){ out << "Hello, world\n"; }, 71 | "Print hello world" ); 72 | rootMenu -> Insert( 73 | "hello_everysession", 74 | [](std::ostream&){ Cli::cout() << "Hello, everybody" << std::endl; }, 75 | "Print hello everybody on all open sessions" ); 76 | rootMenu -> Insert( 77 | "answer", 78 | [](std::ostream& out, int x){ out << "The answer is: " << x << "\n"; }, 79 | "Print the answer to Life, the Universe and Everything " ); 80 | rootMenu -> Insert( 81 | "color", 82 | [](std::ostream& out){ out << "Colors ON\n"; SetColor(); }, 83 | "Enable colors in the cli" ); 84 | rootMenu -> Insert( 85 | "nocolor", 86 | [](std::ostream& out){ out << "Colors OFF\n"; SetNoColor(); }, 87 | "Disable colors in the cli" ); 88 | 89 | auto subMenu = make_unique< Menu >( "sub" ); 90 | subMenu -> Insert( 91 | "hello", 92 | [](std::ostream& out){ out << "Hello, submenu world\n"; }, 93 | "Print hello world in the submenu" ); 94 | subMenu -> Insert( 95 | "demo", 96 | [](std::ostream& out){ out << "This is a sample!\n"; }, 97 | "Print a demo string" ); 98 | 99 | auto subSubMenu = make_unique< Menu >( "subsub" ); 100 | subSubMenu -> Insert( 101 | "hello", 102 | [](std::ostream& out){ out << "Hello, subsubmenu world\n"; }, 103 | "Print hello world in the sub-submenu" ); 104 | subMenu -> Insert( std::move(subSubMenu)); 105 | 106 | rootMenu -> Insert( std::move(subMenu) ); 107 | 108 | 109 | Cli cli( std::move(rootMenu) ); 110 | // global exit action 111 | cli.ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } ); 112 | 113 | MainScheduler scheduler; 114 | CliAsyncSession session(scheduler, cli); 115 | session.ExitAction( 116 | [&scheduler](auto& out) // session exit action 117 | { 118 | out << "Closing App...\n"; 119 | scheduler.Stop(); 120 | } 121 | ); 122 | scheduler.Run(); 123 | 124 | return 0; 125 | } 126 | catch (const std::exception& e) 127 | { 128 | cerr << "Exception caught in main: " << e.what() << '\n'; 129 | } 130 | catch (...) 131 | { 132 | cerr << "Unknown exception caught in main.\n"; 133 | } 134 | return -1; 135 | } 136 | -------------------------------------------------------------------------------- /examples/boostasio_nonowner_iocontext.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | // TODO: NB boostasioscheduler.h includes boost asio 31 | // so in Windows it should appear before cli.h and clilocalsession.h that include rang, 32 | // because both include WinSock.h 33 | // (consider to provide a global header file for the library) 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | using namespace cli; 40 | using namespace std; 41 | 42 | #if BOOST_VERSION < 106600 43 | using IoContext = boost::asio::io_service; 44 | #else 45 | using IoContext = boost::asio::io_context; 46 | #endif 47 | 48 | class UserInterface 49 | { 50 | public: 51 | UserInterface(IoContext& iocontext) : scheduler(iocontext) 52 | { 53 | auto rootMenu = make_unique< Menu >("cli"); 54 | rootMenu->Insert( 55 | "hello", 56 | [](std::ostream& out){ out << "Hello, world\n"; }, 57 | "Print hello world" ); 58 | colorCmd = rootMenu->Insert( 59 | "color", 60 | [&](std::ostream& out) 61 | { 62 | out << "Colors ON\n"; 63 | SetColor(); 64 | colorCmd.Disable(); 65 | nocolorCmd.Enable(); 66 | }, 67 | "Enable colors in the cli" ); 68 | nocolorCmd = rootMenu->Insert( 69 | "nocolor", 70 | [&](std::ostream& out) 71 | { 72 | out << "Colors OFF\n"; 73 | SetNoColor(); 74 | colorCmd.Enable(); 75 | nocolorCmd.Disable(); 76 | }, 77 | "Disable colors in the cli" ); 78 | 79 | cli = make_unique(std::move(rootMenu)); 80 | // global exit action 81 | cli->ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } ); 82 | // std exception custom handler 83 | cli->StdExceptionHandler( 84 | [](std::ostream& out, const std::string& cmd, const std::exception& e) 85 | { 86 | out << "Exception caught in cli handler: " 87 | << e.what() 88 | << " handling command: " 89 | << cmd 90 | << ".\n"; 91 | } 92 | ); 93 | 94 | localSession = make_unique(*cli, scheduler, std::cout, 200); 95 | localSession->ExitAction( 96 | [this](auto& out) // session exit action 97 | { 98 | out << "Closing App...\n"; 99 | scheduler.Stop(); 100 | } 101 | ); 102 | } 103 | private: 104 | BoostAsioScheduler scheduler; 105 | unique_ptr cli; 106 | unique_ptr localSession; 107 | CmdHandler colorCmd; 108 | CmdHandler nocolorCmd; 109 | }; 110 | 111 | int main() 112 | { 113 | try 114 | { 115 | // main application that creates an asio io_context and uses it 116 | IoContext iocontext; 117 | boost::asio::steady_timer timer(iocontext, std::chrono::seconds(5)); 118 | timer.async_wait([](const error_code&){ cout << "Timer expired!\n"; }); 119 | 120 | // cli setup 121 | UserInterface interface(iocontext); 122 | 123 | // start the asio io_context 124 | #if BOOST_VERSION < 106600 125 | boost::asio::io_service::work work(iocontext); 126 | #else 127 | auto work = boost::asio::make_work_guard(iocontext); 128 | #endif 129 | iocontext.run(); 130 | 131 | return 0; 132 | } 133 | catch (const std::exception& e) 134 | { 135 | cerr << "Exception caught in main: " << e.what() << '\n'; 136 | } 137 | catch (...) 138 | { 139 | cerr << "Unknown exception caught in main.\n"; 140 | } 141 | return -1; 142 | } 143 | -------------------------------------------------------------------------------- /examples/complete_vsprj/common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ..\..\include;$(BOOST) 9 | _MBCS;%(PreprocessorDefinitions);_WIN32_WINNT=0x0501; BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE;_SCL_SECURE_NO_WARNINGS 10 | 11 | 12 | $(BOOST)\stage\lib 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /examples/complete_vsprj/complete.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70} 24 | complete 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v143 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v143 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v143 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v143 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | Level3 80 | Disabled 81 | true 82 | true 83 | 84 | 85 | 86 | 87 | Level3 88 | Disabled 89 | true 90 | true 91 | CLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER;%(PreprocessorDefinitions) 92 | 93 | 94 | 95 | 96 | Level3 97 | MaxSpeed 98 | true 99 | true 100 | true 101 | true 102 | 103 | 104 | true 105 | true 106 | 107 | 108 | 109 | 110 | Level3 111 | MaxSpeed 112 | true 113 | true 114 | true 115 | true 116 | CLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER;%(PreprocessorDefinitions) 117 | 118 | 119 | true 120 | true 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /examples/examples.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30907.101 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "complete", "complete_vsprj\complete.vcxproj", "{4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "filesession", "filesession_vsprj\filesession.vcxproj", "{35EAF199-0396-4370-9939-09B9D2714969}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "simplelocalsession", "simplelocalsession_vsprj\simplelocalsession.vcxproj", "{F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}" 11 | EndProject 12 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pluginmanager", "pluginmanager_vsprj\pluginmanager.vcxproj", "{998A824A-CF12-4D78-ADF6-EFCAB931AE01}" 13 | EndProject 14 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "boostasio_nonowner_iocontext", "boostasio_nonowner_iocontext_vsprj\boostasio_nonowner_iocontext.vcxproj", "{5B123F81-785B-413E-AF10-C5B8B7C4C5E9}" 15 | EndProject 16 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "standaloneasio_nonowner_iocontext", "standaloneasio_nonowner_iocontext_vsprj\standaloneasio_nonowner_iocontext.vcxproj", "{611D1C07-FBD2-452E-AC91-AB19263EDD64}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|x64 = Debug|x64 21 | Debug|x86 = Debug|x86 22 | Release|x64 = Release|x64 23 | Release|x86 = Release|x86 24 | EndGlobalSection 25 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 26 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Debug|x64.ActiveCfg = Debug|x64 27 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Debug|x64.Build.0 = Debug|x64 28 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Debug|x86.ActiveCfg = Debug|Win32 29 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Debug|x86.Build.0 = Debug|Win32 30 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Release|x64.ActiveCfg = Release|x64 31 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Release|x64.Build.0 = Release|x64 32 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Release|x86.ActiveCfg = Release|Win32 33 | {4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}.Release|x86.Build.0 = Release|Win32 34 | {35EAF199-0396-4370-9939-09B9D2714969}.Debug|x64.ActiveCfg = Debug|x64 35 | {35EAF199-0396-4370-9939-09B9D2714969}.Debug|x64.Build.0 = Debug|x64 36 | {35EAF199-0396-4370-9939-09B9D2714969}.Debug|x86.ActiveCfg = Debug|Win32 37 | {35EAF199-0396-4370-9939-09B9D2714969}.Debug|x86.Build.0 = Debug|Win32 38 | {35EAF199-0396-4370-9939-09B9D2714969}.Release|x64.ActiveCfg = Release|x64 39 | {35EAF199-0396-4370-9939-09B9D2714969}.Release|x64.Build.0 = Release|x64 40 | {35EAF199-0396-4370-9939-09B9D2714969}.Release|x86.ActiveCfg = Release|Win32 41 | {35EAF199-0396-4370-9939-09B9D2714969}.Release|x86.Build.0 = Release|Win32 42 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Debug|x64.ActiveCfg = Debug|x64 43 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Debug|x64.Build.0 = Debug|x64 44 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Debug|x86.ActiveCfg = Debug|Win32 45 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Debug|x86.Build.0 = Debug|Win32 46 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Release|x64.ActiveCfg = Release|x64 47 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Release|x64.Build.0 = Release|x64 48 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Release|x86.ActiveCfg = Release|Win32 49 | {F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}.Release|x86.Build.0 = Release|Win32 50 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Debug|x64.ActiveCfg = Debug|x64 51 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Debug|x64.Build.0 = Debug|x64 52 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Debug|x86.ActiveCfg = Debug|Win32 53 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Debug|x86.Build.0 = Debug|Win32 54 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Release|x64.ActiveCfg = Release|x64 55 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Release|x64.Build.0 = Release|x64 56 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Release|x86.ActiveCfg = Release|Win32 57 | {998A824A-CF12-4D78-ADF6-EFCAB931AE01}.Release|x86.Build.0 = Release|Win32 58 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Debug|x64.ActiveCfg = Debug|x64 59 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Debug|x64.Build.0 = Debug|x64 60 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Debug|x86.ActiveCfg = Debug|Win32 61 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Debug|x86.Build.0 = Debug|Win32 62 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Release|x64.ActiveCfg = Release|x64 63 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Release|x64.Build.0 = Release|x64 64 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Release|x86.ActiveCfg = Release|Win32 65 | {5B123F81-785B-413E-AF10-C5B8B7C4C5E9}.Release|x86.Build.0 = Release|Win32 66 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Debug|x64.ActiveCfg = Debug|x64 67 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Debug|x64.Build.0 = Debug|x64 68 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Debug|x86.ActiveCfg = Debug|Win32 69 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Debug|x86.Build.0 = Debug|Win32 70 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Release|x64.ActiveCfg = Release|x64 71 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Release|x64.Build.0 = Release|x64 72 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Release|x86.ActiveCfg = Release|Win32 73 | {611D1C07-FBD2-452E-AC91-AB19263EDD64}.Release|x86.Build.0 = Release|Win32 74 | EndGlobalSection 75 | GlobalSection(SolutionProperties) = preSolution 76 | HideSolutionNode = FALSE 77 | EndGlobalSection 78 | GlobalSection(ExtensibilityGlobals) = postSolution 79 | SolutionGuid = {9E30D1A9-BCDB-4D31-860B-70CFE7A70FA6} 80 | EndGlobalSection 81 | EndGlobal 82 | -------------------------------------------------------------------------------- /examples/filesession.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | using namespace cli; 35 | using namespace std; 36 | 37 | 38 | int main() 39 | { 40 | // setup cli 41 | 42 | auto rootMenu = make_unique< Menu >( "cli" ); 43 | rootMenu -> Insert( 44 | "hello", 45 | [](std::ostream& out){ out << "Hello, world\n"; }, 46 | "Print hello world" ); 47 | rootMenu -> Insert( 48 | "hello_everysession", 49 | [](std::ostream&){ Cli::cout() << "Hello, everybody" << std::endl; }, 50 | "Print hello everybody on all open sessions" ); 51 | rootMenu -> Insert( 52 | "answer", 53 | [](std::ostream& out, int x){ out << "The answer is: " << x << "\n"; }, 54 | "Print the answer to Life, the Universe and Everything " ); 55 | rootMenu -> Insert( 56 | "color", 57 | [](std::ostream& out){ out << "Colors ON\n"; SetColor(); }, 58 | "Enable colors in the cli" ); 59 | rootMenu -> Insert( 60 | "nocolor", 61 | [](std::ostream& out){ out << "Colors OFF\n"; SetNoColor(); }, 62 | "Disable colors in the cli" ); 63 | 64 | auto subMenu = make_unique< Menu >( "sub" ); 65 | subMenu -> Insert( 66 | "hello", 67 | [](std::ostream& out){ out << "Hello, submenu world\n"; }, 68 | "Print hello world in the submenu" ); 69 | subMenu -> Insert( 70 | "demo", 71 | [](std::ostream& out){ out << "This is a sample!\n"; }, 72 | "Print a demo string" ); 73 | 74 | auto subSubMenu = make_unique< Menu >( "subsub" ); 75 | subSubMenu -> Insert( 76 | "hello", 77 | [](std::ostream& out){ out << "Hello, subsubmenu world\n"; }, 78 | "Print hello world in the sub-submenu" ); 79 | subMenu -> Insert(std::move(subSubMenu)); 80 | 81 | rootMenu -> Insert(std::move(subMenu)); 82 | 83 | 84 | Cli cli( std::move(rootMenu) ); 85 | // global exit action 86 | cli.ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } ); 87 | 88 | std::ifstream infile("input.txt"); 89 | if (!infile) 90 | { 91 | std::cerr << "File input.txt not found in current directory!\n"; 92 | return 1; 93 | } 94 | std::ofstream outfile("output.txt"); 95 | if (!outfile) 96 | { 97 | std::cerr << "Can't write file output.txt in the current directory!\n"; 98 | return 1; 99 | } 100 | CliFileSession input(cli, infile, outfile); 101 | input.Start(); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /examples/input.txt: -------------------------------------------------------------------------------- 1 | help 2 | hello 3 | exit 4 | -------------------------------------------------------------------------------- /examples/makefile.boostasio.win: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | #define macros 31 | EXE_NAMES = complete.exe filesession.exe simplelocalsession.exe pluginmanager.exe boostasio_nonowner_iocontext.exe 32 | DIR_INCLUDE = /I..\include /I%BOOST% 33 | DIR_LINK = %BOOST%\stage\lib 34 | COMPILE_FLAGS = /nologo /MD /EHsc /D_WIN32_WINNT=0x0501 /DBOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE /DCLI_EXAMPLES_USE_BOOSTASIO_SCHEDULER 35 | CPPFLAGS = $(COMPILE_FLAGS) $(DIR_INCLUDE) /link /LIBPATH:$(DIR_LINK) 36 | RM = del /F /Q 2> nul 37 | 38 | .PHONY: all apps clean 39 | 40 | # clean and build examples 41 | all: clean apps 42 | 43 | .cpp.exe: 44 | @echo Compiling and linking: $< 45 | $(CPP) $< $(CPPFLAGS) 46 | 47 | # applications 48 | apps: $(EXE_NAMES) 49 | 50 | # delete output files 51 | clean: 52 | @-$(RM) *.obj 53 | @-$(RM) $(EXE_NAMES) 54 | 55 | 56 | -------------------------------------------------------------------------------- /examples/makefile.noasio.win: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | #define macros 31 | EXE_NAMES = filesession.exe simplelocalsession.exe pluginmanager.exe 32 | DIR_INCLUDE = /I..\include 33 | COMPILE_FLAGS = /nologo /MD /EHsc /D_WIN32_WINNT=0x0501 34 | CPPFLAGS = $(COMPILE_FLAGS) $(DIR_INCLUDE) 35 | RM = del /F /Q 2> nul 36 | 37 | .PHONY: all apps clean 38 | 39 | # clean and build examples 40 | all: clean apps 41 | 42 | .cpp.exe: 43 | @echo Compiling and linking: $< 44 | $(CPP) $< $(CPPFLAGS) 45 | 46 | # applications 47 | apps: $(EXE_NAMES) 48 | 49 | # delete output files 50 | clean: 51 | @-$(RM) *.obj 52 | @-$(RM) $(EXE_NAMES) 53 | 54 | 55 | -------------------------------------------------------------------------------- /examples/makefile.standaloneasio.win: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | #define macros 31 | EXE_NAMES = complete.exe filesession.exe simplelocalsession.exe pluginmanager.exe standaloneasio_nonowner_iocontext.exe 32 | DIR_INCLUDE = /I%ASIO% /I..\include 33 | COMPILE_FLAGS = /nologo /MD /EHsc /D_WIN32_WINNT=0x0501 /DCLI_EXAMPLES_USE_STANDALONEASIO_SCHEDULER 34 | CPPFLAGS = $(COMPILE_FLAGS) $(DIR_INCLUDE) 35 | RM = del /F /Q 2> nul 36 | 37 | .PHONY: all apps clean 38 | 39 | # clean and build examples 40 | all: clean apps 41 | 42 | .cpp.exe: 43 | @echo Compiling and linking: $< 44 | $(CPP) $< $(CPPFLAGS) 45 | 46 | # applications 47 | apps: $(EXE_NAMES) 48 | 49 | # delete output files 50 | clean: 51 | @-$(RM) *.obj 52 | @-$(RM) $(EXE_NAMES) 53 | 54 | 55 | -------------------------------------------------------------------------------- /examples/simplelocalsession.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include 32 | 33 | using namespace cli; 34 | using namespace std; 35 | 36 | 37 | int main() 38 | { 39 | // setup cli 40 | 41 | auto rootMenu = make_unique< Menu >( "cli" ); 42 | rootMenu -> Insert( 43 | "hello", 44 | [](std::ostream& out){ out << "Hello, world\n"; }, 45 | "Print hello world" ); 46 | rootMenu -> Insert( 47 | "hello_everysession", 48 | [](std::ostream&){ Cli::cout() << "Hello, everybody" << std::endl; }, 49 | "Print hello everybody on all open sessions" ); 50 | rootMenu -> Insert( 51 | "answer", 52 | [](std::ostream& out, int x){ out << "The answer is: " << x << "\n"; }, 53 | "Print the answer to Life, the Universe and Everything " ); 54 | rootMenu -> Insert( 55 | "color", 56 | [](std::ostream& out){ out << "Colors ON\n"; SetColor(); }, 57 | "Enable colors in the cli" ); 58 | rootMenu -> Insert( 59 | "nocolor", 60 | [](std::ostream& out){ out << "Colors OFF\n"; SetNoColor(); }, 61 | "Disable colors in the cli" ); 62 | 63 | auto subMenu = make_unique< Menu >( "sub" ); 64 | subMenu -> Insert( 65 | "hello", 66 | [](std::ostream& out){ out << "Hello, submenu world\n"; }, 67 | "Print hello world in the submenu" ); 68 | subMenu -> Insert( 69 | "demo", 70 | [](std::ostream& out){ out << "This is a sample!\n"; }, 71 | "Print a demo string" ); 72 | 73 | auto subSubMenu = make_unique< Menu >( "subsub" ); 74 | subSubMenu -> Insert( 75 | "hello", 76 | [](std::ostream& out){ out << "Hello, subsubmenu world\n"; }, 77 | "Print hello world in the sub-submenu" ); 78 | subMenu -> Insert( std::move(subSubMenu)); 79 | 80 | rootMenu -> Insert( std::move(subMenu) ); 81 | 82 | 83 | Cli cli( std::move(rootMenu) ); 84 | // global exit action 85 | cli.ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } ); 86 | 87 | CliFileSession input(cli); 88 | input.Start(); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /examples/standaloneasio_nonowner_iocontext.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | using namespace cli; 35 | using namespace std; 36 | 37 | #if ASIO_VERSION < 101200 38 | using IoContext = asio::io_service; 39 | #else 40 | using IoContext = asio::io_context; 41 | #endif 42 | 43 | class UserInterface 44 | { 45 | public: 46 | explicit UserInterface(IoContext& iocontext) : scheduler(iocontext) 47 | { 48 | auto rootMenu = make_unique< Menu >("cli"); 49 | rootMenu->Insert( 50 | "hello", 51 | [](std::ostream& out){ out << "Hello, world\n"; }, 52 | "Print hello world" ); 53 | colorCmd = rootMenu->Insert( 54 | "color", 55 | [&](std::ostream& out) 56 | { 57 | out << "Colors ON\n"; 58 | SetColor(); 59 | colorCmd.Disable(); 60 | nocolorCmd.Enable(); 61 | }, 62 | "Enable colors in the cli" ); 63 | nocolorCmd = rootMenu->Insert( 64 | "nocolor", 65 | [&](std::ostream& out) 66 | { 67 | out << "Colors OFF\n"; 68 | SetNoColor(); 69 | colorCmd.Enable(); 70 | nocolorCmd.Disable(); 71 | }, 72 | "Disable colors in the cli" ); 73 | 74 | cli = make_unique(std::move(rootMenu)); 75 | // global exit action 76 | cli->ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } ); 77 | // std exception custom handler 78 | cli->StdExceptionHandler( 79 | [](std::ostream& out, const std::string& cmd, const std::exception& e) 80 | { 81 | out << "Exception caught in cli handler: " 82 | << e.what() 83 | << " handling command: " 84 | << cmd 85 | << ".\n"; 86 | } 87 | ); 88 | 89 | localSession = make_unique(*cli, scheduler, std::cout, 200); 90 | localSession->ExitAction( 91 | [this](auto& out) // session exit action 92 | { 93 | out << "Closing App...\n"; 94 | scheduler.Stop(); 95 | } 96 | ); 97 | } 98 | private: 99 | StandaloneAsioScheduler scheduler; 100 | unique_ptr cli; 101 | unique_ptr localSession; 102 | CmdHandler colorCmd; 103 | CmdHandler nocolorCmd; 104 | }; 105 | 106 | int main() 107 | { 108 | try 109 | { 110 | // main application that creates an asio io_context and uses it 111 | IoContext iocontext; 112 | asio::steady_timer timer(iocontext, std::chrono::seconds(5)); 113 | timer.async_wait([](const error_code&){ cout << "Timer expired!\n"; }); 114 | 115 | // cli setup 116 | UserInterface interface(iocontext); 117 | 118 | // start the asio io_context 119 | #if ASIO_VERSION < 101200 120 | asio::io_service::work work(iocontext); 121 | #else 122 | auto work = asio::make_work_guard(iocontext); 123 | #endif 124 | iocontext.run(); 125 | 126 | return 0; 127 | } 128 | catch (const std::exception& e) 129 | { 130 | cerr << "Exception caught in main: " << e.what() << '\n'; 131 | } 132 | catch (...) 133 | { 134 | cerr << "Unknown exception caught in main.\n"; 135 | } 136 | return -1; 137 | } 138 | -------------------------------------------------------------------------------- /include/cli/boostasiocliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOCLIASYNCSESSION_H_ 31 | #define CLI_BOOSTASIOCLIASYNCSESSION_H_ 32 | 33 | #include "detail/genericcliasyncsession.h" 34 | #include "detail/boostasiolib.h" 35 | 36 | 37 | namespace cli { using BoostAsioCliAsyncSession = detail::GenericCliAsyncSession; } 38 | 39 | #endif // CLI_BOOSTASIOCLIASYNCSESSION_H_ 40 | 41 | -------------------------------------------------------------------------------- /include/cli/boostasioremotecli.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOREMOTECLI_H_ 31 | #define CLI_BOOSTASIOREMOTECLI_H_ 32 | 33 | #include "detail/boostasiolib.h" 34 | #include "detail/genericasioremotecli.h" 35 | 36 | namespace cli { using BoostAsioCliTelnetServer = detail::CliGenericTelnetServer; } 37 | 38 | #endif // CLI_BOOSTASIOREMOTECLI_H_ 39 | 40 | -------------------------------------------------------------------------------- /include/cli/boostasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOSCHEDULER_H_ 31 | #define CLI_BOOSTASIOSCHEDULER_H_ 32 | 33 | #include "detail/genericasioscheduler.h" 34 | #include "detail/boostasiolib.h" 35 | 36 | namespace cli { using BoostAsioScheduler = detail::GenericAsioScheduler; } 37 | 38 | #endif // CLI_BOOSTASIOSCHEDULER_H_ 39 | -------------------------------------------------------------------------------- /include/cli/clifilesession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_CLIFILESESSION_H 31 | #define CLI_CLIFILESESSION_H 32 | 33 | #include 34 | #include 35 | #include // std::invalid_argument 36 | #include "cli.h" // CliSession 37 | 38 | namespace cli 39 | { 40 | 41 | class CliFileSession : public CliSession 42 | { 43 | public: 44 | /// @throw std::invalid_argument if @c _in or @c out are invalid streams 45 | explicit CliFileSession(Cli& _cli, std::istream& _in=std::cin, std::ostream& _out=std::cout) : 46 | CliSession(_cli, _out, 1), 47 | exit(false), 48 | in(_in) 49 | { 50 | if (!_in.good()) throw std::invalid_argument("istream invalid"); 51 | if (!_out.good()) throw std::invalid_argument("ostream invalid"); 52 | ExitAction( 53 | [this](std::ostream&) noexcept 54 | { 55 | exit = true; 56 | } 57 | ); 58 | } 59 | void Start() 60 | { 61 | Enter(); 62 | 63 | while(!exit) 64 | { 65 | Prompt(); 66 | std::string line; 67 | if (!in.good()) 68 | Exit(); 69 | std::getline(in, line); 70 | if (in.eof()) 71 | Exit(); 72 | else 73 | Feed(line); 74 | } 75 | } 76 | 77 | private: 78 | bool exit; 79 | std::istream& in; 80 | }; 81 | 82 | } // namespace cli 83 | 84 | #endif // CLI_CLIFILESESSION_H 85 | 86 | -------------------------------------------------------------------------------- /include/cli/clilocalsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_CLILOCALSESSION_H 31 | #define CLI_CLILOCALSESSION_H 32 | 33 | #include // std::ostream 34 | #include "detail/commandprocessor.h" 35 | #include "cli.h" // CliSession 36 | #include "detail/keyboard.h" 37 | #include "detail/screen.h" 38 | 39 | namespace cli 40 | { 41 | 42 | class Scheduler; // forward declaration 43 | 44 | /** 45 | * @brief CliLocalTerminalSession represents a local session. 46 | * You should instantiate it to start an interactive prompt on the standard 47 | * input/output of your application. 48 | * The handlers of the commands will be invoked in the same thread the @c Scheduler runs. 49 | */ 50 | class CliLocalTerminalSession : public CliSession 51 | { 52 | public: 53 | 54 | /** 55 | * @brief Construct a new Cli Local Terminal Session object that uses the specified @c std::ostream 56 | * for output. You can also specify a size for the command history. 57 | * 58 | * @param _cli The cli object that defines the menu hierarchy for this session 59 | * @param scheduler The scheduler that will process the command handlers 60 | * @param _out the output stream where command output will be printed 61 | * @param historySize the size of the command history 62 | */ 63 | CliLocalTerminalSession(Cli& _cli, Scheduler& scheduler, std::ostream& _out, std::size_t historySize = 100) : 64 | CliSession(_cli, _out, historySize), 65 | kb(scheduler), 66 | ih(*this, kb) 67 | { 68 | Enter(); 69 | Prompt(); 70 | } 71 | 72 | private: 73 | detail::Keyboard kb; 74 | detail::CommandProcessor ih; 75 | }; 76 | 77 | using CliLocalSession = CliLocalTerminalSession; 78 | 79 | } // namespace cli 80 | 81 | #endif // CLI_CLILOCALSESSION_H 82 | 83 | -------------------------------------------------------------------------------- /include/cli/colorprofile.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_COLORPROFILE_H_ 31 | #define CLI_COLORPROFILE_H_ 32 | 33 | #include "detail/rang.h" 34 | 35 | namespace cli 36 | { 37 | 38 | inline bool& Color() { static bool color; return color; } 39 | 40 | inline void SetColor() { Color() = true; } 41 | inline void SetNoColor() { Color() = false; } 42 | 43 | enum BeforePrompt { beforePrompt }; 44 | enum AfterPrompt { afterPrompt }; 45 | enum BeforeInput { beforeInput }; 46 | enum AfterInput { afterInput }; 47 | 48 | inline std::ostream& operator<<(std::ostream& os, BeforePrompt) 49 | { 50 | if ( Color() ) { os << detail::rang::control::forceColor << detail::rang::fg::green << detail::rang::style::bold; } 51 | return os; 52 | } 53 | 54 | inline std::ostream& operator<<(std::ostream& os, AfterPrompt) 55 | { 56 | os << detail::rang::style::reset; 57 | return os; 58 | } 59 | 60 | inline std::ostream& operator<<(std::ostream& os, BeforeInput) 61 | { 62 | if ( Color() ) { os << detail::rang::control::forceColor << detail::rang::fgB::gray; } 63 | return os; 64 | } 65 | 66 | inline std::ostream& operator<<(std::ostream& os, AfterInput) 67 | { 68 | os << detail::rang::style::reset; 69 | return os; 70 | } 71 | 72 | } // namespace cli 73 | 74 | #endif // CLI_COLORPROFILE_H_ 75 | 76 | 77 | -------------------------------------------------------------------------------- /include/cli/detail/boostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_BOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_BOOSTASIOLIB_H_ 32 | 33 | /** 34 | * This header file provides the class `cli::BoostAsioLib`, using the right 35 | * implementation according to the version of boost libraries included. 36 | */ 37 | 38 | #include 39 | 40 | #if BOOST_VERSION < 106600 41 | #include "oldboostasiolib.h" 42 | namespace cli { namespace detail { using BoostAsioLib = OldBoostAsioLib; } } 43 | #else 44 | #include "newboostasiolib.h" 45 | namespace cli { namespace detail { using BoostAsioLib = NewBoostAsioLib; } } 46 | #endif 47 | 48 | #endif // CLI_DETAIL_BOOSTASIOLIB_H_ 49 | 50 | -------------------------------------------------------------------------------- /include/cli/detail/commandprocessor.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_COMMANDPROCESSOR_H_ 31 | #define CLI_DETAIL_COMMANDPROCESSOR_H_ 32 | 33 | #include 34 | #include 35 | #include "terminal.h" 36 | #include "inputdevice.h" 37 | #include "../cli.h" // CliSession 38 | #include "commonprefix.h" 39 | 40 | namespace cli 41 | { 42 | namespace detail 43 | { 44 | 45 | /** 46 | * @class CommandProcessor 47 | * @brief This class handles user input and processes commands in a CLI session. 48 | * 49 | * The CommandProcessor class is responsible for handling user input from an InputDevice, 50 | * processing the input into commands, and executing these commands in a CLI session. 51 | * It also provides functionality for command history navigation and command auto-completion. 52 | * 53 | * @tparam SCREEN The type of the terminal screen. 54 | */ 55 | template 56 | class CommandProcessor 57 | { 58 | public: 59 | /** 60 | * @brief Construct a new CommandProcessor object. 61 | * 62 | * @param _session The CLI session to be managed. 63 | * @param _kb The input device to be used. 64 | */ 65 | CommandProcessor(CliSession& _session, InputDevice& _kb) : 66 | session(_session), 67 | terminal(session.OutStream()), 68 | kb(_kb) 69 | { 70 | kb.Register( [this](auto key){ this->Keypressed(key); } ); 71 | } 72 | 73 | private: 74 | 75 | /** 76 | * @brief Handle a keypress event. 77 | * 78 | * @param k The key that was pressed. 79 | */ 80 | void Keypressed(std::pair k) 81 | { 82 | const std::pair s = terminal.Keypressed(k); 83 | NewCommand(s); 84 | } 85 | 86 | /** 87 | * @brief Process a new command. 88 | * 89 | * @param s The symbol and string representing the command. 90 | */ 91 | void NewCommand(const std::pair& s) 92 | { 93 | switch (s.first) 94 | { 95 | case Symbol::nothing: 96 | { 97 | break; 98 | } 99 | case Symbol::eof: 100 | { 101 | session.Exit(); 102 | break; 103 | } 104 | case Symbol::command: 105 | { 106 | kb.DeactivateInput(); 107 | session.Feed(s.second); 108 | session.Prompt(); 109 | kb.ActivateInput(); 110 | break; 111 | } 112 | case Symbol::down: 113 | { 114 | terminal.SetLine(session.NextCmd()); 115 | break; 116 | } 117 | case Symbol::up: 118 | { 119 | auto line = terminal.GetLine(); 120 | terminal.SetLine(session.PreviousCmd(line)); 121 | break; 122 | } 123 | case Symbol::tab: 124 | { 125 | auto line = terminal.GetLine(); 126 | auto completions = session.GetCompletions(line); 127 | 128 | if (completions.empty()) 129 | break; 130 | if (completions.size() == 1) 131 | { 132 | terminal.SetLine(completions[0]+' '); 133 | break; 134 | } 135 | 136 | auto commonPrefix = CommonPrefix(completions); 137 | if (commonPrefix.size() > line.size()) 138 | { 139 | terminal.SetLine(commonPrefix); 140 | break; 141 | } 142 | session.OutStream() << '\n'; 143 | std::string items; 144 | std::for_each( completions.begin(), completions.end(), [&items](auto& cmd){ items += '\t' + cmd; } ); 145 | session.OutStream() << items << '\n'; 146 | session.Prompt(); 147 | terminal.ResetCursor(); 148 | terminal.SetLine( line ); 149 | break; 150 | } 151 | case Symbol::clear: 152 | { 153 | const auto currentLine = terminal.GetLine(); 154 | terminal.Clear(); 155 | session.Prompt(); 156 | terminal.ResetCursor(); 157 | terminal.SetLine(currentLine); 158 | break; 159 | } 160 | } 161 | 162 | } 163 | 164 | CliSession& session; 165 | Terminal terminal; 166 | InputDevice& kb; 167 | }; 168 | 169 | } // namespace detail 170 | } // namespace cli 171 | 172 | #endif // CLI_DETAIL_COMMANDPROCESSOR_H_ 173 | 174 | 175 | -------------------------------------------------------------------------------- /include/cli/detail/commonprefix.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_COMMONPREFIX_H_ 31 | #define CLI_DETAIL_COMMONPREFIX_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | namespace cli 39 | { 40 | namespace detail 41 | { 42 | 43 | inline std::string CommonPrefix(const std::vector& v) 44 | { 45 | assert(!v.empty()); 46 | std::string prefix; 47 | 48 | // find the shorter string 49 | auto smin = std::min_element(v.begin(), v.end(), 50 | [] (const std::string& s1, const std::string& s2) 51 | { 52 | return s1.size() < s2.size(); 53 | }); 54 | 55 | for (std::size_t i = 0; i < smin->size(); ++i) 56 | { 57 | // check if i-th element is equal in each input string 58 | const char c = (*smin)[i]; 59 | for (auto& x: v) 60 | if (x[i] != c) return prefix; 61 | prefix += c; 62 | } 63 | 64 | return prefix; 65 | } 66 | 67 | } // namespace detail 68 | } // namespace cli 69 | 70 | #endif // CLI_DETAIL_COMMONPREFIX_H_ 71 | -------------------------------------------------------------------------------- /include/cli/detail/genericasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_GENERICASIOSCHEDULER_H_ 31 | #define CLI_DETAIL_GENERICASIOSCHEDULER_H_ 32 | 33 | #include "../scheduler.h" 34 | #include // unique_ptr 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | template 42 | class GenericAsioScheduler : public Scheduler 43 | { 44 | public: 45 | 46 | using ContextType = typename ASIOLIB::ContextType; 47 | using WorkGuard = typename ASIOLIB::WorkGuard; 48 | 49 | GenericAsioScheduler() : 50 | owned{true}, 51 | context{new ContextType()}, 52 | executor{*context}, 53 | work{std::make_unique(ASIOLIB::MakeWorkGuard(*context))} 54 | {} 55 | 56 | explicit GenericAsioScheduler(ContextType& _context) : context{&_context}, executor{*context} {} 57 | 58 | ~GenericAsioScheduler() override 59 | { 60 | if (owned) 61 | { 62 | work.reset(); // work uses context, so it must be deleted before context 63 | delete context; 64 | } 65 | } 66 | 67 | // non copyable 68 | GenericAsioScheduler(const GenericAsioScheduler&) = delete; 69 | GenericAsioScheduler& operator=(const GenericAsioScheduler&) = delete; 70 | 71 | void Stop() 72 | { 73 | if (work) 74 | ASIOLIB::Reset(*work); 75 | context->stop(); 76 | } 77 | 78 | void Run() 79 | { 80 | context->run(); 81 | } 82 | 83 | bool Stopped() const 84 | { 85 | return context->stopped(); 86 | } 87 | 88 | void ExecOne() { context->run_one(); } 89 | 90 | void PollOne() { context->poll_one(); } 91 | 92 | void Post(const std::function& f) override 93 | { 94 | executor.Post(f); 95 | } 96 | 97 | ContextType& AsioContext() { return *context; } 98 | 99 | private: 100 | 101 | using ExecutorType = typename ASIOLIB::Executor; 102 | 103 | bool owned = false; 104 | ContextType* context; 105 | ExecutorType executor; 106 | std::unique_ptr work; 107 | }; 108 | 109 | 110 | } // namespace detail 111 | } // namespace cli 112 | 113 | #endif // CLI_DETAIL_GENERICASIOSCHEDULER_H_ 114 | -------------------------------------------------------------------------------- /include/cli/detail/genericcliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 31 | #define CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 32 | 33 | #include 34 | #include "../cli.h" // CliSession 35 | #include "genericasioscheduler.h" 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | template 43 | class GenericCliAsyncSession : public CliSession 44 | { 45 | public: 46 | GenericCliAsyncSession(GenericAsioScheduler& _scheduler, Cli& _cli) : 47 | CliSession(_cli, std::cout, 1), 48 | input(_scheduler.AsioContext(), ::dup(STDIN_FILENO)) 49 | { 50 | Read(); 51 | } 52 | ~GenericCliAsyncSession() noexcept override 53 | { 54 | try { input.close(); } catch (const std::exception&) { /* do nothing */ } 55 | } 56 | 57 | private: 58 | 59 | void Read() 60 | { 61 | Prompt(); 62 | // Read a line of input entered by the user. 63 | asiolib::async_read_until( 64 | input, 65 | inputBuffer, 66 | '\n', 67 | std::bind( &GenericCliAsyncSession::NewLine, this, 68 | std::placeholders::_1, 69 | std::placeholders::_2 ) 70 | ); 71 | } 72 | 73 | void NewLine(const asiolibec::error_code& error, std::size_t length ) 74 | { 75 | if ( !error || error == asiolib::error::not_found ) 76 | { 77 | auto bufs = inputBuffer.data(); 78 | auto size = static_cast(length); 79 | if ( !error ) --size; // remove \n 80 | std::string s(asiolib::buffers_begin( bufs ), asiolib::buffers_begin( bufs ) + size); 81 | inputBuffer.consume( length ); 82 | 83 | Feed( s ); 84 | Read(); 85 | } 86 | else 87 | { 88 | input.close(); 89 | } 90 | } 91 | 92 | asiolib::streambuf inputBuffer; 93 | asiolib::posix::stream_descriptor input; 94 | }; 95 | 96 | } // namespace detail 97 | } // namespace cli 98 | 99 | #endif // CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 100 | 101 | -------------------------------------------------------------------------------- /include/cli/detail/inputdevice.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_INPUTDEVICE_H_ 31 | #define CLI_DETAIL_INPUTDEVICE_H_ 32 | 33 | #include 34 | #include 35 | #include "../scheduler.h" 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | enum class KeyType { ascii, up, down, left, right, backspace, canc, home, end, ret, eof, ignored, clear, }; 43 | 44 | class InputDevice 45 | { 46 | public: 47 | using Handler = std::function< void( std::pair ) >; 48 | 49 | explicit InputDevice(Scheduler& _scheduler) : scheduler(_scheduler) {} 50 | virtual ~InputDevice() = default; 51 | virtual void ActivateInput() {} 52 | virtual void DeactivateInput() {} 53 | 54 | template 55 | void Register(H&& h) { handler = std::forward(h); } 56 | 57 | protected: 58 | 59 | void Notify(std::pair k) 60 | { 61 | scheduler.Post([this,k](){ if (handler) handler(k); }); 62 | } 63 | 64 | private: 65 | 66 | Scheduler& scheduler; 67 | Handler handler; 68 | }; 69 | 70 | } // namespace detail 71 | } // namespace cli 72 | 73 | #endif // CLI_DETAIL_INPUTDEVICE_H_ 74 | 75 | -------------------------------------------------------------------------------- /include/cli/detail/keyboard.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_KEYBOARD_H_ 31 | #define CLI_DETAIL_KEYBOARD_H_ 32 | 33 | #include "platform.h" 34 | 35 | #if defined(CLI_OS_LINUX) || defined(CLI_OS_MAC) 36 | #include "linuxkeyboard.h" 37 | namespace cli { namespace detail { using Keyboard = LinuxKeyboard; } } 38 | #elif defined(CLI_OS_WIN) 39 | #include "winkeyboard.h" 40 | namespace cli { namespace detail { using Keyboard = WinKeyboard; } } 41 | #else 42 | #error "Platform not supported (yet)." 43 | #endif 44 | 45 | #endif // CLI_DETAIL_KEYBOARD_H_ 46 | 47 | -------------------------------------------------------------------------------- /include/cli/detail/newboostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_NEWBOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_NEWBOOSTASIOLIB_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | namespace asiolib = boost::asio; 42 | namespace asiolibec = boost::system; 43 | 44 | class NewBoostAsioLib 45 | { 46 | public: 47 | 48 | using ContextType = boost::asio::io_context; 49 | using WorkGuard = boost::asio::executor_work_guard; 50 | 51 | class Executor 52 | { 53 | public: 54 | explicit Executor(ContextType& ios) : 55 | executor(ios.get_executor()) {} 56 | explicit Executor(boost::asio::ip::tcp::socket& socket) : 57 | executor(socket.get_executor()) {} 58 | template void Post(T&& t) { boost::asio::post(executor, std::forward(t)); } 59 | private: 60 | #if BOOST_VERSION >= 107400 61 | using AsioExecutor = boost::asio::any_io_executor; 62 | #else 63 | using AsioExecutor = boost::asio::executor; 64 | #endif 65 | AsioExecutor executor; 66 | }; 67 | 68 | static boost::asio::ip::address IpAddressFromString(const std::string& address) 69 | { 70 | return boost::asio::ip::make_address(address); 71 | } 72 | 73 | static auto MakeWorkGuard(ContextType& context) 74 | { 75 | return boost::asio::make_work_guard(context); 76 | } 77 | 78 | static void Reset(WorkGuard& wg) 79 | { 80 | wg.reset(); 81 | } 82 | 83 | }; 84 | 85 | } // namespace detail 86 | } // namespace cli 87 | 88 | #endif // CLI_DETAIL_NEWBOOSTASIOLIB_H_ 89 | 90 | -------------------------------------------------------------------------------- /include/cli/detail/newstandaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | namespace asiolib = asio; 42 | namespace asiolibec = asio; 43 | 44 | class NewStandaloneAsioLib 45 | { 46 | public: 47 | 48 | using ContextType = asio::io_context; 49 | using WorkGuard = asio::executor_work_guard; 50 | 51 | class Executor 52 | { 53 | public: 54 | explicit Executor(ContextType& ios) : 55 | executor(ios.get_executor()) {} 56 | explicit Executor(asio::ip::tcp::socket& socket) : 57 | executor(socket.get_executor()) {} 58 | template void Post(T&& t) { asio::post(executor, std::forward(t)); } 59 | private: 60 | #if ASIO_VERSION >= 101700 61 | using AsioExecutor = asio::any_io_executor; 62 | #else 63 | using AsioExecutor = asio::executor; 64 | #endif 65 | AsioExecutor executor; 66 | }; 67 | 68 | static asio::ip::address IpAddressFromString(const std::string& address) 69 | { 70 | return asio::ip::make_address(address); 71 | } 72 | 73 | static auto MakeWorkGuard(ContextType& context) 74 | { 75 | return asio::make_work_guard(context); 76 | } 77 | 78 | static void Reset(WorkGuard& wg) 79 | { 80 | wg.reset(); 81 | } 82 | 83 | }; 84 | 85 | } // namespace detail 86 | } // namespace cli 87 | 88 | #endif // CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 89 | 90 | -------------------------------------------------------------------------------- /include/cli/detail/oldboostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_OLDBOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_OLDBOOSTASIOLIB_H_ 32 | 33 | #include 34 | 35 | namespace cli 36 | { 37 | namespace detail 38 | { 39 | 40 | namespace asiolib = boost::asio; 41 | namespace asiolibec = boost::system; 42 | 43 | class OldBoostAsioLib 44 | { 45 | public: 46 | using ContextType = boost::asio::io_service; 47 | using WorkGuard = boost::asio::io_service::work; 48 | 49 | class Executor 50 | { 51 | public: 52 | explicit Executor(ContextType& _ios) : 53 | ios(_ios) {} 54 | explicit Executor(boost::asio::ip::tcp::socket& socket) : 55 | ios(socket.get_io_service()) {} 56 | template void Post(T&& t) { ios.post(std::forward(t)); } 57 | private: 58 | ContextType& ios; 59 | }; 60 | 61 | static boost::asio::ip::address IpAddressFromString(const std::string& address) 62 | { 63 | return boost::asio::ip::address::from_string(address); 64 | } 65 | 66 | static auto MakeWorkGuard(ContextType& context) 67 | { 68 | boost::asio::io_service::work work(context); 69 | return work; 70 | } 71 | 72 | static void Reset(WorkGuard& /*wg*/) 73 | { 74 | } 75 | 76 | }; 77 | 78 | } // namespace detail 79 | } // namespace cli 80 | 81 | #endif // CLI_DETAIL_OLDBOOSTASIOLIB_H_ 82 | 83 | -------------------------------------------------------------------------------- /include/cli/detail/oldstandaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 32 | 33 | #define ASIO_STANDALONE 1 34 | 35 | #include 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | namespace asiolib = asio; 43 | namespace asiolibec = asio; 44 | 45 | class OldStandaloneAsioLib 46 | { 47 | public: 48 | 49 | using ContextType = asio::io_service; 50 | using WorkGuard = asio::io_service::work; 51 | 52 | class Executor 53 | { 54 | public: 55 | explicit Executor(ContextType& _ios) : 56 | ios(_ios) {} 57 | explicit Executor(asio::ip::tcp::socket& socket) : 58 | ios(socket.get_io_service()) {} 59 | template void Post(T&& t) { ios.post(std::forward(t)); } 60 | private: 61 | ContextType& ios; 62 | }; 63 | 64 | static asio::ip::address IpAddressFromString(const std::string& address) 65 | { 66 | return asio::ip::address::from_string(address); 67 | } 68 | 69 | static auto MakeWorkGuard(ContextType& context) 70 | { 71 | asio::io_service::work work(context); 72 | return work; 73 | } 74 | 75 | static void Reset(WorkGuard& /*wg*/) 76 | { 77 | } 78 | 79 | }; 80 | 81 | } // namespace detail 82 | } // namespace cli 83 | 84 | #endif // CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 85 | 86 | -------------------------------------------------------------------------------- /include/cli/detail/platform.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2024 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_PLATFORM_H_ 31 | #define CLI_DETAIL_PLATFORM_H_ 32 | 33 | #if defined(__unix__) || defined(__unix) || defined(__linux__) 34 | #define CLI_OS_LINUX 35 | #elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) 36 | #define CLI_OS_WIN 37 | #elif defined(__APPLE__) || defined(__MACH__) 38 | #define CLI_OS_MAC 39 | #else 40 | #error "Platform not supported (yet)." 41 | #endif 42 | 43 | #endif // CLI_DETAIL_PLATFORM_H_ 44 | 45 | -------------------------------------------------------------------------------- /include/cli/detail/screen.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2024 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_SCREEN_H_ 31 | #define CLI_DETAIL_SCREEN_H_ 32 | 33 | #include "platform.h" 34 | #include "telnetscreen.h" 35 | 36 | #if defined(CLI_OS_LINUX) || defined(CLI_OS_MAC) 37 | namespace cli { namespace detail { using LocalScreen = TelnetScreen; } } 38 | #elif defined(CLI_OS_WIN) 39 | #include "winscreen.h" 40 | namespace cli { namespace detail { using LocalScreen = WinScreen; } } 41 | #else 42 | #error "Platform not supported (yet)." 43 | #endif 44 | 45 | #endif // CLI_DETAIL_SCREEN_H_ 46 | 47 | -------------------------------------------------------------------------------- /include/cli/detail/server.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_SERVER_H_ 31 | #define CLI_DETAIL_SERVER_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | class Session : public std::enable_shared_from_this, public std::streambuf 42 | { 43 | public: 44 | ~Session() override = default; 45 | virtual void Start() 46 | { 47 | OnConnect(); 48 | Read(); 49 | } 50 | 51 | protected: 52 | 53 | explicit Session(asiolib::ip::tcp::socket _socket) : socket(std::move(_socket)), outStream( this ) {} 54 | 55 | virtual void Disconnect() 56 | { 57 | socket.shutdown(asiolib::ip::tcp::socket::shutdown_both); 58 | socket.close(); 59 | } 60 | 61 | virtual void Read() 62 | { 63 | auto self( shared_from_this() ); 64 | socket.async_read_some(asiolib::buffer( data, max_length ), 65 | [ this, self ]( asiolibec::error_code ec, std::size_t length ) 66 | { 67 | if ( !socket.is_open() || ( ec == asiolib::error::eof ) || ( ec == asiolib::error::connection_reset ) ) 68 | OnDisconnect(); 69 | else if ( ec ) 70 | OnError(); 71 | else 72 | { 73 | OnDataReceived( std::string( data, length )); 74 | Read(); 75 | } 76 | }); 77 | } 78 | 79 | virtual void Send(const std::string& msg) 80 | { 81 | asiolibec::error_code ec; 82 | asiolib::write(socket, asiolib::buffer(msg), ec); 83 | if ((ec == asiolib::error::eof) || (ec == asiolib::error::connection_reset)) 84 | OnDisconnect(); 85 | else if (ec) 86 | OnError(); 87 | } 88 | 89 | virtual std::ostream& OutStream() { return outStream; } 90 | 91 | virtual void OnConnect() = 0; 92 | virtual void OnDisconnect() = 0; 93 | virtual void OnError() = 0; 94 | virtual void OnDataReceived(const std::string& _data) = 0; 95 | 96 | virtual std::string Encode(const std::string& _data) const { return _data; } 97 | 98 | private: 99 | 100 | // std::streambuf 101 | std::streamsize xsputn( const char* s, std::streamsize n ) override 102 | { 103 | Send(Encode(std::string(s, s+n))); 104 | return n; 105 | } 106 | int overflow( int c ) override 107 | { 108 | Send(Encode(std::string(1, static_cast< char >(c)))); 109 | return c; 110 | } 111 | 112 | asiolib::ip::tcp::socket socket; 113 | enum { max_length = 1024 }; 114 | char data[ max_length ]; 115 | std::ostream outStream; 116 | }; 117 | 118 | 119 | template 120 | class Server 121 | { 122 | public: 123 | // disable value semantics 124 | Server( const Server& ) = delete; 125 | Server& operator = ( const Server& ) = delete; 126 | 127 | Server(typename ASIOLIB::ContextType& ios, unsigned short port) : 128 | acceptor(ios, asiolib::ip::tcp::endpoint(asiolib::ip::tcp::v4(), port)) 129 | { 130 | Accept(); 131 | } 132 | Server(typename ASIOLIB::ContextType& ios, std::string address, unsigned short port) : 133 | acceptor(ios, asiolib::ip::tcp::endpoint(ASIOLIB::IpAddressFromString(address), port)) 134 | { 135 | Accept(); 136 | } 137 | virtual ~Server() = default; 138 | // returns shared_ptr instead of unique_ptr because Session needs to use enable_shared_from_this 139 | virtual std::shared_ptr CreateSession(asiolib::ip::tcp::socket socket) = 0; 140 | private: 141 | void Accept() 142 | { 143 | acceptor.async_accept([this](asiolibec::error_code ec, asiolib::ip::tcp::socket socket) 144 | { 145 | if (!ec) CreateSession(std::move(socket))->Start(); 146 | Accept(); 147 | }); 148 | } 149 | asiolib::ip::tcp::acceptor acceptor; 150 | }; 151 | 152 | 153 | } // namespace detail 154 | } // namespace cli 155 | 156 | #endif // CLI_DETAIL_SERVER_H_ 157 | 158 | -------------------------------------------------------------------------------- /include/cli/detail/standaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_STANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_STANDALONEASIOLIB_H_ 32 | 33 | /** 34 | * This header file provides the class `cli::StandaloneAsioLib`, using the right 35 | * implementation according to the version of asio libraries included. 36 | */ 37 | 38 | #include 39 | 40 | #if ASIO_VERSION < 101300 41 | #include "oldstandaloneasiolib.h" 42 | namespace cli { namespace detail { using StandaloneAsioLib = OldStandaloneAsioLib; } } 43 | #else 44 | #include "newstandaloneasiolib.h" 45 | namespace cli { namespace detail { using StandaloneAsioLib = NewStandaloneAsioLib; } } 46 | #endif 47 | 48 | #endif // CLI_DETAIL_STANDALONEASIOLIB_H_ 49 | 50 | -------------------------------------------------------------------------------- /include/cli/detail/telnetscreen.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2024 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_TELNETSCREEN_H_ 31 | #define CLI_DETAIL_TELNETSCREEN_H_ 32 | 33 | #include 34 | 35 | namespace cli 36 | { 37 | namespace detail 38 | { 39 | 40 | struct TelnetScreen 41 | { 42 | static void Clear(std::ostream& out) { out << "\033[H\033[J" << std::flush; } 43 | }; 44 | 45 | } // namespace detail 46 | } // namespace cli 47 | 48 | #endif // CLI_DETAIL_TELNETSCREEN_H_ 49 | -------------------------------------------------------------------------------- /include/cli/detail/winkeyboard.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_WINKEYBOARD_H_ 31 | #define CLI_DETAIL_WINKEYBOARD_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "inputdevice.h" 41 | 42 | #if !defined(NOMINMAX) 43 | #define NOMINMAX 1 // prevent windows from defining min and max macros 44 | #endif // !defined(NOMINMAX) 45 | #include 46 | 47 | namespace cli 48 | { 49 | namespace detail 50 | { 51 | 52 | class InputSource 53 | { 54 | public: 55 | 56 | InputSource() 57 | { 58 | events[0] = CreateEvent(nullptr, FALSE, FALSE, nullptr); // Obtain a Windows handle to use to stop 59 | events[1] = GetStdHandle(STD_INPUT_HANDLE); // Get a Windows handle to the keyboard input 60 | } 61 | 62 | void WaitKbHit() 63 | { 64 | // Wait for either the timer to expire or a key press event 65 | DWORD dwResult = WaitForMultipleObjects(2, events, false, INFINITE); 66 | 67 | if (dwResult == WAIT_FAILED) 68 | { 69 | // TODO 70 | assert(false); 71 | } 72 | else 73 | { 74 | if (dwResult == WAIT_OBJECT_0) // WAIT_OBJECT_0 corresponds to the stop event 75 | { 76 | throw std::runtime_error("InputSource stop"); 77 | } 78 | else 79 | { 80 | return; 81 | } 82 | } 83 | 84 | // we can't reach this point 85 | assert(false); 86 | } 87 | 88 | void Stop() 89 | { 90 | SetEvent(events[0]); 91 | } 92 | 93 | private: 94 | HANDLE events[2]; 95 | }; 96 | 97 | // 98 | 99 | class WinKeyboard : public InputDevice 100 | { 101 | public: 102 | explicit WinKeyboard(Scheduler& _scheduler) : 103 | InputDevice(_scheduler), 104 | servant([this]() noexcept { Read(); }) 105 | { 106 | } 107 | ~WinKeyboard() override 108 | { 109 | is.Stop(); 110 | servant.join(); 111 | } 112 | 113 | private: 114 | 115 | void Read() noexcept 116 | { 117 | try 118 | { 119 | while (true) 120 | { 121 | auto k = Get(); 122 | Notify(k); 123 | } 124 | } 125 | catch (const std::exception&) 126 | { 127 | // nothing to do: just exit 128 | } 129 | } 130 | 131 | std::pair Get() 132 | { 133 | is.WaitKbHit(); 134 | 135 | int c = _getch(); 136 | switch (c) 137 | { 138 | case EOF: 139 | case 4: // EOT ie CTRL-D 140 | case 26: // CTRL-Z 141 | case 3: // CTRL-C 142 | return std::make_pair(KeyType::eof, ' '); 143 | break; 144 | 145 | case 224: // symbol 146 | { 147 | c = _getch(); 148 | switch (c) 149 | { 150 | case 72: return std::make_pair(KeyType::up, ' '); 151 | case 80: return std::make_pair(KeyType::down, ' '); 152 | case 75: return std::make_pair(KeyType::left, ' '); 153 | case 77: return std::make_pair(KeyType::right, ' '); 154 | case 71: return std::make_pair(KeyType::home, ' '); 155 | case 79: return std::make_pair(KeyType::end, ' '); 156 | case 83: return std::make_pair(KeyType::canc, ' '); 157 | default: return std::make_pair(KeyType::ignored, ' '); 158 | } 159 | } 160 | case 8: 161 | return std::make_pair(KeyType::backspace, c); 162 | break; 163 | case 12: // CTRL-L 164 | return std::make_pair(KeyType::clear, ' '); 165 | break; 166 | case 13: 167 | return std::make_pair(KeyType::ret, c); 168 | break; 169 | default: // hopefully ascii 170 | { 171 | const char ch = static_cast(c); 172 | return std::make_pair(KeyType::ascii, ch); 173 | } 174 | } 175 | return std::make_pair(KeyType::ignored, ' '); 176 | } 177 | 178 | InputSource is; 179 | std::thread servant; 180 | }; 181 | 182 | } // namespace detail 183 | } // namespace cli 184 | 185 | #endif // CLI_DETAIL_WINKEYBOARD_H_ 186 | -------------------------------------------------------------------------------- /include/cli/detail/winscreen.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2024 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_WINSCREEN_H_ 31 | #define CLI_DETAIL_WINSCREEN_H_ 32 | 33 | #if !defined(NOMINMAX) 34 | #define NOMINMAX 1 // prevent windows from defining min and max macros 35 | #endif // !defined(NOMINMAX) 36 | #include 37 | 38 | namespace cli 39 | { 40 | namespace detail 41 | { 42 | 43 | struct WinScreen 44 | { 45 | static void Clear(std::ostream& /*out*/) 46 | { 47 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); 48 | COORD coord = { 0, 0 }; 49 | DWORD count; 50 | 51 | CONSOLE_SCREEN_BUFFER_INFO csbi; 52 | GetConsoleScreenBufferInfo(hStdOut, &csbi); 53 | 54 | FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count); 55 | SetConsoleCursorPosition(hStdOut, coord); 56 | } 57 | }; 58 | 59 | } // namespace detail 60 | } // namespace cli 61 | 62 | #endif // CLI_DETAIL_WINSCREEN_H_ 63 | -------------------------------------------------------------------------------- /include/cli/filehistorystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_FILEHISTORYSTORAGE_H_ 31 | #define CLI_FILEHISTORYSTORAGE_H_ 32 | 33 | #include "historystorage.h" 34 | #include 35 | #include 36 | 37 | namespace cli 38 | { 39 | 40 | class FileHistoryStorage : public HistoryStorage 41 | { 42 | public: 43 | explicit FileHistoryStorage(std::string _fileName, std::size_t size = 1000) : 44 | maxSize(size), 45 | fileName(std::move(_fileName)) 46 | { 47 | } 48 | void Store(const std::vector& cmds) override 49 | { 50 | using dt = std::vector::difference_type; 51 | auto commands = Commands(); 52 | commands.insert(commands.end(), cmds.begin(), cmds.end()); 53 | if (commands.size() > maxSize) 54 | commands.erase( 55 | commands.begin(), 56 | commands.begin() + static_cast
(commands.size() - maxSize) 57 | ); 58 | std::ofstream f(fileName, std::ios_base::out); 59 | for (const auto& line: commands) 60 | f << line << '\n'; 61 | } 62 | std::vector Commands() const override 63 | { 64 | std::vector commands; 65 | std::ifstream in(fileName); 66 | if (in) 67 | { 68 | std::string line; 69 | while (std::getline(in, line)) 70 | commands.push_back(line); 71 | } 72 | return commands; 73 | } 74 | void Clear() override 75 | { 76 | std::ofstream f(fileName, std::ios_base::out | std::ios_base::trunc); 77 | } 78 | 79 | private: 80 | const std::size_t maxSize; 81 | const std::string fileName; 82 | }; 83 | 84 | } // namespace cli 85 | 86 | #endif // CLI_FILEHISTORYSTORAGE_H_ 87 | -------------------------------------------------------------------------------- /include/cli/historystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_HISTORYSTORAGE_H_ 31 | #define CLI_HISTORYSTORAGE_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | 39 | class HistoryStorage 40 | { 41 | public: 42 | virtual ~HistoryStorage() = default; 43 | // Store a vector of commands in the history storage 44 | virtual void Store(const std::vector& commands) = 0; 45 | // Returns all the commands stored 46 | virtual std::vector Commands() const = 0; 47 | // Clear the whole content of the storage 48 | // After calling this method, Commands() returns the empty vector 49 | virtual void Clear() = 0; 50 | }; 51 | 52 | } // namespace cli 53 | 54 | #endif // CLI_HISTORYSTORAGE_H_ 55 | -------------------------------------------------------------------------------- /include/cli/loopscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_LOOPSCHEDULER_H_ 31 | #define CLI_LOOPSCHEDULER_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "scheduler.h" 38 | 39 | namespace cli 40 | { 41 | 42 | /** 43 | * @brief The LoopScheduler is a simple thread-safe scheduler 44 | * 45 | */ 46 | class LoopScheduler : public Scheduler 47 | { 48 | public: 49 | LoopScheduler() = default; 50 | ~LoopScheduler() override 51 | { 52 | Stop(); 53 | } 54 | 55 | // non copyable 56 | LoopScheduler(const LoopScheduler&) = delete; 57 | LoopScheduler& operator=(const LoopScheduler&) = delete; 58 | 59 | void Stop() 60 | { 61 | std::lock_guard lck (mtx); 62 | running = false; 63 | cv.notify_all(); 64 | } 65 | 66 | void Run() 67 | { 68 | while( ExecOne() ) {}; 69 | } 70 | 71 | bool Stopped() const 72 | { 73 | std::lock_guard lck (mtx); 74 | return !running; 75 | } 76 | 77 | void Post(const std::function& f) override 78 | { 79 | std::lock_guard lck (mtx); 80 | tasks.push(f); 81 | cv.notify_all(); 82 | } 83 | 84 | bool ExecOne() 85 | { 86 | std::function task; 87 | { 88 | std::unique_lock lck(mtx); 89 | cv.wait(lck, [this](){ return !running || !tasks.empty(); }); 90 | if (!running) 91 | return false; 92 | task = tasks.front(); 93 | tasks.pop(); 94 | } 95 | 96 | if (task) 97 | task(); 98 | 99 | return true; 100 | } 101 | 102 | bool PollOne() 103 | { 104 | std::function task; 105 | { 106 | std::lock_guard lck(mtx); 107 | if (!running || tasks.empty()) 108 | return false; 109 | task = tasks.front(); 110 | tasks.pop(); 111 | } 112 | 113 | if (task) 114 | task(); 115 | 116 | return true; 117 | } 118 | 119 | private: 120 | std::queue> tasks; 121 | bool running{ true }; 122 | mutable std::mutex mtx; 123 | std::condition_variable cv; 124 | }; 125 | 126 | } // namespace cli 127 | 128 | #endif // CLI_LOOPSCHEDULER_H_ 129 | -------------------------------------------------------------------------------- /include/cli/scheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_SCHEDULER_H_ 31 | #define CLI_SCHEDULER_H_ 32 | 33 | #include 34 | 35 | namespace cli 36 | { 37 | 38 | /** 39 | * A `Scheduler` represents an engine capable of running a task. 40 | * Its method `Post` can be safely called from any thread to submit the task 41 | * that will execute in an unspecified thread of execution as soon as possible 42 | * (but in any case after the call to `Post` is terminated). 43 | */ 44 | class Scheduler 45 | { 46 | public: 47 | virtual ~Scheduler() = default; 48 | 49 | /// Submits a completion token or function object for execution. 50 | virtual void Post(const std::function& f) = 0; 51 | }; 52 | 53 | } // namespace cli 54 | 55 | #endif // CLI_SCHEDULER_H_ 56 | -------------------------------------------------------------------------------- /include/cli/standaloneasiocliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOCLIASYNCSESSION_H_ 31 | #define CLI_STANDALONEASIOCLIASYNCSESSION_H_ 32 | 33 | #include "detail/genericcliasyncsession.h" 34 | #include "detail/standaloneasiolib.h" 35 | 36 | 37 | namespace cli { using StandaloneAsioCliAsyncSession = detail::GenericCliAsyncSession; } 38 | 39 | #endif // CLI_STANDALONEASIOCLIASYNCSESSION_H_ 40 | 41 | -------------------------------------------------------------------------------- /include/cli/standaloneasioremotecli.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOREMOTECLI_H_ 31 | #define CLI_STANDALONEASIOREMOTECLI_H_ 32 | 33 | #include "detail/standaloneasiolib.h" 34 | #include "detail/genericasioremotecli.h" 35 | 36 | namespace cli { using StandaloneAsioCliTelnetServer = detail::CliGenericTelnetServer; } 37 | 38 | 39 | #endif // CLI_STANDALONEASIOREMOTECLI_H_ 40 | 41 | -------------------------------------------------------------------------------- /include/cli/standaloneasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOSCHEDULER_H_ 31 | #define CLI_STANDALONEASIOSCHEDULER_H_ 32 | 33 | #include "detail/genericasioscheduler.h" 34 | #include "detail/standaloneasiolib.h" 35 | 36 | namespace cli { using StandaloneAsioScheduler = detail::GenericAsioScheduler; } 37 | 38 | #endif // CLI_STANDALONEASIOSCHEDULER_H_ 39 | -------------------------------------------------------------------------------- /include/cli/volatilehistorystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_VOLATILEHISTORYSTORAGE_H_ 31 | #define CLI_VOLATILEHISTORYSTORAGE_H_ 32 | 33 | #include "historystorage.h" 34 | #include 35 | 36 | namespace cli 37 | { 38 | 39 | class VolatileHistoryStorage : public HistoryStorage 40 | { 41 | public: 42 | explicit VolatileHistoryStorage(std::size_t size = 1000) : maxSize(size) {} 43 | void Store(const std::vector& cmds) override 44 | { 45 | using dt = std::deque::difference_type; 46 | commands.insert(commands.end(), cmds.begin(), cmds.end()); 47 | if (commands.size() > maxSize) 48 | commands.erase( 49 | commands.begin(), 50 | commands.begin()+static_cast
(commands.size()-maxSize) 51 | ); 52 | } 53 | std::vector Commands() const override 54 | { 55 | return std::vector(commands.begin(), commands.end()); 56 | } 57 | void Clear() override 58 | { 59 | commands.clear(); 60 | } 61 | private: 62 | const std::size_t maxSize; 63 | std::deque commands; 64 | }; 65 | 66 | } // namespace cli 67 | 68 | #endif // CLI_VOLATILEHISTORYSTORAGE_H_ 69 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | cmake_minimum_required(VERSION 3.8) 31 | project(cli_test) 32 | enable_testing() 33 | 34 | set(Boost_ADDITIONAL_VERSIONS "1.66" "1.66.0") 35 | 36 | set(Boost_NO_BOOST_CMAKE ON) 37 | add_definitions( -DBOOST_ALL_NO_LIB ) # for windows 38 | 39 | # finds boost, triggers an error otherwise 40 | find_package(Boost 1.66 REQUIRED COMPONENTS unit_test_framework system) 41 | 42 | # finds standalone asio, triggers an error otherwise 43 | find_path(STANDALONE_ASIO_INCLUDE_PATH NAMES "asio.hpp" HINTS ${ASIO_INCLUDEDIR}) 44 | mark_as_advanced(STANDALONE_ASIO_INCLUDE_PATH) 45 | add_library(standalone_asio_test INTERFACE) 46 | target_include_directories(standalone_asio_test SYSTEM INTERFACE ${STANDALONE_ASIO_INCLUDE_PATH}) 47 | target_link_libraries(standalone_asio_test INTERFACE Threads::Threads) 48 | 49 | # creates the executable 50 | add_executable( 51 | test_suite 52 | driver.cpp 53 | test_history.cpp 54 | test_volatilehistorystorage.cpp 55 | test_filehistorystorage.cpp 56 | test_split.cpp 57 | test_commonprefix.cpp 58 | test_menu.cpp 59 | test_cli.cpp 60 | test_loopscheduler.cpp 61 | test_standaloneasioscheduler.cpp 62 | test_boostasioscheduler.cpp 63 | ) 64 | # indicates the include paths 65 | target_include_directories(test_suite SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) 66 | # indicates the shared library variant 67 | target_compile_definitions(test_suite PRIVATE "BOOST_TEST_DYN_LINK=1") 68 | # indicates the link paths 69 | target_link_libraries(test_suite ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} Boost::system standalone_asio_test cli::cli) 70 | 71 | # declares a test with our executable 72 | add_test(NAME cli_test COMMAND test_suite) 73 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | # usage: 31 | # make CXXFLAGS="-isystem ${ASIO_INCLUDE_PATH}" BOOST_INC= BOOST_LIB= 32 | # or 33 | # make CXXFLAGS="-isystem " BOOST_INC= BOOST_LIB= CXXFLAGS=-std=c++0x 34 | 35 | override RUN_OPT += --build_info --report_level=short 36 | 37 | override CXXFLAGS += -Wall -Wextra -Werror -I../include -DBOOST_TEST_DYN_LINK -isystem $(BOOST_INC) -std=c++1y 38 | override LDFLAGS += -L$(BOOST_LIB) 39 | override LDLIBS += -lboost_unit_test_framework -lboost_system -ldl -lpthread 40 | 41 | OBJ := test_history.o \ 42 | test_volatilehistorystorage.o \ 43 | test_filehistorystorage.o \ 44 | test_split.o \ 45 | test_commonprefix.o \ 46 | test_menu.o \ 47 | test_cli.o \ 48 | test_loopscheduler.o \ 49 | test_standaloneasioscheduler.o \ 50 | test_boostasioscheduler.o \ 51 | driver.o 52 | 53 | EXE := test_suite 54 | 55 | .PHONY: all test clean 56 | 57 | all: $(EXE) test 58 | 59 | $(EXE): $(OBJ) 60 | $(LINK.cc) $(OBJ) -o $(EXE) $(LDLIBS) 61 | 62 | test: 63 | export LD_LIBRARY_PATH=.:$(BOOST_LIB) ; ./$(EXE) $(RUN_OPT) 64 | 65 | clean: 66 | @- $(RM) *.o *~ core $(EXE) 67 | -------------------------------------------------------------------------------- /test/cli_test_history: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/daniele77/cli/9b0b99f746fa9de3f5ad78a695b5d650738e0676/test/cli_test_history -------------------------------------------------------------------------------- /test/driver.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #define BOOST_TEST_MAIN 31 | #define BOOST_TEST_MODULE CliTest 32 | #include 33 | // #include 34 | -------------------------------------------------------------------------------- /test/makefile.win: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # CLI - A simple command line interface. 3 | # Copyright (C) 2016-2021 Daniele Pallastrelli 4 | # 5 | # Boost Software License - Version 1.0 - August 17th, 2003 6 | # 7 | # Permission is hereby granted, free of charge, to any person or organization 8 | # obtaining a copy of the software and accompanying documentation covered by 9 | # this license (the "Software") to use, reproduce, display, distribute, 10 | # execute, and transmit the Software, and to prepare derivative works of the 11 | # Software, and to permit third-parties to whom the Software is furnished to 12 | # do so, all subject to the following: 13 | # 14 | # The copyright notices in the Software and this entire statement, including 15 | # the above license grant, this restriction and the following disclaimer, 16 | # must be included in all copies of the Software, in whole or in part, and 17 | # all derivative works of the Software, unless such copies or derivative 18 | # works are solely in the form of machine-executable object code generated by 19 | # a source language processor. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | # SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | # FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | # DEALINGS IN THE SOFTWARE. 28 | ################################################################################ 29 | 30 | # usage: 31 | # set BOOST= 32 | # set ASIO= 33 | # nmake /f makefile.win 34 | # or 35 | # nmake /f makefile.win DEBUG=1 36 | # or 37 | # nmake /f makefile.win UNICODE=1 38 | # or 39 | # nmake /f makefile.win DEBUG=1 UNICODE=1 40 | 41 | #define macros 42 | EXE_NAME = test_suite.exe 43 | DIR_INCLUDE = /I..\include /I%BOOST% /I%ASIO% 44 | 45 | !ifdef DEBUG 46 | RUNTIME_LIB = /MDd 47 | !else 48 | RUNTIME_LIB = /MD 49 | !endif 50 | 51 | !ifdef UNICODE 52 | CHAR_SET = /D "_UNICODE" /D "UNICODE" 53 | !else 54 | CHAR_SET = /D "_MBCS" 55 | !endif 56 | 57 | RUN_OPT = --build_info --report_level=short 58 | COMPILE_FLAGS = /nologo /EHsc /DBOOST_TEST_DYN_LINK $(RUNTIME_LIB) $(CHAR_SET) /D_WIN32_WINNT=0x0501 /DBOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE 59 | LINK_FLAGS = /LIBPATH:%BOOST%\stage\lib /NOLOGO 60 | PATH = $(PATH);$(BOOST)\stage\lib\ # to run the test suite 61 | 62 | RM = del /F /Q 2> nul 63 | 64 | CPPFLAGS = $(COMPILE_FLAGS) $(DIR_INCLUDE) 65 | 66 | EXE_OBJ_FILES= \ 67 | test_history.obj \ 68 | test_volatilehistorystorage.obj \ 69 | test_filehistorystorage.obj \ 70 | test_split.obj \ 71 | test_commonprefix.obj \ 72 | test_menu.obj \ 73 | test_cli.obj \ 74 | test_loopscheduler.obj \ 75 | test_standaloneasioscheduler.obj \ 76 | test_boostasioscheduler.obj \ 77 | driver.obj 78 | 79 | .PHONY: all mainapp test clean 80 | 81 | # create directories and build application 82 | all: clean mainapp test 83 | 84 | $(EXE_NAME) : $(EXE_OBJ_FILES) 85 | @echo Linking $(EXE_NAME)... 86 | link $(LINK_FLAGS) /out:$(EXE_NAME) $(EXE_OBJ_FILES) 87 | 88 | # application 89 | 90 | mainapp: $(EXE_NAME) 91 | 92 | # run the test 93 | test: 94 | $(EXE_NAME) $(RUN_OPT) 95 | 96 | # delete output files 97 | clean: 98 | @-$(RM) *.obj 99 | @-$(RM) *.exp 100 | @-$(RM) *.lib 101 | @-$(RM) $(EXE_NAME) 102 | 103 | 104 | -------------------------------------------------------------------------------- /test/scheduler_test_templates.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef SCHEDULER_TEST_TEMPLATES_H_ 31 | #define SCHEDULER_TEST_TEMPLATES_H_ 32 | 33 | #include 34 | #include 35 | 36 | template 37 | void SchedulingTest() 38 | { 39 | S scheduler; 40 | bool done = false; 41 | scheduler.Post( [&done]() noexcept { done = true; } ); 42 | scheduler.ExecOne(); 43 | BOOST_CHECK(done); 44 | } 45 | 46 | template 47 | void SameThreadTest() 48 | { 49 | using namespace std; 50 | 51 | S scheduler; 52 | thread::id runThreadId; 53 | thread::id postThreadId; 54 | thread th( 55 | [&]() 56 | { 57 | postThreadId = this_thread::get_id(); 58 | scheduler.Post( 59 | [&runThreadId]() noexcept 60 | { 61 | runThreadId = this_thread::get_id(); 62 | } 63 | ); 64 | } 65 | ); 66 | th.join(); 67 | scheduler.ExecOne(); 68 | BOOST_CHECK_NE( runThreadId, postThreadId ); 69 | BOOST_CHECK_EQUAL( runThreadId, this_thread::get_id() ); 70 | } 71 | 72 | template 73 | void ExceptionTest() 74 | { 75 | S scheduler; 76 | scheduler.Post( [](){ throw 42; } ); 77 | BOOST_CHECK_THROW( scheduler.ExecOne(), int ); 78 | } 79 | 80 | #endif // SCHEDULER_TEST_TEMPLATES_H_ 81 | -------------------------------------------------------------------------------- /test/test_boostasioscheduler.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include "scheduler_test_templates.h" 31 | #include "cli/boostasioscheduler.h" 32 | 33 | using namespace std; 34 | using namespace cli; 35 | 36 | BOOST_AUTO_TEST_SUITE(BoostAsioSchedulerSuite) 37 | 38 | BOOST_AUTO_TEST_CASE(Basics) 39 | { 40 | SchedulingTest(); 41 | } 42 | 43 | BOOST_AUTO_TEST_CASE(SameThread) 44 | { 45 | SameThreadTest(); 46 | } 47 | 48 | BOOST_AUTO_TEST_CASE(Exceptions) 49 | { 50 | ExceptionTest(); 51 | } 52 | 53 | BOOST_AUTO_TEST_CASE(BoostAsioNonOwner) 54 | { 55 | detail::BoostAsioLib::ContextType ioc; 56 | BoostAsioScheduler scheduler(ioc); 57 | bool done = false; 58 | scheduler.Post( [&done]() noexcept { done = true; } ); 59 | ioc.run_one(); 60 | BOOST_CHECK(done); 61 | } 62 | 63 | BOOST_AUTO_TEST_SUITE_END() 64 | -------------------------------------------------------------------------------- /test/test_commonprefix.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include "cli/detail/commonprefix.h" 32 | 33 | using namespace std; 34 | using namespace cli::detail; 35 | 36 | BOOST_AUTO_TEST_SUITE(CommonPrefixSuite) 37 | 38 | BOOST_AUTO_TEST_CASE(Everything) 39 | { 40 | BOOST_CHECK_EQUAL( CommonPrefix({"foo"}), "foo" ); 41 | BOOST_CHECK_EQUAL( CommonPrefix({"foo", "bar"}), "" ); 42 | BOOST_CHECK_EQUAL( CommonPrefix({"prefix_foo", "prefix_bar"}), "prefix_" ); 43 | BOOST_CHECK_EQUAL( CommonPrefix({"prefix foo", "prefix bar"}), "prefix " ); 44 | } 45 | 46 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/test_filehistorystorage.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include "cli/filehistorystorage.h" 32 | 33 | using namespace cli; 34 | 35 | BOOST_AUTO_TEST_SUITE(FileHistoryStorageSuite) 36 | 37 | BOOST_AUTO_TEST_CASE(Basics) 38 | { 39 | FileHistoryStorage s("cli_test_history", 10); 40 | s.Clear(); // in case the test runs multiple times 41 | 42 | // starts empty 43 | BOOST_CHECK(s.Commands().empty()); 44 | 45 | const std::vector v = { "item1", "item2", "item3", "item4", "item5", "item6" }; 46 | s.Store(v); 47 | auto result = s.Commands(); 48 | BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), result.begin(), result.end()); 49 | 50 | const std::vector v2 = { "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 51 | s.Store(v2); 52 | result = s.Commands(); 53 | const std::vector expected = { "item3", "item4", "item5", "item6", "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 54 | BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), result.begin(), result.end()); 55 | } 56 | 57 | BOOST_AUTO_TEST_CASE(Persistence) 58 | { 59 | FileHistoryStorage s("cli_test_history", 10); 60 | s.Clear(); // in case the test runs multiple times 61 | 62 | // starts empty 63 | BOOST_CHECK(s.Commands().empty()); 64 | 65 | const std::vector v = { "item1", "item2", "item3", "item4", "item5", "item6" }; 66 | s.Store(v); 67 | auto result = s.Commands(); 68 | BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), result.begin(), result.end()); 69 | 70 | const std::vector v2 = { "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 71 | s.Store(v2); 72 | result = s.Commands(); 73 | const std::vector expected = { "item3", "item4", "item5", "item6", "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 74 | BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), result.begin(), result.end()); 75 | 76 | // another object, same file => same result 77 | 78 | FileHistoryStorage s2("cli_test_history", 10); 79 | result = s2.Commands(); 80 | BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), result.begin(), result.end()); 81 | 82 | s2.Clear(); 83 | BOOST_CHECK(s2.Commands().empty()); // check clear 84 | } 85 | 86 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/test_loopscheduler.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include "scheduler_test_templates.h" 31 | #include "cli/loopscheduler.h" 32 | 33 | using namespace std; 34 | using namespace cli; 35 | 36 | BOOST_AUTO_TEST_SUITE(LoopSchedulerSuite) 37 | 38 | BOOST_AUTO_TEST_CASE(Basics) 39 | { 40 | SchedulingTest(); 41 | } 42 | 43 | BOOST_AUTO_TEST_CASE(SameThread) 44 | { 45 | SameThreadTest(); 46 | } 47 | 48 | BOOST_AUTO_TEST_CASE(Exceptions) 49 | { 50 | ExceptionTest(); 51 | } 52 | 53 | BOOST_AUTO_TEST_SUITE_END() 54 | -------------------------------------------------------------------------------- /test/test_menu.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include "cli/cli.h" 32 | 33 | using namespace std; 34 | using namespace cli; 35 | using namespace cli::detail; 36 | 37 | BOOST_AUTO_TEST_SUITE(MenuSuite) 38 | 39 | BOOST_AUTO_TEST_CASE(Basics) 40 | { 41 | Menu menu("menu"); 42 | menu.Insert("aaa", [](ostream&){}); 43 | menu.Insert("bbb", [](ostream&){}); 44 | menu.Insert("aaa_bbb", [](ostream&){}); 45 | menu.Insert("aaa_ccc", [](ostream&){}); 46 | 47 | auto subMenu = make_unique("submenu"); 48 | auto subSubMenu = make_unique("subsubmenu"); 49 | subSubMenu->Insert("bar", [](ostream&){}); 50 | subMenu->Insert(std::move(subSubMenu)); 51 | subMenu->Insert("foo", [](ostream&){}); 52 | menu.Insert(std::move(subMenu)); 53 | 54 | /* 55 | menu 56 | +-- aaa 57 | +-- bbb 58 | +-- aaa_bbb 59 | +-- aaa_ccc 60 | +-- submenu 61 | +-- foo 62 | +-- subsubmenu 63 | +-- bar 64 | */ 65 | 66 | // GetCompletions returns: 67 | // - the completions of this menu command 68 | // - the recursive completions of subcommands 69 | // - the recursive completions of parent menu 70 | 71 | // GetCompletionRecursive returns: 72 | // - the completion of this menu command 73 | // - the recursive completions of the subcommands 74 | 75 | auto completions = menu.GetCompletions(""); 76 | vector expected({"aaa", "bbb", "aaa_bbb", "aaa_ccc", "submenu"}); 77 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 78 | 79 | completions = menu.GetCompletions("aaa"); 80 | expected = {"aaa", "aaa_bbb", "aaa_ccc"}; 81 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 82 | 83 | completions = menu.GetCompletions("aaa_b"); 84 | expected = {"aaa_bbb"}; 85 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 86 | 87 | completions = menu.GetCompletions("b"); 88 | expected = {"bbb"}; 89 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 90 | 91 | completions = menu.GetCompletions("submenu"); 92 | expected = {"submenu subsubmenu", "submenu foo", "submenu menu"}; 93 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 94 | 95 | /* 96 | TODO: unfortunately, this won't work because we have moved subMenu into root menu 97 | 98 | completions = subMenu->GetCompletions(""); 99 | expected = {"menu", "foo", "subsubmenu"}; 100 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 101 | */ 102 | 103 | completions = menu.GetCompletionRecursive(""); 104 | expected = {"menu"}; 105 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 106 | 107 | completions = menu.GetCompletionRecursive("me"); 108 | expected = {"menu"}; 109 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 110 | 111 | completions = menu.GetCompletionRecursive("menu"); 112 | expected = {"menu aaa", "menu bbb", "menu aaa_bbb", "menu aaa_ccc", "menu submenu"}; 113 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 114 | 115 | completions = menu.GetCompletionRecursive("menu a"); 116 | expected = {"menu aaa", "menu aaa_bbb", "menu aaa_ccc"}; 117 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 118 | 119 | completions = menu.GetCompletionRecursive("menu submenu"); 120 | expected = {"menu submenu subsubmenu", "menu submenu foo", "menu submenu menu"}; 121 | BOOST_CHECK_EQUAL_COLLECTIONS(completions.begin(), completions.end(), expected.begin(), expected.end()); 122 | } 123 | 124 | BOOST_AUTO_TEST_SUITE_END() -------------------------------------------------------------------------------- /test/test_standaloneasioscheduler.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include "scheduler_test_templates.h" 31 | #include "cli/standaloneasioscheduler.h" 32 | 33 | using namespace std; 34 | using namespace cli; 35 | 36 | BOOST_AUTO_TEST_SUITE(StandaloneAsioSchedulerSuite) 37 | 38 | BOOST_AUTO_TEST_CASE(Basics) 39 | { 40 | SchedulingTest(); 41 | } 42 | 43 | BOOST_AUTO_TEST_CASE(SameThread) 44 | { 45 | SameThreadTest(); 46 | } 47 | 48 | BOOST_AUTO_TEST_CASE(Exceptions) 49 | { 50 | ExceptionTest(); 51 | } 52 | 53 | BOOST_AUTO_TEST_CASE(StandaloneAsioNonOwner) 54 | { 55 | detail::StandaloneAsioLib::ContextType ioc; 56 | StandaloneAsioScheduler scheduler(ioc); 57 | bool done = false; 58 | scheduler.Post( [&done]() noexcept { done = true; } ); 59 | ioc.run_one(); 60 | BOOST_CHECK(done); 61 | } 62 | 63 | BOOST_AUTO_TEST_SUITE_END() 64 | -------------------------------------------------------------------------------- /test/test_volatilehistorystorage.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #include 31 | #include "cli/volatilehistorystorage.h" 32 | 33 | using namespace cli; 34 | 35 | BOOST_AUTO_TEST_SUITE(VolatileHistoryStorageSuite) 36 | 37 | BOOST_AUTO_TEST_CASE(Basics) 38 | { 39 | VolatileHistoryStorage s(10); 40 | 41 | // starts empty 42 | BOOST_CHECK(s.Commands().empty()); 43 | 44 | const std::vector v = { "item1", "item2", "item3", "item4", "item5", "item6" }; 45 | s.Store(v); 46 | auto result = s.Commands(); 47 | BOOST_CHECK_EQUAL_COLLECTIONS(v.begin(), v.end(), result.begin(), result.end()); 48 | 49 | const std::vector v2 = { "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 50 | s.Store(v2); 51 | result = s.Commands(); 52 | const std::vector expected = { "item3", "item4", "item5", "item6", "itemA", "itemB", "itemC", "itemD", "itemE", "itemF" }; 53 | BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(), result.begin(), result.end()); 54 | 55 | s.Clear(); 56 | BOOST_CHECK(s.Commands().empty()); // check clear 57 | } 58 | 59 | BOOST_AUTO_TEST_SUITE_END() --------------------------------------------------------------------------------