Skip to content

Commit

Permalink
CLI for kim-base64-encode
Browse files Browse the repository at this point in the history
Create the command-line-interface for kim-base64-encode following the docopt.org format keeping inline with other utils/kim-api-* binary executables
  • Loading branch information
nav-mohan committed Jan 21, 2025
1 parent 8c01bb3 commit 6f0876d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ if(NOT WIN32 OR CYGWIN)
endif()

add_executable(kim-base64-encode "${PROJECT_SOURCE_DIR}/cpp/src/KIM_Base64Encode.cpp")
target_include_directories(kim-base64-encode PRIVATE "${PROJECT_SOURCE_DIR}/cpp/include")
target_include_directories(kim-base64-encode PRIVATE "${PROJECT_SOURCE_DIR}/cpp/include" "${CMAKE_CURRENT_BINARY_DIR}/cpp/include")
set_target_properties(kim-base64-encode PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/utils" )

# Keeping the name XXD for now, as otherwise this will result in much larger refactoring
Expand Down
27 changes: 24 additions & 3 deletions cpp/src/KIM_Base64Encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// and writes it to a new file, like XXD but with base64 encoding

#include "KIM_Base64.hpp"
#include "KIM_Version.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -92,12 +93,32 @@ void WriteEncodedFile(std::string & fileName, std::string & outputFileName){
fclose(outputFile);
}

void usage(std::string name)
{
size_t beg = name.find_last_of("/\\");
if (beg != std::string::npos) name = name.substr(beg + 1, std::string::npos);

// Follows docopt.org format
std::cerr << "Usage:\n"
<< " " << name << " " << "<input-filename> " << "<output-filename>\n"
<< " " << name << " " << "--version\n";
// note: this interface is likely to change in future kim-api releases
}

int main(int argc, char * argv[]){
if (argc != 3){
std::cerr << "Improper arguments, please provide input and output file name\n";
return 1;

if ((argc == 2) && (std::string(argv[1]) == "--version"))
{
std::cout << KIM_VERSION_STRING << std::endl;
return 0;
}
if ((argc != 3))
{
usage(argv[0]);
return -1;
}


std::string fileName = argv[1];
std::string outputFileName = argv[2];
WriteEncodedFile(fileName, outputFileName);
Expand Down

0 comments on commit 6f0876d

Please sign in to comment.