Skip to content

Commit f59c107

Browse files
author
Fytch
committed
implement print_data in option and parser
1 parent 279bcf2 commit f59c107

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

ProgramOptions.hxx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,62 @@ namespace po {
16331633
}
16341634
return result.error;
16351635
}
1636+
1637+
void print_data( std::ostream& stream ) const {
1638+
if( !available() ) {
1639+
if( is_multi() )
1640+
stream << "[]";
1641+
else
1642+
stream << "<unavailable>";
1643+
return;
1644+
}
1645+
1646+
void( *printer )( std::ostream&, value const& );
1647+
switch( get_type() ) {
1648+
case void_:
1649+
printer = []( std::ostream& s, value const& /*v*/ ){ s << "<void>"; };
1650+
break;
1651+
case string:
1652+
printer = []( std::ostream& s, value const& v ){ s << '"' << v.string << '"'; };
1653+
break;
1654+
case i32:
1655+
printer = []( std::ostream& s, value const& v ){ s << v.i32; };
1656+
break;
1657+
case i64:
1658+
printer = []( std::ostream& s, value const& v ){ s << v.i64; };
1659+
break;
1660+
case u32:
1661+
printer = []( std::ostream& s, value const& v ){ s << v.u32; };
1662+
break;
1663+
case u64:
1664+
printer = []( std::ostream& s, value const& v ){ s << v.u64; };
1665+
break;
1666+
case f32:
1667+
printer = []( std::ostream& s, value const& v ){ s << v.f32; };
1668+
break;
1669+
case f64:
1670+
printer = []( std::ostream& s, value const& v ){ s << v.f64; };
1671+
break;
1672+
default:
1673+
assert( false );
1674+
}
1675+
1676+
if( is_multi() )
1677+
stream << "[ ";
1678+
value_vector_base const& data = get_vector();
1679+
auto begin = data.begin();
1680+
auto end = data.end();
1681+
if( begin != end ) {
1682+
for( ;; ) {
1683+
( *printer )( stream, *begin++ );
1684+
if( begin == end )
1685+
break;
1686+
stream << ", ";
1687+
}
1688+
}
1689+
if( is_multi() )
1690+
stream << " ]";
1691+
}
16361692
};
16371693

16381694
class parser {
@@ -1971,6 +2027,18 @@ namespace po {
19712027
return operator_brackets_helper( std::move( designator ) );
19722028
}
19732029

2030+
void print_data( std::ostream& stream, char delim = ':' ) const {
2031+
for( auto iter = m_order.begin(); iter != m_order.end(); ++iter ) {
2032+
auto& opt = **iter;
2033+
if( opt.first.empty() )
2034+
stream << "<unnamed>";
2035+
else
2036+
stream << opt.first;
2037+
stream << delim;
2038+
opt.second.print_data(stream);
2039+
stream << '\n';
2040+
}
2041+
}
19742042
void print_help( std::ostream& stream ) const {
19752043
enum : std::size_t {
19762044
console_width = 80 - 1, // -1 because writing until the real end returns the carriage

0 commit comments

Comments
 (0)