cli-base64-main.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-23
  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. CliCommand command(argv, argv + argc);
  27. if (isValidCommand(command))
  28. {
  29. if (command[1] == "--encode")
  30. {
  31. cout << Base64{}.encode(command[2]) << endl;
  32. }
  33. if (command[1] == "--decode")
  34. {
  35. cout << Base64{}.decode(command[2]) << endl;
  36. }
  37. if (command[1] == "--help")
  38. {
  39. printHelp();
  40. }
  41. }
  42. else
  43. {
  44. cerr << "There is an error in this command. Please use \"--help\" to get more information." << endl;
  45. }
  46. exit(0);
  47. }
  48. void printHelp()
  49. {
  50. string help = "Base 64 CLI - " + LibraryVersion::getVersion() + "\n\n";
  51. help += "(1) encode a string:\t\t";
  52. help += "--encode [string]\n";
  53. help += "(2) decode a string:\t\t";
  54. help += "--decode [string]";
  55. cout << help << endl;
  56. }
  57. bool isValidCommand(const CliCommand &_command)
  58. {
  59. 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";
  60. }