From 49d7098738fbda435923ca9e712679fbb75d49c7 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 10 Sep 2018 15:39:38 +0800 Subject: [PATCH] fix error --- Makefile | 15 ++++++++------- include/json.h | 13 +++++++++++++ src/csar.cpp | 26 ++++++++++++++------------ src/json.cpp | 22 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 include/json.h create mode 100644 src/json.cpp diff --git a/Makefile b/Makefile index 6bcaad5..06db9b8 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,9 @@ CPP = $(MinGW64)/bin/g++ # CC = gcc.exe # WINDRES = windres.exe # RM = rm -f -OBJ = src/csar.o test/test_emplace.o test/test_json.o -LINKOBJ = +OBJ = src/csar.o +TESTOBJ = test/test_emplace.o test/test_json.o +LINKOBJ = src/json.o LIBS = -lboost_program_options-mt -L $(MinGW64)/lib INCS = -I $(MinGW64)/include -I include CXXINCS = $(INCS) @@ -26,13 +27,13 @@ SRC = $(SRCDIR)/$(NAME:.exe=.cpp) all: all-before $(BIN) all-after clean: clean-custom - ${RM} $(OBJ) $(BIN) + ${RM} $(OBJ) $(BIN) $(LINKOBJ) -$(BIN): $(SRC) - $(CPP) -o $@ $< $(LINKOBJ) $(LIBS) +$(BIN): $(OBJ) $(LINKOBJ) + $(CPP) -o $@ $^ $(LIBS) -src/csar.o: src/csar.cpp - $(CPP) -c src/csar.cpp -o src/csar.o $(CXXFLAGS) +src/%.o: src/%.cpp + $(CPP) -o $@ -c $< $(CXXFLAGS) test/test_emplace.o: test/test_emplace.cpp $(CPP) -c test/test_emplace.cpp -o test/test_emplace.o $(CXXFLAGS) diff --git a/include/json.h b/include/json.h new file mode 100644 index 0000000..cbe9b7e --- /dev/null +++ b/include/json.h @@ -0,0 +1,13 @@ +#pragma once + +#include "json.hpp" + +using json = nlohmann::json; + +class Task:public json +{ +public: + void operator() (); +}; + +Task&& loadjson(std::string file_path); diff --git a/src/csar.cpp b/src/csar.cpp index 4dff3ca..945ad62 100644 --- a/src/csar.cpp +++ b/src/csar.cpp @@ -1,20 +1,16 @@ #include -#include #include - -struct NotImplementedError: public std::exception -{ -}; +#include "json.h" namespace po = boost::program_options; int main(int argc, char const *argv[]) { - po::options_description desc("Arguments: "); + po::options_description desc("语法: csar [options]"); desc.add_options() - ("help", "produce help messages") + ("help,h", "produce help messages") ("input-file", po::value< std::string >(), "input file path") - ("output", po::value< std::string >(), "output file path") + ("output-file", po::value< std::string >(), "output file path") ; po::positional_options_description p; p.add("input-file", -1); @@ -25,13 +21,19 @@ int main(int argc, char const *argv[]) po::notify(vm); if (vm.count("help")) { - std::cout << "Usage: options_description [options]\n"; std::cout << desc << std::endl; - return 0; } - if (vm.count("output")) + + if (vm.count("input-file")) { - // std::cout << desc["output"].as() << std::endl; + auto infile = vm["input-file"].as(); + std::cout << "Read from file: " << infile << std::endl; + auto task = loadjson(infile); + task(); } + else + { + std::cout << desc << std::endl; + } return 0; } \ No newline at end of file diff --git a/src/json.cpp b/src/json.cpp new file mode 100644 index 0000000..5c513a9 --- /dev/null +++ b/src/json.cpp @@ -0,0 +1,22 @@ +#include "json.h" +#include +#include +#include + +struct NotImplementedError: public std::exception +{ +}; + +void Task::operator() () +{ + std::cout << "Running Task" << std::endl; + throw NotImplementedError(); +} + +Task&& loadjson(std::string file_path) +{ + std::ifstream file(file_path); + Task s; + file >> *static_cast(&s); + return std::move(s); +} \ No newline at end of file