From e758ec367bfd96b4597e461dcdfb40f7904e8b5a Mon Sep 17 00:00:00 2001 From: Serein <2075337935@qq.com> Date: Sun, 11 Aug 2024 18:47:03 +0800 Subject: [PATCH] feat: cmd arg parse --- src/args/cmd_args.cpp | 18 ++++++++++++++++++ src/args/cmd_args.h | 10 ++++++++++ src/main.cc | 5 ----- src/main.cpp | 23 +++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/args/cmd_args.cpp create mode 100644 src/args/cmd_args.h delete mode 100644 src/main.cc create mode 100644 src/main.cpp diff --git a/src/args/cmd_args.cpp b/src/args/cmd_args.cpp new file mode 100644 index 0000000..e401087 --- /dev/null +++ b/src/args/cmd_args.cpp @@ -0,0 +1,18 @@ +#include "cmd_args.h" +#include +#include + +CmdArgs parseCmdArgs(int argc, char** argv) { + argparse::ArgumentParser program("yarusto", VERSION_FULL); + program.add_argument("-i", "--input-path") + .help("The input file(.zip) path") + .default_value(std::string(".")); + program.add_argument("-o", "--output-path") + .help("The output file(.tar) path") + .default_value(std::string("./out")); + + program.parse_args(argc, argv); + + return {.inputPath = program.get("--input-path"), + .outputPath = program.get("--output-path")}; +} \ No newline at end of file diff --git a/src/args/cmd_args.h b/src/args/cmd_args.h new file mode 100644 index 0000000..55f5be8 --- /dev/null +++ b/src/args/cmd_args.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +struct CmdArgs { + std::string inputPath; + std::string outputPath; +}; + +CmdArgs parseCmdArgs(int argc, char** argv); \ No newline at end of file diff --git a/src/main.cc b/src/main.cc deleted file mode 100644 index b233b6e..0000000 --- a/src/main.cc +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main(int argc, char** argv) { - return 0; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..fe7a2af --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,23 @@ +#include +#include + +/* +Usage: yarusto [--help] [--version] [--input-path VAR] [--output-path VAR] + +Optional arguments: + -h, --help shows help message and exits + -v, --version prints version information and exits + -i, --input-path The input file(.zip) path [nargs=0..1] [default: "."] + -o, --output-path The output file(.tar) path [nargs=0..1] [default: "./out"] +*/ + +int main(int argc, char** argv) { + CmdArgs args; + try { + args = parseCmdArgs(argc, argv); + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + return 1; + } + return 0; +}