example_name.cc
This example demonstrates how to use name classes.
#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::setw;
using std::left;
namespace
{
template <typename NameType_>
void show_name(const std::string & type_string, const std::string & value)
{
cout << type_string << " " << value << ":" << endl;
try
{
NameType_ name(value);
cout << " " << left << setw(24) << "To stream:" << " " << name << endl;
cout << " " << left << setw(24) << "stringify(n).length:"
<< " " << stringify(name).length() << endl;
}
catch (const NameError & e)
{
cout << " " << left << setw(24) << "Exception:" << " '" <<
e.message() << "' (" << e.what() << ")" << endl;
}
cout << endl;
}
}
int main(int argc, char * argv[])
{
try
{
CommandLine::get_instance()->run(argc, argv,
"example_name", "EXAMPLE_NAME_OPTIONS", "EXAMPLE_NAME_CMDLINE");
show_name<SetName>("SetName", "world");
show_name<SetName>("SetName", "not*valid");
show_name<PackageNamePart>("PackageNamePart", "fred");
show_name<PackageNamePart>("PackageNamePart", "not/valid");
show_name<QualifiedPackageName>("QualifiedPackageName", "cat/pkg");
show_name<QualifiedPackageName>("QualifiedPackageName", "not-valid");
QualifiedPackageName q1("cat/pkg"), q2(CategoryNamePart("cat") + PackageNamePart("pkg"));
cout << q1 << " " << (q1 == q2 ? "==" : "!=") << " " << q2 << endl;
cout << q1 << " has category '" << q1.category << "' and package part '" << q1.package << "'" << endl;
}
catch (const Exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.backtrace("\n * ")
<< e.message() << " (" << e.what() << ")" << endl;
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * Unknown exception type. Ouch..." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}