MLIB
Loading...
Searching...
No Matches
options.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2024) Licensed under MIT License.
3 This is part of MLIB project. See LICENSE file for full license terms.
4*/
5
22#pragma once
23
24#include <string>
25#include <vector>
26
27#if __has_include("defs.h")
28#include "defs.h"
29#endif
30
31namespace mlib {
32
34{
35public:
36 explicit OptParser ();
37
39 OptParser (std::vector<const char*>& list);
40
42 OptParser (std::initializer_list<const char*> list);
43
45 OptParser (const char** list);
46
48 void set_options (std::vector<const char*>& list);
49
51 void add_option (const char* descr);
52
54 int parse (int argc, const char* const* argv, int* stop = 0);
55
58 bool next (std::string& opt, std::string& optarg, char sep = '|');
59
61 bool next (std::string& opt, std::vector<std::string>& optarg);
63
66 int getopt (const std::string& option, std::string& optarg, char sep = '|') const;
67
69 int getopt (const std::string& option, std::vector<std::string>& optarg) const;
70
72 int getopt (char option, std::string& optarg, char sep = '|') const;
73
75 int getopt (char option, std::vector<std::string>& optarg) const;
77
80 bool hasopt (const std::string& option) const;
81
83 bool hasopt (char option) const;
85
87 const std::string synopsis () const;
88
90 const std::string description (size_t indent_size = 2) const;
91
93 const std::string& appname () const;
94
95private:
96 struct opt
97 {
98 char oshort; // short form
99 std::string olong; // long form
100 char flag; // argument type
101 std::string arg_descr; // argument description
102 std::vector<std::string> arg; // actual argument(s)
103 int count; // number of occurrences
104 };
105
106 std::vector<opt>::const_iterator find_option (const std::string& opt) const;
107 std::vector<opt>::const_iterator find_option (char opt) const;
108 void format_arg (std::string& str, const opt& option, char sep) const;
109
110 std::vector<opt> optlist;
111 std::vector<opt> cmd;
112 std::vector<opt>::iterator nextop;
113 std::string app;
114};
115
119inline const std::string& OptParser::appname () const
120{
121 return app;
122}
123
127inline bool OptParser::hasopt (const std::string& option) const
128{
129 return find_option (option) != cmd.end ();
130}
131
136inline bool OptParser::hasopt (char option) const
137{
138 return find_option (option) != cmd.end ();
139}
140
148inline int OptParser::getopt (const std::string& option, std::vector<std::string>& optarg) const
149{
150 optarg.clear ();
151 auto p = find_option (option);
152 if (p == cmd.end ())
153 return 0;
154
155 optarg = p->arg;
156 return p->count;
157}
158
165inline int OptParser::getopt (char option, std::vector<std::string>& optarg) const
166{
167 optarg.clear ();
168 auto p = find_option (option);
169 if (p == cmd.end ())
170 return 0;
171
172 optarg = p->arg;
173 return p->count;
174}
175
176} // namespace mlib
Command Line Parsing class.
Definition options.h:34
int getopt(const std::string &option, std::string &optarg, char sep='|') const
Definition options.cpp:447
const std::string synopsis() const
Return a nicely formatted syntax string containing all the options.
Definition options.cpp:471
OptParser()
Initialize parser.
Definition options.cpp:97
bool hasopt(const std::string &option) const
Definition options.h:127
const std::string description(size_t indent_size=2) const
Return options description.
Definition options.cpp:539
bool next(std::string &opt, std::string &optarg, char sep='|')
Definition options.cpp:396
void add_option(const char *descr)
Add a new option descriptor to the list of options.
Definition options.cpp:200
void set_options(std::vector< const char * > &list)
Set list of valid options.
Definition options.cpp:141
int parse(int argc, const char *const *argv, int *stop=0)
Parse a command line.
Definition options.cpp:242
const std::string & appname() const
Return program name.
Definition options.h:119