10 ParseError(
const char* msg) : std::runtime_error(msg) { }
11 ParseError(
const std::string& msg) : std::runtime_error(msg) { }
22 virtual bool matches(
const std::string& flag)
const = 0;
23 virtual void parse_item(
const std::vector<std::string>& arguments) = 0;
25 virtual std::string
printable(
int description_column = -1,
int* chars_before_desc=NULL)
const = 0;
28 virtual std::string
flag_name()
const = 0;
30 void parse(
const std::string& name,
const std::vector<std::string>& arguments){
35 ss <<
"Flag \"" << name <<
"\" expected at least one argument";
38 <<
" argument(s) and received " << arguments.size();
56 std::stringstream ss(flag_list);
60 if(temp.length() == 1){
61 flags.push_back(
"-" + temp);
62 }
else if (temp.length() > 1) {
63 flags.push_back(
"--" + temp);
71 for(
auto flag :
flags){
72 if(flag.length() > output.length()){
79 virtual bool matches(
const std::string& flag)
const {
81 if(flag.at(0)!=
'-' &&
flags.size()==0){
109 virtual std::string
printable(
int description_column = -1,
int* chars_before_desc=NULL)
const {
110 std::stringstream ss;
114 bool has_singlechar_flag =
false;
115 for(
auto flag :
flags){
116 if(flag.length()==2){
118 has_singlechar_flag =
true;
121 for(
auto flag : flags){
122 if(flag.length()!=2){
123 if(has_singlechar_flag){
127 if(has_singlechar_flag){
137 auto chars = ss.tellp();
138 if(chars_before_desc){
139 *chars_before_desc = chars;
142 if(description_column != -1 &&
143 chars < description_column){
144 for(
unsigned int i=0; i<description_column-chars; i++){
171 virtual void parse_item(
const std::vector<std::string>& arguments){
172 std::stringstream ss(arguments[0]);
188 stored_default_value(false) {
189 *output_location = stored_default_value;
194 stored_default_value =
value;
213 :
ArgParseConfig<std::vector<T> >(flag), output_location(output_location),
214 num_arguments_expected(-1) { }
216 virtual void parse_item(
const std::vector<std::string>& arguments){
217 for(
auto arg : arguments){
218 std::stringstream ss(arg);
248 bool double_dash_encountered =
false;
253 std::string arg = argv[iarg++];
255 if(arg.at(0) !=
'-' ||
256 double_dash_encountered){
258 }
else if(arg.substr(0,2) ==
"--"){
266 if(val->is_required() && !val->is_present()){
267 std::stringstream ss;
268 ss <<
"Required argument \"" << val->flag_name() <<
"\" is not present";
283 return option(
"", output_location);
286 void print(std::ostream& out)
const {
292 item->printable(-1, &length);
293 max_length = std::max(length, max_length);
296 for(
auto it = values.begin(); it!=values.end(); it++){
299 if(it!=values.end()-1){
307 std::string arg = argv[iarg-1];
308 std::vector<std::string> flag_args;
310 size_t equals_index = arg.find(
"=");
311 if(equals_index == std::string::npos){
316 flag = arg.substr(0, equals_index);
321 if(equals_index == std::string::npos){
324 flag_args.push_back(arg.substr(equals_index+1));
327 item.
parse(flag, flag_args);
331 std::string arg = argv[iarg-1];
332 std::string flag = arg.substr(0,2);
336 for(
unsigned int ichar=1; ichar<arg.length(); ichar++){
337 std::string flag =
"-" + arg.substr(ichar,1);
338 std::vector<std::string> flag_args;
342 if(arg.length() == 2){
345 item.
parse(flag, flag_args);
348 std::vector<std::string> flag_args{arg.substr(2)};
349 item.
parse(flag, flag_args);
355 std::string arg = argv[iarg-1];
356 std::vector<std::string> flag_args{arg};
359 item.
parse(arg, flag_args);
363 std::vector<std::string>
argument_list(
int argc,
char** argv,
int& iarg,
int max_args){
364 std::vector<std::string> output;
365 bool read_extra =
false;
367 (max_args==-1 || output.size() < size_t(max_args))){
368 std::string next_arg = argv[iarg++];
369 if(next_arg.at(0) ==
'-'){
373 output.push_back(next_arg);
384 if(val->matches(flag)){
389 std::stringstream ss;
391 ss <<
"Unknown option: \"" << flag <<
"\"";
393 ss <<
"Was passed \"" << flag <<
"\" as a non-option argument, when no non-option arguments are allowed";