Skip to content

Commit 302b6a9

Browse files
committed
Better args parsing
1 parent 5f2230c commit 302b6a9

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

cliphist.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <winrt/Windows.Foundation.Collections.h>
88
#include <cstring>
99
#include <string>
10+
#include <tchar.h>
11+
#include "arg.inl"
1012

1113
namespace winrt
1214
{
@@ -99,17 +101,33 @@ int wmain(const int argc, const wchar_t* argv[])
99101
return 1;
100102
}
101103

102-
if (argc <= 1)
104+
arginit(argc, argv, L"Show and select clipboard history");
105+
106+
argoptional();
107+
const TCHAR* cmd = argnumdesc(1, nullptr, L"command", L"Command option: list, view or select");
108+
const TCHAR* item = argnumdesc(2, nullptr, L"clip_id", L"Clip item target");
109+
110+
if (!argcleanup())
111+
return EXIT_FAILURE;
112+
if (argusage())
113+
return EXIT_SUCCESS;
114+
115+
if (cmd == nullptr || _wcsicmp(cmd, L"list") == 0)
103116
DumpClipboardHistoryAsync().get();
104-
else if (_wcsicmp(argv[1], L"view") == 0)
105-
DumpClipboardHistoryItemAsync(argv[2]).get();
106-
else if (_wcsicmp(argv[1], L"select") == 0)
107-
SelectClipboardHistoryItemAsync(argv[2]).get();
117+
else if (item == nullptr)
118+
{
119+
fwprintf(stderr, L"Error: Clip id missing\n");
120+
return EXIT_FAILURE;
121+
}
122+
else if (_wcsicmp(cmd, L"view") == 0)
123+
DumpClipboardHistoryItemAsync(item).get();
124+
else if (_wcsicmp(cmd, L"select") == 0)
125+
SelectClipboardHistoryItemAsync(item).get();
108126
else
109127
{
110-
fwprintf(stderr, L"Unknown option.\n");
111-
return 1;
128+
fwprintf(stderr, L"Error: Unknown command: %s\n", cmd);
129+
return EXIT_FAILURE;
112130
}
113131

114-
return 0;
132+
return EXIT_SUCCESS;
115133
}

0 commit comments

Comments
 (0)