{"id":20549594,"url":"https://github.com/edenreich/console-component","last_synced_at":"2025-04-14T11:05:21.231Z","repository":{"id":44813801,"uuid":"190436997","full_name":"edenreich/console-component","owner":"edenreich","description":"An easy to use component for building powerful console applications written in C++","archived":false,"fork":false,"pushed_at":"2022-01-22T23:18:32.000Z","size":215,"stargazers_count":91,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T11:05:14.641Z","etag":null,"topics":["cli","cmake","command-line","commandline","console","console-component","cpp","cpp-component","cpp14","cpp17","cross-platform"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edenreich.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-05T17:15:24.000Z","updated_at":"2022-11-26T07:46:17.000Z","dependencies_parsed_at":"2022-08-12T11:40:12.213Z","dependency_job_id":null,"html_url":"https://github.com/edenreich/console-component","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edenreich%2Fconsole-component","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edenreich%2Fconsole-component/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edenreich%2Fconsole-component/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edenreich%2Fconsole-component/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edenreich","download_url":"https://codeload.github.com/edenreich/console-component/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248868768,"owners_count":21174757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","cmake","command-line","commandline","console","console-component","cpp","cpp-component","cpp14","cpp17","cross-platform"],"created_at":"2024-11-16T02:18:42.129Z","updated_at":"2025-04-14T11:05:21.208Z","avatar_url":"https://github.com/edenreich.png","language":"C++","readme":"![Build](https://github.com/edenreich/console-component/workflows/Build/badge.svg?branch=master) ![Lint](https://github.com/edenreich/console-component/workflows/Lint/badge.svg?branch=master)\n\n# Console Component\n\nAn easy to use component for building powerful console applications written in C++.\n\n- [Console Component](#Console-Component)\n  - [Preview](#Preview)\n  - [Install](#Install)\n  - [Usage](#Usage)\n  - [Build](#Build)\n  - [Target](#Target)\n  - [Motivation](#Motivation)\n  - [Contribution](#Contribution)\n\n## Preview\n\n- Install:\n\u003cp align=\"left\"\u003e\u003cimg src=\"https://drive.google.com/uc?export=view\u0026id=1iRNoWwXo0ZsWZ8T9VdJXouaabXP5r6ra\"\u003e\u003c/p\u003e\n\n- Create Project:\n\u003cp align=\"left\"\u003e\u003cimg src=\"https://drive.google.com/uc?export=view\u0026id=1B-M_ITUwNsU3nTICGJ1l91aEggJmZx5z\"\u003e\u003c/p\u003e\n\n- Usage:\n\u003cp align=\"left\"\u003e\u003cimg src=\"https://drive.google.com/uc?export=view\u0026id=1MX0seuhVv7uu4QzMaEXHMyDJRviioSy0\"\u003e\u003c/p\u003e\n\n## Install\n\nThe easiest way to install is to use the vscode extension:\n\n- [Marketplace](https://marketplace.visualstudio.com/items?itemName=EdenReich.cpp-console-generator)\n- [Repository](https://github.com/edenreich/console-generator-ext-vscode)\n\nAlternatively this could also be achieved in a few manual steps:\n\n1. Create a **CMakeLists.txt** file:\n\n```cmake\ncmake_minimum_required(VERSION 3.12 FATAL_ERROR)\n\nproject(todo CXX)\n\n# Add the CPM package manager\nif(NOT EXISTS \"${CMAKE_HOME_DIRECTORY}/cmake/CPM.cmake\")\n  file(DOWNLOAD https://raw.githubusercontent.com/TheLartians/CPM.cmake/master/cmake/CPM.cmake \"${CMAKE_HOME_DIRECTORY}/cmake/CPM.cmake\")\nendif()\n\ninclude(cmake/CPM.cmake)\n\n# Add The console-component\nCPMAddPackage(\n  NAME console\n  GITHUB_REPOSITORY edenreich/console-component\n  VERSION 1.0.6\n  OPTIONS\n    \"WITH_TESTS Off\"\n)\n\n# Move the default directory of the executable to bin directory\nset(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)\n\n# Collect implementation files of the current project\nset(SOURCES_CXX_FILES\n    main.cpp\n    # add your Implementation files here..\n)\n\n# Add the implementation files to the executable\nadd_executable(${PROJECT_NAME} ${SOURCES_CXX_FILES})\n\n# Add the definition files\ninclude_directories(${CMAKE_BINARY_DIR}/dist/include)\n\n# Link the executable with the library\ntarget_link_libraries(${PROJECT_NAME} console)\n```\n\n2. Create a **build** directory and cd into it:\n\n```sh\nmkdir build \u0026\u0026 cd build\n```\n\n3. Look at usage for creating commands or use the [console-component-generator](https://github.com/edenreich/console-component-generator) util to generates command files easily.\n\n```sh\ncurl -sSL https://github.com/edenreich/console-component-generator/releases/download/v1.0.0/console-gen -o console-gen\nchmod u+x console-gen \u0026\u0026 sudo mv console-gen /usr/bin/console-gen\n```\n\n4. Build your project:\n\n```sh\ncmake .. \u0026\u0026 make\n```\n\nIf you are having trouble setting this up, take a look at the examples first.\n\n## Usage\n\n1. Create the application:\n\n```cpp\n// main.cpp\n#include \u003cconsole/application.h\u003e\n\n#include \"commands/copy_files.h\"\n#include \"commands/hello_world.h\"\n\n\nint main(int argc, char * argv[])\n{\n    Console::Application app(argc, argv);\n\n    app.setApplicationName(\"Todo List Application\");\n    app.setApplicationUsage(\"./bin/todo [command] [options]\");\n    app.setApplicationVersion(\"1.0.0\");\n    app.setAutoPrintHelp(true);\n\n    app.setApplicationDescription(\"Todo List Application\");\n\n    app.addGlobalOption(\"--test\", \"Testing the application\", \"-t\");\n\n    app.addCommand(new CopyFiles);\n    app.addCommand(new HelloWorld);\n\n    return app.run();\n}\n```\n\n2. Create a command definition file:\n\n```cpp\n// commands/copy_files.h\n#pragma once\n\n#include \u003cconsole/interfaces/command_interface.h\u003e\n#include \u003cconsole/types/collections.h\u003e\n\nnamespace Interfaces = Console::Interfaces;\nnamespace Types = Console::Types;\n\n/**\n * @name copy:files\n */\nclass CopyFiles : public Interfaces::CommandInterface\n{\n\npublic:\n\n    /**\n     * Retrieve the name of the command.\n     *\n     * @return std::string\n     */\n    std::string getName() override;\n\n    /**\n     * Retrieve the description of the command.\n     *\n     * @return std::string\n     */\n    std::string getDescription() override;\n\n    /**\n     * Retrieve the command options.\n     *\n     * @return Console::Types::AvailableOptions\n     */\n    Types::AvailableOptions getOptions() override;\n\n    /**\n     * Handle the command.\n     *\n     * @param Console::Interfaces::InputInterface * input\n     * @param Console::Interfaces::OutputInterface * output\n     * @return ExitCode\n     */\n    ExitCode handle(Interfaces::InputInterface * input, Interfaces::OutputInterface * output) override;\n\n};\n```\n\n3. Create a command implementation file:\n\n```cpp\n// commands/copy_files.cpp\n#include \"copy_files.h\"\n\n\n/**\n * Retrieve the name of the command.\n *\n * @return std::string\n */\nstd::string CopyFiles::getName()\n{\n    return \"copy:files\";\n}\n\n/**\n * Retrieve the description of the command.\n *\n * @return std::string\n */\nstd::string CopyFiles::getDescription()\n{\n    return \"copy files from \u003csource\u003e to \u003cdist\u003e\";\n}\n\n/**\n * Retrieve the command options.\n *\n * @return Console::Types::AvailableOptions\n */\nTypes::AvailableOptions CopyFiles::getOptions()\n{\n    Types::AvailableOptions options;\n\n    options[\"-s\"] = std::pair\u003cstd::string, std::string\u003e(\"--source\", \"specific the source\");\n    options[\"-d\"] = std::pair\u003cstd::string, std::string\u003e(\"--dest\", \"specific the destination\");\n\n    return options;\n}\n\n/**\n * Handle the command.\n *\n * @param Console::Interfaces::InputInterface * input\n * @param Console::Interfaces::OutputInterface * output\n * @return ExitCode\n */\nExitCode CopyFiles::handle(Interfaces::InputInterface * input, Interfaces::OutputInterface * output)\n{\n    if (input-\u003ewantsHelp()) {\n        output-\u003eprintCommandHelp(this);\n        return ExitCode::NeedHelp;\n    }\n\n    if (input-\u003egetOption(\"source\").empty() || input-\u003egetOption(\"dest\").empty()) {\n        output-\u003ewarning(\"wrong options..\");\n        output-\u003eprintCommandHelp(this);\n        return ExitCode::NeedHelp;\n    }\n\n    std::string source = input-\u003egetOption(\"source\");\n    std::string dest = input-\u003egetOption(\"dest\");\n\n    output-\u003einfo(\"Copying files from %s to %s\", source.c_str(), dest.c_str());\n\n    return ExitCode::Ok;\n}\n```\n\n## Build\n\nRun `cd build \u0026\u0026 cmake .. -DCMAKE_BUILD_TYPE=Release \u0026\u0026 cmake --build . --config Release --target install -- -j4`\n\nAfter building the project all distributed files should be located in `build/dist`.\n\n## Target\n\nThis project targets Linux, Windows and MacOS.\n\n## Motivation\n\nOften I find myself having to write the same code over and over again, So I've decided to dedicate my time to building a console application in an OOP way.\nAfter all, all it is needed is a CPP and a Header file per command. Having a dedicated class / object per command makes it easier to maintain.\n\nBecause it is a statically linked library I have avoided including external libraries and kept it as simple as possible.\n\n## Contribution\n\nIf you find this project interesting or have any suggestions, feel free to send a pull request. I will be more than happy to review it.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedenreich%2Fconsole-component","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedenreich%2Fconsole-component","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedenreich%2Fconsole-component/lists"}