一个易于使用的C++库,用于构建强大的控制台应用程序

网友投稿 626 2022-10-31

一个易于使用的C++库,用于构建强大的控制台应用程序

一个易于使用的C++库,用于构建强大的控制台应用程序

Console Component

An easy to use component for building powerful console applications written in C++.

Console ComponentPreviewInstallUsageBuildTargetMotiviationContribution

Preview

Install:

Create Project:

Usage:

Install

The easiest way to install is to use the vscode extension:

MarketplaceRepository

Alternative this could also be achieved in few manual steps:

First let's download the CPM script

mkdir -p cmake && wget -O cmake/CPM.cmake https://raw.githubusercontent.com/TheLartians/CPM/master/cmake/CPM.cmake

Then include this script in your CMake file:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)project(my_cli CXX)# Add The CPM Scriptinclude(cmake/CPM.cmake)# Add The console-componentCPMAddPackage( NAME console GITHUB_REPOSITORY edenreich/console-component VERSION 1.0.2 OPTIONS "WITH_TESTS Off")

Create a directory called commands, there you would store all your command objects (see usage on how to create a command). Lastly link your executable with the console-component like this: target_link_libraries(${PROJECT_NAME} console)

If you having trouble setting this up, take a look on the a examples first.

Usage

Create a command definition file:

// commands/copy_files.h#pragma once#include #include namespace Interfaces = Console::Interfaces;namespace Types = Console::Types;/** * @name copy:files */class CopyFiles : public Interfaces::CommandInterface{public: /** * Retrieve the name of the command. * * @return std::string */ std::string getName() override; /** * Retrieve the description of the command. * * @return std::string */ std::string getDescription() override; /** * Retrieve the command options. * * @return Console::Types::AvailableOptions */ Types::AvailableOptions getOptions() override; /** * Handle the command. * * @param Console::Interfaces::InputInterface * input * @param Console::Interfaces::OutputInterface * output * @return ExitCode */ ExitCode handle(Interfaces::InputInterface * input, Interfaces::OutputInterface * output) override;};

Create a command implemention file:

// commands/copy_files.cpp#include "copy_files.h"/** * Retrieve the name of the command. * * @return std::string */std::string CopyFiles::getName(){ return "copy:files";}/** * Retrieve the description of the command. * * @return std::string */std::string CopyFiles::getDescription(){ return "copy files from to ";}/** * Retrieve the command options. * * @return Console::Types::AvailableOptions */Types::AvailableOptions CopyFiles::getOptions(){ Types::AvailableOptions options; options["-s"] = std::pair("--source", "specific the source"); options["-d"] = std::pair("--dest", "specific the destination"); return options;}/** * Handle the command. * * @param Console::Interfaces::InputInterface * input * @param Console::Interfaces::OutputInterface * output * @return ExitCode */ExitCode CopyFiles::handle(Interfaces::InputInterface * input, Interfaces::OutputInterface * output){ if (input->wantsHelp()) { output->printCommandHelp(this); return ExitCode::NeedHelp; } if (input->getOption("source").empty() || input->getOption("dest").empty()) { output->warning("wrong options.."); output->printCommandHelp(this); return ExitCode::NeedHelp; } std::string source = input->getOption("source"); std::string dest = input->getOption("dest"); output->info("Copying files from %s to %s", source.c_str(), dest.c_str()); return ExitCode::Ok;}

Create the application and add the command:

// main.cpp#include #include "commands/copy_files.h"#include "commands/hello_world.h"int main(int argc, char * argv[]){ Console::Application app(argc, argv); app.setApplicationName("Todo List Application"); app.setApplicationUsage("./bin/todo [command] [options]"); app.setApplicationVersion("1.0.0"); app.setAutoPrintHelp(true); app.setApplicationDescription("Todo List Application"); app.addGlobalOption("--test", "Testing the application", "-t"); app.addCommand(new CopyFiles); app.addCommand(new HelloWorld); return app.run();}

Build

Run cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release --target install -- -j4

After building the project all distributed files should be located in build/dist.

Target

This project targets Linux, Windows and macOS

Motiviation

Often I find myself have to write the same code over and over again, So I've decided to dedicate my time to build a console application in an OOP way. After all, all it is needed is a CPP and HEADER file per command. Having dedicated class / object per command makes it easier to maintain.

Because it is statically linked library I have avoided including external libraries and kept it as simple as possible.

Contribution

If 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.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot+Flowable 快速实现工作流的开发流程
下一篇:Python中实用金融机器学习(FinML)工具和应用程序的精选列表
相关文章

 发表评论

暂时没有评论,来抢沙发吧~