Newer
Older
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
public:
//---------------------------------------------------------------
using value_type = section;
using const_iterator = section_store::const_iterator;
using size_type = section_store::size_type;
//---------------------------------------------------------------
man_page&
append_section(string title, string content)
{
sections_.emplace_back(std::move(title), std::move(content));
return *this;
}
//-----------------------------------------------------
man_page&
prepend_section(string title, string content)
{
sections_.emplace(sections_.begin(),
std::move(title), std::move(content));
return *this;
}
//---------------------------------------------------------------
const section& operator [] (size_type index) const noexcept {
return sections_[index];
}
//---------------------------------------------------------------
size_type size() const noexcept { return sections_.size(); }
bool empty() const noexcept { return sections_.empty(); }
//---------------------------------------------------------------
const_iterator begin() const noexcept { return sections_.begin(); }
const_iterator end() const noexcept { return sections_.end(); }
//---------------------------------------------------------------
man_page& program_name(const string& n) {
progName_ = n;
return *this;
}
man_page& program_name(string&& n) {
progName_ = std::move(n);
return *this;
}
const string& program_name() const noexcept {
return progName_;
}
//---------------------------------------------------------------
man_page& section_row_spacing(int rows) {
sectionSpc_ = rows > 0 ? rows : 0;
return *this;
}
int section_row_spacing() const noexcept { return sectionSpc_; }
private:
int sectionSpc_ = 1;
section_store sections_;
string progName_;
};
/*************************************************************************//**
*
* @brief generates man sections from command line parameters
* with sections "synopsis" and "options"
*
*****************************************************************************/
inline man_page
make_man_page(const group& params,
doc_string progname = "",
const doc_formatting& fmt = doc_formatting{})
{
man_page man;
man.append_section("SYNOPSIS", usage_lines(params,progname,fmt).str());
man.append_section("OPTIONS", documentation(params,fmt).str());
return man;
}
/*************************************************************************//**
*
* @brief generates man page based on command line parameters
*
*****************************************************************************/
template<class OStream>
OStream&
operator << (OStream& os, const man_page& man)
{
bool first = true;
const auto secSpc = doc_string(man.section_row_spacing() + 1, '\n');
for(const auto& section : man) {
if(!section.content().empty()) {
if(first) first = false; else os << secSpc;
if(!section.title().empty()) os << section.title() << '\n';
os << section.content();
}
}
os << '\n';
return os;
}
/*************************************************************************//**
*
* @brief printing methods for debugging command line interfaces
*
*****************************************************************************/
namespace debug {
/*************************************************************************//**
*
* @brief prints first flag or value label of a parameter
*
*****************************************************************************/
inline doc_string doc_label(const parameter& p)
{
if(!p.flags().empty()) return p.flags().front();
if(!p.label().empty()) return p.label();
return doc_string{"<?>"};
}
inline doc_string doc_label(const group&)
{
return "<group>";
}
inline doc_string doc_label(const pattern& p)
{
return p.is_group() ? doc_label(p.as_group()) : doc_label(p.as_param());
}
/*************************************************************************//**
*
* @brief prints parsing result
*
*****************************************************************************/
template<class OStream>
void print(OStream& os, const parsing_result& result)
{
for(const auto& m : result) {
os << "#" << m.index() << " " << m.arg() << " -> ";
auto p = m.param();
if(p) {
os << doc_label(*p) << " \t";
if(m.repeat() > 0) {
os << (m.bad_repeat() ? "[bad repeat " : "[repeat ")
<< m.repeat() << "]";
}
if(m.blocked()) os << " [blocked]";
if(m.conflict()) os << " [conflict]";
os << '\n';
}
else {
os << " [unmapped]\n";
}
}
for(const auto& m : result.missing()) {
auto p = m.param();
if(p) {
os << doc_label(*p) << " \t";
os << " [missing after " << m.after_index() << "]\n";
}
}
}
/*************************************************************************//**
*
* @brief prints parameter label and some properties
*
*****************************************************************************/
template<class OStream>
void print(OStream& os, const parameter& p)
{
if(p.blocking()) os << '!';
if(!p.required()) os << '[';
os << doc_label(p);
if(p.repeatable()) os << "...";
if(!p.required()) os << "]";
}
//-------------------------------------------------------------------
template<class OStream>
void print(OStream& os, const group& g, int level = 0);
/*************************************************************************//**
*
* @brief prints group or parameter; uses indentation
*
*****************************************************************************/
template<class OStream>
void print(OStream& os, const pattern& param, int level = 0)
{
if(param.is_group()) {
print(os, param.as_group(), level);
}
else {
os << doc_string(4*level, ' ');
print(os, param.as_param());
}
}
/*************************************************************************//**
*
* @brief prints group and its contents; uses indentation
*
*****************************************************************************/
template<class OStream>
void print(OStream& os, const group& g, int level)
{
auto indent = doc_string(4*level, ' ');
os << indent;
if(g.blocking()) os << '!';
if(g.joinable()) os << 'J';
os << (g.exclusive() ? "(|\n" : "(\n");
for(const auto& p : g) {
print(os, p, level+1);
}
os << '\n' << indent << (g.exclusive() ? "|)" : ")");
if(g.repeatable()) os << "...";
os << '\n';
}
} // namespace debug
} //namespace clipp
#endif