working cli prototype
This commit is contained in:
parent
05fa1caa61
commit
9ab4eb97fe
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
main:
|
||||
g++ -I lib/ src/setopt.cpp -o setopt
|
||||
g++ -I lib/ src/setopt.cpp -o setopt -g
|
||||
|
||||
test: test_template test_compile
|
||||
|
||||
|
@ -1,7 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <getopt.h>
|
||||
#include <json.hpp>
|
||||
#include "setopt.hpp"
|
||||
|
||||
signed main(){
|
||||
return 0;
|
||||
#define EXECUTABLE_NAME "setopt"
|
||||
|
||||
const char HELP_MSG[] = "\
|
||||
%s <options> : \n\
|
||||
\t-s <str> : specify optstring\n\
|
||||
\t-j <json> : read arguments from JSON string\n\
|
||||
\t-h : print help message and quit\n\
|
||||
";
|
||||
|
||||
|
||||
|
||||
using json = nlohmann::json;
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
void usage(){
|
||||
printf(HELP_MSG, EXECUTABLE_NAME);
|
||||
}
|
||||
|
||||
[[ noreturn ]] inline void die(){
|
||||
usage();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char* json_str = nullptr;
|
||||
bool done_something = false;
|
||||
unordered_map<char, std::string> g_argv;
|
||||
const char* g_optstring = nullptr;
|
||||
|
||||
void parse_args(int argc, char** argv){
|
||||
char opt;
|
||||
while((opt = getopt(argc, argv, "j:s:h")) != -1){
|
||||
switch(opt){
|
||||
case 'j':
|
||||
json_str = strdup(optarg);
|
||||
break;
|
||||
case 's':
|
||||
g_optstring = strdup(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
usage();
|
||||
exit(0);
|
||||
}
|
||||
done_something = true;
|
||||
}
|
||||
if(not done_something){ die(); }
|
||||
}
|
||||
|
||||
signed main(int argc, char** argv){
|
||||
parse_args(argc, argv);
|
||||
|
||||
if(json_str){
|
||||
json j = json::parse(json_str);
|
||||
for(const auto &i : j.items()){
|
||||
g_argv[i.key()[0]] = i.value();
|
||||
}
|
||||
}
|
||||
|
||||
if(not g_optstring){ die(); }
|
||||
|
||||
puts(setopt(g_optstring, g_argv));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user