-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustom_flag.cc
More file actions
52 lines (45 loc) · 1.48 KB
/
custom_flag.cc
File metadata and controls
52 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2020 The Console Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <iostream>
#include "console/autocompletion.h"
#include "console/flag.h"
#include "console/stream.h"
int main(int argc, char** argv) {
#if defined(OS_WIN)
console::Console::EnableAnsi(std::cout);
#endif
uint16_t number;
console::FlagParser flag_parser;
flag_parser.set_program_name("custom_flag");
flag_parser
.AddFlag<console::Uint16Flag>(
[&number](absl::string_view input, std::string* reason) {
uint16_t n;
if (!console::FlagValueTraits<uint16_t>::ParseValue(input, &n,
reason)) {
return false;
}
if (n % 2 == 0) {
*reason = absl::StrFormat("%u is not a odd number", n);
return false;
}
number = n;
return true;
})
.set_name("number")
.set_required()
.set_help("Please input only odd numbers!");
// Uncomment if you want to generate json file used by console-autocomplete!
// console::Autocompletion::WriteToJson(flag_parser, "custom_flag.json");
if (!flag_parser.Parse(argc, argv)) {
{
console::Stream stream(std::cerr);
stream.Red();
std::cerr << "[ERROR]: ";
}
std::cerr << flag_parser.error_message() << std::endl;
return 1;
}
return 0;
}