cli-base64-main.cpp 1.6 KB

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