Skip to content

Commit 64d4ded

Browse files
committed
add -h/-v via argparse
1 parent 4b4e240 commit 64d4ded

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ target_precompile_headers(get-livecaptions PRIVATE src/pch.h)
1010

1111
find_package(asio CONFIG REQUIRED)
1212
find_package(wil CONFIG REQUIRED)
13-
target_link_libraries(get-livecaptions PRIVATE WIL::WIL asio::asio)
13+
find_package(argparse CONFIG REQUIRED)
14+
target_link_libraries(get-livecaptions PRIVATE WIL::WIL asio::asio argparse::argparse)
1415

1516
#if you use lastest cppwinrt from vcpkg, uncomment following
1617
#find_package(cppwinrt CONFIG REQUIRED)

src/main.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ using namespace winrt;
44
using namespace winrt::Windows::Foundation;
55
using namespace winrt::Windows::UI::UIAutomation;
66

7+
#define VERSION_STRING "0.1.240505"
8+
79
std::string get_current_time()
810
{
911
// [TIPS]c+20 runtime is too expensive, + 500k for following implement.
@@ -111,14 +113,14 @@ class Engine
111113

112114

113115

114-
void usage()
115-
{
116-
std::cerr << "Write all content of LiveCaptions Windows System Program into file, continually.Ctrl-C to exit." << std::endl;
117-
std::cerr << "Usage: get-livecatpions file" << std::endl;
118-
std::cerr << "Options:" << std::endl;
119-
std::cerr << " file filename, to save content of live catpions running." << std::endl;
120-
exit(1);
121-
}
116+
// void usage()
117+
// {
118+
// std::cerr << "Write all content of LiveCaptions Windows System Program into file, continually.Ctrl-C to exit." << std::endl;
119+
// std::cerr << "Usage: get-livecatpions file" << std::endl;
120+
// std::cerr << "Options:" << std::endl;
121+
// std::cerr << " file filename, to save content of live catpions running." << std::endl;
122+
// exit(1);
123+
// }
122124
bool touch_file(const std::string &filename)
123125
{
124126
std::ofstream file(filename,std::ios::app);
@@ -129,18 +131,39 @@ bool touch_file(const std::string &filename)
129131

130132
int main(int argc, char *argv[])
131133
{
132-
if ((argc != 2) || (!touch_file(argv[1])))
133-
usage();
134-
if (!Engine::is_livecaption_running())
135-
{
136-
std::cerr << "[Error]Live Captions is not running." <<std::endl;
137-
exit(1);
134+
135+
136+
std::string strFileName;
137+
argparse::ArgumentParser program("get-livecaptions",VERSION_STRING);
138+
program.add_argument("-o", "--output")
139+
.metavar("file")
140+
.help("filename, write content into file. use - for console.")
141+
.required();
142+
143+
program.add_description("Write the content of LiveCaptions Windows System Program into file, continually.");
144+
program.add_epilog("use ctrl-c to exit program.");
145+
146+
try {
147+
if(argc==1) {program.print_help();exit(1);}
148+
program.parse_args(argc, argv);
149+
strFileName = program.get<std::string>("--output");
150+
151+
if (!Engine::is_livecaption_running())
152+
{
153+
std::cerr << "[Error]Live Captions is not running." <<std::endl;
154+
exit(1);
155+
}
156+
}
157+
catch (const std::exception& err) {
158+
std::cerr << err.what() << std::endl;
159+
std::cerr << program;
160+
return 1;
138161
}
139162

140163
try
141164
{
142165
asio::io_context io_context(1);
143-
Engine eng(argv[1]);
166+
Engine eng(strFileName);
144167

145168
asio::signal_set signals(io_context, SIGINT, SIGTERM);
146169
signals.async_wait([&](auto, auto){

src/pch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
#include <winrt/windows.ui.uiautomation.h>
1212
#include <uiautomation.h>
13-
#include <wil/resource.h>
13+
#include <wil/resource.h>
14+
#include <argparse/argparse.hpp>

vcpkg.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version-string": "latest",
44
"dependencies": [
55
"asio",
6-
"wil"
6+
"wil",
7+
"argparse"
78
]
89
}

0 commit comments

Comments
 (0)