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 = nullptr);
55
57 int parse (const std::vector<std::string>& args, int* stop = nullptr);
58
61 bool next (std::string& opt, std::string& optarg, char sep = '|');
62
64 bool next (std::string& opt, std::vector<std::string>& optarg);
66
69 int getopt (const std::string& option, std::string& optarg, char sep = '|') const;
70
72 int getopt (const std::string& option, std::vector<std::string>& optarg) const;
73
75 int getopt (char option, std::string& optarg, char sep = '|') const;
76
78 int getopt (char option, std::vector<std::string>& optarg) const;
80
83 bool hasopt (const std::string& option) const;
84
86 bool hasopt (char option) const;
88
90 const std::string synopsis () const;
91
93 const std::string description (size_t indent_size = 2) const;
94
96 const std::string& appname () const;
97
98private:
99 struct opt
100 {
101 char oshort; // short form
102 std::string olong; // long form
103 char flag; // argument type
104 std::string arg_descr; // argument description
105 std::vector<std::string> arg; // actual argument(s)
106 int count; // number of occurrences
107 };
108
109 std::vector<opt>::const_iterator find_option (const std::string& opt) const;
110 std::vector<opt>::const_iterator find_option (char opt) const;
111 void format_arg (std::string& str, const opt& option, char sep) const;
112
113 std::vector<opt> optlist;
114 std::vector<opt> cmd;
115 std::vector<opt>::iterator nextop;
116 std::string app;
117};
118
122inline const std::string& OptParser::appname () const
123{
124 return app;
125}
126
130inline bool OptParser::hasopt (const std::string& option) const
131{
132 return find_option (option) != cmd.end ();
133}
134
139inline bool OptParser::hasopt (char option) const
140{
141 return find_option (option) != cmd.end ();
142}
143
150
151inline int OptParser::getopt (const std::string& option, std::vector<std::string>& optarg) const
152{
153 optarg.clear ();
154 auto p = find_option (option);
155 if (p == cmd.end ())
156 return 0;
157
158 optarg = p->arg;
159 return p->count;
160}
161
168inline int OptParser::getopt (char option, std::vector<std::string>& optarg) const
169{
170 optarg.clear ();
171 auto p = find_option (option);
172 if (p == cmd.end ())
173 return 0;
174
175 optarg = p->arg;
176 return p->count;
177}
178
179} // namespace mlib
int parse(int argc, const char *const *argv, int *stop=nullptr)
Parse a command line.
Definition options.cpp:242
int getopt(const std::string &option, std::string &optarg, char sep='|') const
Definition options.cpp:466
const std::string synopsis() const
Return a nicely formatted syntax string containing all the options.
Definition options.cpp:490
OptParser()
Initialize parser.
Definition options.cpp:97
bool hasopt(const std::string &option) const
Definition options.h:130
const std::string description(size_t indent_size=2) const
Return options description.
Definition options.cpp:558
bool next(std::string &opt, std::string &optarg, char sep='|')
Definition options.cpp:415
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
const std::string & appname() const
Return program name.
Definition options.h:122