瀏覽代碼

Add CLI Tool for Base64 functionality

This tool is a CLI tool, which you can use to encode or decode strings
based on Base64 encoding standard.
Patrick-Christopher Mattulat 1 年之前
父節點
當前提交
b71e397ac7
共有 2 個文件被更改,包括 70 次插入0 次删除
  1. 6 0
      CMakeLists.txt
  2. 64 0
      source/ls_std/encoding/cli/cli_base64_main.cpp

+ 6 - 0
CMakeLists.txt

@@ -438,6 +438,12 @@ if (${LS_STD_BUILD_MODULE})
     set_target_properties("${MODULE_NAME_ENCODING}" PROPERTIES DEBUG_POSTFIX "_d")
 endif ()
 
+# CLI base64
+
+add_executable(cli_base64
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/encoding/cli/cli_base64_main.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/encoding/Base64.cpp)
+
 ##########################################################
 # Build Library (event)
 ##########################################################

+ 64 - 0
source/ls_std/encoding/cli/cli_base64_main.cpp

@@ -0,0 +1,64 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2022-07-03
+ * Changed:         2022-07-03
+ *
+ * */
+
+#include <iostream>
+#include <vector>
+#include <string>
+#include <ls_std/ls_std_encoding.hpp>
+#include <ls_std/ls_std_core.hpp>
+
+using CLICommand = std::vector<std::string>;
+
+void printHelp();
+bool isValidCommand(const CLICommand &_command);
+
+int main(int argc, char *argv[])
+{
+  CLICommand command(argv, argv + argc);
+
+  if (isValidCommand(command))
+  {
+    if (command[1] == "--encode")
+    {
+      std::cout << ls::std::encoding::Base64{}.encode(command[2]) << std::endl;
+    }
+
+    if (command[1] == "--decode")
+    {
+      std::cout << ls::std::encoding::Base64{}.decode(command[2]) << std::endl;
+    }
+
+    if (command[1] == "--help")
+    {
+      printHelp();
+    }
+  }
+  else
+  {
+    std::cerr << "There is an error in this command. Please use \"--help\" to get some support." << std::endl;
+  }
+
+  exit(0);
+}
+
+void printHelp()
+{
+  std::string help = "Base 64 CLI - " + ls::std::core::getVersion() + "\n\n";
+  help += "(1) encode a string:\t\t";
+  help += "--encode [string]\n";
+  help += "(2) decode a string:\t\t";
+  help += "--decode [string]";
+
+  std::cout << help << std::endl;
+}
+
+bool isValidCommand(const CLICommand &_command)
+{
+  return _command.size() == 3 && _command[1] == "--encode" && !_command[2].empty() || _command.size() == 3 && _command[1] == "--decode" && !_command[2].empty() || _command.size() == 2 && _command[1] == "--help";
+}