-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathArgParser.cc
More file actions
965 lines (866 loc) · 29.9 KB
/
ArgParser.cc
File metadata and controls
965 lines (866 loc) · 29.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/** @file
Powerful and easy-to-use command line parsing for ATS
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "tscore/ArgParser.h"
#include "tscore/ink_file.h"
#include "tscore/Version.h"
#include <iostream>
#include <set>
#include <sstream>
#include <utility>
#include <sysexits.h>
std::string global_usage;
std::string description;
std::string parser_program_name;
std::string default_command;
// by default return EX_USAGE(64) when usage is called.
// if -h or --help is called specifically, return 0
int usage_return_code = EX_USAGE;
namespace ts
{
bool ArgParser::_test_mode = false;
ArgParser::ArgParser() {}
ArgParser::ArgParser(std::string const &name, std::string const &description, std::string const &envvar, unsigned arg_num,
Function const &f)
{
// initialize _top_level_command according to the provided message
_top_level_command = ArgParser::Command(name, description, envvar, arg_num, f);
}
ArgParser::~ArgParser() {}
// add new options with args
ArgParser::Command &
ArgParser::add_option(std::string const &long_option, std::string const &short_option, std::string const &description,
std::string const &envvar, unsigned arg_num, std::string const &default_value, std::string const &key)
{
return _top_level_command.add_option(long_option, short_option, description, envvar, arg_num, default_value, key);
}
// Create a mutually exclusive group
ArgParser::Command &
ArgParser::add_mutex_group(std::string const &group_name, bool required, std::string const &description)
{
return _top_level_command.add_mutex_group(group_name, required, description);
}
// Add an option to a mutually exclusive group
ArgParser::Command &
ArgParser::add_option_to_group(std::string const &group_name, std::string const &long_option, std::string const &short_option,
std::string const &description, std::string const &envvar, unsigned arg_num,
std::string const &default_value, std::string const &key)
{
return _top_level_command.add_option_to_group(group_name, long_option, short_option, description, envvar, arg_num, default_value,
key);
}
// add sub-command with only function
ArgParser::Command &
ArgParser::add_command(std::string const &cmd_name, std::string const &cmd_description, Function const &f, std::string const &key)
{
return _top_level_command.add_command(cmd_name, cmd_description, f, key);
}
// add sub-command without args and function
ArgParser::Command &
ArgParser::add_command(std::string const &cmd_name, std::string const &cmd_description, std::string const &cmd_envvar,
unsigned cmd_arg_num, Function const &f, std::string const &key)
{
return _top_level_command.add_command(cmd_name, cmd_description, cmd_envvar, cmd_arg_num, f, key);
}
void
ArgParser::add_global_usage(std::string const &usage)
{
global_usage = usage;
}
void
ArgParser::help_message(std::string_view err) const
{
_top_level_command.help_message(err);
}
// a graceful way to output help message
void
ArgParser::Command::help_message(std::string_view err) const
{
if (!description.empty()) {
std::cout << description << '\n';
}
if (!err.empty()) {
std::cout << "Error: " << err << std::endl;
}
// output global usage
if (global_usage.size() > 0) {
std::cout << "\nUsage: " + global_usage << std::endl;
}
// output subcommands
std::cout << "\nCommands ---------------------- Description -----------------------" << std::endl;
std::string prefix = "";
output_command(std::cout, prefix);
// output options
if (_option_list.size() > 0) {
std::cout << "\nOptions ======================= Default ===== Description =============" << std::endl;
output_option();
}
// output example usages
if (!_example_usages.empty()) {
std::cout << "\nExample Usage:" << std::endl;
for (const auto &example : _example_usages) {
std::cout << " " << example << std::endl;
}
}
// standard return code
ArgParser::do_exit(usage_return_code);
}
void
ArgParser::Command::version_message() const
{
// unified version message of ATS
AppVersionInfo::setup_version(_name.c_str());
AppVersionInfo::print_version();
ArgParser::do_exit(0);
}
void
ArgParser::set_default_command(std::string const &cmd)
{
if (default_command.empty()) {
if (_top_level_command._subcommand_list.find(cmd) == _top_level_command._subcommand_list.end()) {
std::cerr << "Error: Default command " << cmd << "not found" << std::endl;
ArgParser::do_exit(1);
}
default_command = cmd;
} else if (cmd != default_command) {
std::cerr << "Error: Default command " << default_command << "already existed" << std::endl;
ArgParser::do_exit(1);
}
}
// Top level call of parsing
Arguments
ArgParser::parse(const char **argv)
{
// deal with argv first
int size = 0;
_argv.clear();
while (argv[size]) {
_argv.push_back(argv[size]);
size++;
}
if (size == 0) {
std::cout << "Error: invalid argv provided" << std::endl;
ArgParser::do_exit(1);
}
// the name of the program only
_argv[0] = _argv[0].substr(_argv[0].find_last_of('/') + 1);
_top_level_command._name = _argv[0];
_top_level_command._key = _argv[0];
parser_program_name = _argv[0];
Arguments ret; // the parsed arg object to return
AP_StrVec args = _argv;
// call the recursive parse method in Command
if (!_top_level_command.parse(ret, args)) {
// deal with default command
if (!default_command.empty()) {
args = _argv;
args.insert(args.begin() + 1, default_command);
_top_level_command.parse(ret, args);
}
};
// if there is anything left, then output usage
if (!args.empty()) {
std::string msg = "Unknown command, option or args:";
for (const auto &it : args) {
msg = msg + " '" + it + "'";
}
// find the correct level to output help message
ArgParser::Command *command = &_top_level_command;
for (unsigned i = 1; i < _argv.size(); i++) {
auto it = command->_subcommand_list.find(_argv[i]);
if (it == command->_subcommand_list.end()) {
break;
}
command = &it->second;
}
command->help_message(msg);
}
return ret;
}
ArgParser::Command &
ArgParser::require_commands()
{
return _top_level_command.require_commands();
}
void
ArgParser::set_error(std::string e)
{
_error_msg = std::move(e);
}
std::string
ArgParser::get_error() const
{
return _error_msg;
}
void
ArgParser::add_description(std::string const &descr)
{
description = descr;
}
//=========================== Command class ================================
ArgParser::Command::Command() {}
ArgParser::Command::~Command() {}
ArgParser::Command::Command(std::string const &name, std::string const &description, std::string const &envvar, unsigned arg_num,
Function const &f, std::string const &key)
: _name(name), _description(description), _arg_num(arg_num), _envvar(envvar), _f(f), _key(key)
{
}
// check if this is a valid option before adding
void
ArgParser::Command::check_option(std::string const &long_option, std::string const &short_option,
std::string const & /* key ATS_UNUSED */) const
{
if (long_option.size() < 3 || long_option[0] != '-' || long_option[1] != '-') {
// invalid name
std::cerr << "Error: invalid long option added: '" + long_option + "'" << std::endl;
ArgParser::do_exit(1);
}
if (short_option.size() > 2 || (short_option.size() > 0 && short_option[0] != '-')) {
// invalid short option
std::cerr << "Error: invalid short option added: '" + short_option + "'" << std::endl;
ArgParser::do_exit(1);
}
// find if existing in option list
if (_option_list.find(long_option) != _option_list.end()) {
std::cerr << "Error: long option '" + long_option + "' already existed" << std::endl;
ArgParser::do_exit(1);
} else if (_option_map.find(short_option) != _option_map.end()) {
std::cerr << "Error: short option '" + short_option + "' already existed" << std::endl;
ArgParser::do_exit(1);
}
}
// check if this is a valid command before adding
void
ArgParser::Command::check_command(std::string const &name, std::string const & /* key ATS_UNUSED */) const
{
if (name.empty()) {
// invalid name
std::cerr << "Error: empty command cannot be added" << std::endl;
ArgParser::do_exit(1);
}
// find if existing in subcommand list
if (_subcommand_list.find(name) != _subcommand_list.end()) {
std::cerr << "Error: command already exists: '" + name + "'" << std::endl;
ArgParser::do_exit(1);
}
}
// add new options with args
ArgParser::Command &
ArgParser::Command::add_option(std::string const &long_option, std::string const &short_option, std::string const &description,
std::string const &envvar, unsigned arg_num, std::string const &default_value,
std::string const &key)
{
std::string lookup_key = key.empty() ? long_option.substr(2) : key;
check_option(long_option, short_option, lookup_key);
_option_list[long_option] = {long_option, short_option == "-" ? "" : short_option, description, envvar, arg_num, default_value,
lookup_key};
if (short_option != "-" && !short_option.empty()) {
_option_map[short_option] = long_option;
}
_last_added_option = long_option; // track for with_required() chaining
return *this;
}
// Create a mutually exclusive group
ArgParser::Command &
ArgParser::Command::add_mutex_group(std::string const &group_name, bool required, std::string const &description)
{
if (group_name.empty()) {
std::cerr << "Error: Mutex group name cannot be empty" << std::endl;
ArgParser::do_exit(1);
}
if (_mutex_groups.find(group_name) != _mutex_groups.end()) {
std::cerr << "Error: Mutex group '" << group_name << "' already exists" << std::endl;
ArgParser::do_exit(1);
}
_mutex_groups.emplace(group_name, MutexGroup(group_name, required, description));
return *this;
}
// Add an option to a mutually exclusive group
ArgParser::Command &
ArgParser::Command::add_option_to_group(std::string const &group_name, std::string const &long_option,
std::string const &short_option, std::string const &description, std::string const &envvar,
unsigned arg_num, std::string const &default_value, std::string const &key)
{
if (group_name.empty()) {
std::cerr << "Error: Mutex group name cannot be empty" << std::endl;
ArgParser::do_exit(1);
}
auto it_mutex_group = _mutex_groups.find(group_name);
if (it_mutex_group == _mutex_groups.end()) {
std::cerr << "Error: Mutex group '" << group_name << "' not found" << std::endl;
ArgParser::do_exit(1);
}
// Add the option normally (this also sets _last_added_option)
add_option(long_option, short_option, description, envvar, arg_num, default_value, key);
// Track this option in the mutex group
it_mutex_group->second.options.push_back(long_option);
_option_to_group[long_option] = group_name;
return *this;
}
// add sub-command with only function
ArgParser::Command &
ArgParser::Command::add_command(std::string const &cmd_name, std::string const &cmd_description, Function const &f,
std::string const &key)
{
std::string lookup_key = key.empty() ? cmd_name : key;
check_command(cmd_name, lookup_key);
_subcommand_list[cmd_name] = ArgParser::Command(cmd_name, cmd_description, "", 0, f, lookup_key);
return _subcommand_list[cmd_name];
}
// add sub-command without args and function
ArgParser::Command &
ArgParser::Command::add_command(std::string const &cmd_name, std::string const &cmd_description, std::string const &cmd_envvar,
unsigned cmd_arg_num, Function const &f, std::string const &key)
{
std::string lookup_key = key.empty() ? cmd_name : key;
check_command(cmd_name, lookup_key);
_subcommand_list[cmd_name] = ArgParser::Command(cmd_name, cmd_description, cmd_envvar, cmd_arg_num, f, lookup_key);
return _subcommand_list[cmd_name];
}
ArgParser::Command &
ArgParser::Command::add_example_usage(std::string const &usage)
{
_example_usages.push_back(usage);
return *this;
}
// method used by help_message()
void
ArgParser::Command::output_command(std::ostream &out, std::string const &prefix) const
{
if (_name != parser_program_name) {
// a nicely formatted way to output command usage
std::string msg = prefix + _name;
// nicely formatted output
if (!_description.empty()) {
if (INDENT_ONE - static_cast<int>(msg.size()) < 0) {
// if the command msg is too long
std::cout << msg << "\n" << std::string(INDENT_ONE, ' ') << _description << std::endl;
} else {
std::cout << msg << std::string(INDENT_ONE - msg.size(), ' ') << _description << std::endl;
}
}
}
// recursive call
for (const auto &it : _subcommand_list) {
it.second.output_command(out, " " + prefix);
}
}
// a nicely formatted way to output option message for help.
void
ArgParser::Command::output_option() const
{
// Helper method to build argument message
auto arg_msg_builder = [](unsigned num) -> std::string {
if (num == 1) {
return {" <arg>"};
} else if (num == MORE_THAN_ZERO_ARG_N) {
return {" [<arg> ...]"};
} else if (num == MORE_THAN_ONE_ARG_N) {
return {" <arg> ..."};
} else {
return " <arg1> ... <arg" + std::to_string(num) + ">";
}
};
// First, output regular options (excluding those in mutex groups)
for (const auto &it : _option_list) {
// Skip if this option is in a mutex group (it will be displayed in the mutex group)
if (_option_to_group.find(it.first) != _option_to_group.end()) {
continue;
}
std::string msg;
if (!it.second.short_option.empty()) {
msg = it.second.short_option + ", ";
}
msg += it.first;
if (it.second.arg_num != 0) {
msg += arg_msg_builder(it.second.arg_num);
}
if (!it.second.default_value.empty()) {
if (INDENT_ONE - static_cast<int>(msg.size()) < 0) {
msg = msg + "\n" + std::string(INDENT_ONE, ' ') + it.second.default_value;
} else {
msg = msg + std::string(INDENT_ONE - msg.size(), ' ') + it.second.default_value;
}
}
// Build description with dependency info if applicable
std::string desc = it.second.description;
auto dep_it = _option_dependencies.find(it.first);
if (dep_it != _option_dependencies.end() && !dep_it->second.empty()) {
if (!desc.empty()) {
desc += " ";
}
desc += "(requires";
for (size_t i = 0; i < dep_it->second.size(); ++i) {
desc += " " + dep_it->second[i];
if (i < dep_it->second.size() - 1) {
desc += ",";
}
}
desc += ")";
}
if (!desc.empty()) {
if (INDENT_TWO - static_cast<int>(msg.size()) < 0) {
std::cout << msg << "\n" << std::string(INDENT_TWO, ' ') << desc << std::endl;
} else {
std::cout << msg << std::string(INDENT_TWO - msg.size(), ' ') << desc << std::endl;
}
}
}
// Then output mutually exclusive groups
for (const auto &[group_name, group] : _mutex_groups) {
std::cout << "\nGroup (" << group_name;
if (group.required) {
std::cout << ", required";
}
std::cout << ")";
if (!group.description.empty()) {
std::cout << " - " << group.description;
}
std::cout << std::endl;
for (const auto &option_name : group.options) {
auto const it = _option_list.find(option_name);
if (it != _option_list.end()) {
std::string msg{" "}; // Indent group options
if (!it->second.short_option.empty()) {
msg += it->second.short_option + ", ";
}
msg += it->first;
if (it->second.arg_num != 0) {
msg += arg_msg_builder(it->second.arg_num);
}
if (!it->second.default_value.empty()) {
if (INDENT_ONE - static_cast<int>(msg.size()) < 0) {
msg = msg + "\n" + std::string(INDENT_ONE, ' ') + it->second.default_value;
} else {
msg = msg + std::string(INDENT_ONE - msg.size(), ' ') + it->second.default_value;
}
}
if (!it->second.description.empty()) {
if (INDENT_TWO - static_cast<int>(msg.size()) < 0) {
std::cout << msg << "\n" << std::string(INDENT_TWO, ' ') << it->second.description << std::endl;
} else {
std::cout << msg << std::string(INDENT_TWO - msg.size(), ' ') << it->second.description << std::endl;
}
}
}
}
}
}
// helper method to handle the arguments and put them nicely in arguments
// can be switched to ts::errata
static std::string
handle_args(Arguments &ret, AP_StrVec &args, std::string const &name, unsigned arg_num, unsigned &index)
{
ArgumentData data;
ret.append(name, data);
// handle the args
if (arg_num == MORE_THAN_ZERO_ARG_N || arg_num == MORE_THAN_ONE_ARG_N) {
// infinite arguments
if (arg_num == MORE_THAN_ONE_ARG_N && args.size() <= index + 1) {
return "at least one argument expected by " + name;
}
for (unsigned j = index + 1; j < args.size(); j++) {
ret.append_arg(name, args[j]);
}
args.erase(args.begin() + index, args.end());
return "";
}
// finite number of argument handling
for (unsigned j = 0; j < arg_num; j++) {
if (args.size() < index + j + 2 || args[index + j + 1].empty()) {
return std::to_string(arg_num) + " argument(s) expected by " + name;
}
ret.append_arg(name, args[index + j + 1]);
}
// erase the used arguments and append the data to the return structure
args.erase(args.begin() + index, args.begin() + index + arg_num + 1);
index -= 1;
return "";
}
// Validate mutually exclusive groups
void
ArgParser::Command::validate_mutex_groups(Arguments &ret) const
{
// Check each mutex group
for (const auto &[group_name, group] : _mutex_groups) {
std::vector<std::string> used_options;
// Find which options from this group were used
for (const auto &option_name : group.options) {
auto it = _option_list.find(option_name);
if (it != _option_list.end()) {
// Check if this option was called
if (ret.get(it->second.key)) {
used_options.push_back(option_name);
}
}
}
// Validate: only one option from the group can be used
if (used_options.size() > 1) {
std::string error_msg = "Error: Options in mutex group '" + group_name + "' are mutually exclusive. Used: ";
for (size_t i = 0; i < used_options.size(); ++i) {
if (i > 0) {
error_msg += ", ";
}
error_msg += used_options[i];
}
help_message(error_msg);
}
// Validate: if group is required, at least one option must be used
if (group.required && used_options.empty()) {
std::string error_msg = "Error: One option from required mutex group '" + group_name + "' must be specified. Options: ";
for (size_t i = 0; i < group.options.size(); ++i) {
if (i > 0) {
error_msg += ", ";
}
error_msg += group.options[i];
}
help_message(error_msg);
}
}
}
// Specify that the last added option requires another option
ArgParser::Command &
ArgParser::Command::with_required(std::string const &required_option)
{
if (_last_added_option.empty()) {
std::cerr << "Error: with_required() must be called after add_option()" << std::endl;
ArgParser::do_exit(1);
}
// Validate that required option exists
if (_option_list.find(required_option) == _option_list.end()) {
std::cerr << "Error: Required option '" << required_option << "' not found" << std::endl;
ArgParser::do_exit(1);
}
_option_dependencies[_last_added_option].push_back(required_option);
return *this;
}
// Validate option dependencies
void
ArgParser::Command::validate_dependencies(Arguments &ret) const
{
for (const auto &[dependent, required_list] : _option_dependencies) {
// Get the key for the dependent option
auto it = _option_list.find(dependent);
if (it == _option_list.end()) {
continue;
}
const std::string &dep_key = it->second.key;
// Check if dependent option was used
if (ret.get(dep_key)) {
// Dependent option was used, check all required options
for (const auto &required : required_list) {
auto req_it = _option_list.find(required);
if (req_it == _option_list.end()) {
continue;
}
const std::string &req_key = req_it->second.key;
if (!ret.get(req_key)) {
std::string error_msg = "Option '" + dependent + "' requires '" + required + "' to be specified";
help_message(error_msg); // exit with status code 64 (EX_USAGE - command line usage error)
}
}
}
}
}
// Append the args of option to parsed data. Return true if there is any option called
void
ArgParser::Command::append_option_data(Arguments &ret, AP_StrVec &args, int index)
{
std::map<std::string, unsigned> check_map;
for (unsigned i = index; i < args.size(); i++) {
// find matches of the arg
if (args[i][0] == '-' && args[i][1] == '-' && args[i].find('=') != std::string::npos) {
// deal with --args=
std::string option_name = args[i].substr(0, args[i].find_first_of('='));
std::string value = args[i].substr(args[i].find_last_of('=') + 1);
if (value.empty()) {
help_message("missing argument for '" + option_name + "'");
}
auto it = _option_list.find(option_name);
if (it != _option_list.end()) {
ArgParser::Option cur_option = it->second;
// handle environment variable
if (!cur_option.envvar.empty()) {
const char *const env = getenv(cur_option.envvar.c_str());
ret.set_env(cur_option.key, nullptr != env ? env : "");
}
ret.append_arg(cur_option.key, value);
check_map[cur_option.long_option] += 1;
args.erase(args.begin() + i);
i -= 1;
}
} else {
// output version message
if ((args[i] == "--version" || args[i] == "-V") && _option_list.find("--version") != _option_list.end()) {
version_message();
}
// output help message
if ((args[i] == "--help" || args[i] == "-h") && _option_list.find("--help") != _option_list.end()) {
ArgParser::Command *command = this;
// find the correct level to output help message
for (unsigned i = 1; i < args.size(); i++) {
auto it = command->_subcommand_list.find(args[i]);
if (it == command->_subcommand_list.end()) {
break;
}
command = &it->second;
}
usage_return_code = 0;
command->help_message();
}
// deal with normal --arg val1 val2 ...
auto long_it = _option_list.find(args[i]);
auto short_it = _option_map.find(args[i]);
// long option match or short option match
if (long_it != _option_list.end() || short_it != _option_map.end()) {
ArgParser::Option cur_option;
if (long_it != _option_list.end()) {
cur_option = long_it->second;
} else {
cur_option = _option_list.at(short_it->second);
}
// handle the arguments
std::string err = handle_args(ret, args, cur_option.key, cur_option.arg_num, i);
if (!err.empty()) {
help_message(err);
}
// handle environment variable
if (!cur_option.envvar.empty()) {
const char *const env = getenv(cur_option.envvar.c_str());
ret.set_env(cur_option.key, nullptr != env ? env : "");
}
}
}
}
// check for wrong number of arguments for --arg=...
for (const auto &it : check_map) {
unsigned num = _option_list.at(it.first).arg_num;
if (num != it.second && num < MORE_THAN_ONE_ARG_N) {
help_message(std::to_string(_option_list.at(it.first).arg_num) + " arguments expected by " + it.first);
}
}
}
// Apply default values for options not explicitly set by the user.
// This must be called AFTER validate_dependencies() so that default values
// (e.g. --timeout "0") don't falsely trigger dependency checks.
void
ArgParser::Command::apply_option_defaults(Arguments &ret) const
{
for (const auto &it : _option_list) {
if (!it.second.default_value.empty() && ret.get(it.second.key).empty()) {
std::istringstream ss(it.second.default_value);
std::string token;
while (std::getline(ss, token, ' ')) {
ret.append_arg(it.second.key, token);
}
}
}
}
// Main recursive logic of Parsing
bool
ArgParser::Command::parse(Arguments &ret, AP_StrVec &args)
{
bool command_called = false;
// Only check the first remaining argument for command name to avoid
// treating arguments as commands (e.g., "metric match host" where "host" is an arg, not a command)
if (!args.empty() && _name == args[0]) {
command_called = true;
// Note: handle_args modifies its index parameter (designed for loop usage), but we
// discard the result. This causes unsigned underflow (0 - 1 = UINT_MAX) which is
// harmless since we don't use index afterward.
unsigned index{0};
// handle the option
append_option_data(ret, args, index);
// handle the action
if (_f) {
ret._action = _f;
}
const std::string err = handle_args(ret, args, _key, _arg_num, index);
if (!err.empty()) {
help_message(err);
}
// set ENV var
if (!_envvar.empty()) {
const char *const env = getenv(_envvar.c_str());
ret.set_env(_key, nullptr != env ? env : "");
}
// Validate mutually exclusive groups
validate_mutex_groups(ret);
// Validate option dependencies
validate_dependencies(ret);
// Apply default values after validation so that defaults don't
// trigger dependency checks (e.g. --timeout with default "0"
// should not require --monitor when not explicitly used).
apply_option_defaults(ret);
}
if (command_called) {
bool flag = false;
// recursively call subcommand
for (auto &it : _subcommand_list) {
if (it.second.parse(ret, args)) {
flag = true;
break;
}
}
// check for command required
if (!flag && _command_required) {
std::ostringstream msg;
if (!args.empty()) {
msg << "No sub-command '" << args[0] << "' found for " << _name;
} else {
msg << "No sub-command found for " << _name;
}
help_message(msg.str());
}
if (_name == parser_program_name) {
// if we are at the top level
return flag;
}
}
return command_called;
}
ArgParser::Command &
ArgParser::Command::require_commands()
{
_command_required = true;
return *this;
}
ArgParser::Command &
ArgParser::Command::set_default()
{
default_command = _name;
return *this;
}
//=========================== Arguments class ================================
Arguments::Arguments() {}
Arguments::~Arguments() {}
ArgumentData
Arguments::get(std::string const &name)
{
if (_data_map.find(name) != _data_map.end()) {
_data_map[name]._is_called = true;
return _data_map[name];
}
return ArgumentData();
}
void
Arguments::append(std::string const &key, ArgumentData const &value)
{
// perform overwrite for now
_data_map[key] = value;
}
void
Arguments::append_arg(std::string const &key, std::string const &value)
{
_data_map[key]._values.push_back(value);
}
void
Arguments::set_env(std::string const &key, std::string const &value)
{
// perform overwrite for now
_data_map[key]._env_value = value;
}
void
Arguments::show_all_configuration() const
{
for (const auto &it : _data_map) {
std::cout << "name: " + it.first << std::endl;
std::string msg;
msg = "args value:";
for (const auto &it_data : it.second._values) {
msg += " " + it_data;
}
std::cout << msg << std::endl;
std::cout << "env value: " + it.second._env_value << std::endl << std::endl;
}
}
// invoke the function with the args
void
Arguments::invoke()
{
if (_action) {
// call the std::function
_action();
} else {
throw std::runtime_error("no function to invoke");
}
}
bool
Arguments::has_action() const
{
return _action != nullptr;
}
//=========================== ArgumentData class ================================
std::string const &
ArgumentData::env() const noexcept
{
return _env_value;
}
std::string const &
ArgumentData::at(unsigned index) const
{
if (index >= _values.size()) {
throw std::out_of_range("argument not found at index: " + std::to_string(index));
}
return _values.at(index);
}
std::string const &
ArgumentData::value() const noexcept
{
if (_values.empty()) {
// To prevent compiler warning
static const std::string empty = "";
return empty;
}
return _values.at(0);
}
size_t
ArgumentData::size() const noexcept
{
return _values.size();
}
bool
ArgumentData::empty() const noexcept
{
return _values.empty() && _env_value.empty();
}
AP_StrVec::const_iterator
ArgumentData::begin() const noexcept
{
return _values.begin();
}
AP_StrVec::const_iterator
ArgumentData::end() const noexcept
{
return _values.end();
}
// protected method for testing
/*static*/ void
ArgParser::set_test_mode(bool test)
{
_test_mode = test;
}
// protected method for testing
/*static*/ void
ArgParser::do_exit(int code)
{
if (_test_mode) {
throw std::runtime_error("Test mode: exit with code " + std::to_string(code));
}
exit(code);
}
} // namespace ts