CmdLine.cpp

CmdLine example program Should be installed as ui-utilcpp-cmdline along with the library.

// Local
#include "config.h"
// STDC++
#include <iostream>
#include <fstream>
#include <vector>
// POSIX C
// C++ Libraries
#include <ui-utilcpp/CmdLine.hpp>
class TestCmd: public UI::Util::CmdLine::Cmd
{
public:
TestCmd()
:Cmd("test", "Test command")
{
addArg("testarg", "Mandatory test argument documentation");
addOptArg("testoptarg", "Optional test argument documentation");
}
private:
int runCmd()
{
cl_->os() << "Test command running" << std::endl;
cl_->os() << "Arg(1) == " << getArg(1) << std::endl;
cl_->os() << "Arg(2) == " << getArg(2) << std::endl;
// Return "0" for ok, anything else for error
return(0);
}
};
class TestCmdLine: public UI::Util::CmdLine::CmdLine
{
public:
TestCmdLine(std::istream * is, std::ostream * os)
:CmdLine(is, os, &std::cerr, "Test Command Line", "\nTest Prompt# ")
{
// You can set some intital variables here
setVar("NATIVE_MOJO", "YES");
// You add any amount of Commands here
add(new UI::Util::CmdLine::HeaderCmd("Custom command line functions"));
add(new TestCmd());
}
~TestCmdLine()
{}
};
int main()
{
return(TestCmdLine(0, &std::cout).run());
}
CmdLine.hpp
Utility for easy command line interfaces.
UI::Util::CmdLine::CmdLine
Simple Command Line interface.
Definition CmdLine.hpp:50
UI::Util::CmdLine::CmdLine::setVar
void setVar(std::string const &key, std::string const &value)
Set variable value.
Definition CmdLine.cpp:498
UI::Util::CmdLine::CmdLine::add
void add(Cmd *cmd)
Add a command to the command line.
Definition CmdLine.cpp:433
UI::Util::CmdLine::Cmd
Represents a command.
Definition CmdLine.hpp:119
UI::Util::CmdLine::Cmd::addArg
void addArg(std::string const &name, std::string const &help="No help for this option")
Add mandatory argument. Use this in constructors of custom Cmd classes.
Definition CmdLine.cpp:52
UI::Util::CmdLine::Cmd::Cmd
Cmd(std::string const &name, std::string const &help="No help for this command")
Standard constructor.
Definition CmdLine.cpp:32
UI::Util::CmdLine::Cmd::getArg
std::string getArg(int i) const
Get the argument of a parsed command.
Definition CmdLine.cpp:145
UI::Util::CmdLine::Cmd::addOptArg
void addOptArg(std::string const &name, std::string const &help="No help for this option")
Add optional argument. Use this in constructors of custom Cmd classes.
Definition CmdLine.cpp:58
UI::Util::CmdLine::HeaderCmd
Pseudo command class to add header like seperators in help descriptions.
Definition CmdLine.hpp:219