cli-base64-main.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-07-03
  6. * Changed: 2023-02-05
  7. *
  8. * */
  9. #include <iostream>
  10. #include <ls-std/ls-std-core.hpp>
  11. #include <ls-std/ls-std-encoding.hpp>
  12. #include <string>
  13. #include <vector>
  14. using CliCommand = std::vector<std::string>;
  15. void printHelp();
  16. bool isValidCommand(const CliCommand &_command);
  17. int main(int argc, char *argv[])
  18. {
  19. CliCommand command(argv, argv + argc);
  20. if (isValidCommand(command))
  21. {
  22. if (command[1] == "--encode")
  23. {
  24. std::cout << ls::std::encoding::Base64{}.encode(command[2]) << std::endl;
  25. }
  26. if (command[1] == "--decode")
  27. {
  28. std::cout << ls::std::encoding::Base64{}.decode(command[2]) << std::endl;
  29. }
  30. if (command[1] == "--help")
  31. {
  32. printHelp();
  33. }
  34. }
  35. else
  36. {
  37. std::cerr << "There is an error in this command. Please use \"--help\" to get more information." << std::endl;
  38. }
  39. exit(0);
  40. }
  41. void printHelp()
  42. {
  43. std::string help = "Base 64 CLI - " + ls::std::core::LibraryVersion::getVersion() + "\n\n";
  44. help += "(1) encode a string:\t\t";
  45. help += "--encode [string]\n";
  46. help += "(2) decode a string:\t\t";
  47. help += "--decode [string]";
  48. std::cout << help << std::endl;
  49. }
  50. bool isValidCommand(const CliCommand &_command)
  51. {
  52. 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";
  53. }