diff options
author | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
---|---|---|
committer | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
commit | c44379cc7d9c7aa113989237ab0f56db12aa5219 (patch) | |
tree | 66a84b20d47f2269d8bdc6e0323f338763424d3a /ACE/contrib/utility/Example/CommandLine | |
parent | 3aff90f4a822fcf5d902bbfbcc9fa931d6191a8c (diff) | |
download | ATCD-c44379cc7d9c7aa113989237ab0f56db12aa5219.tar.gz |
Repo restructuring
Diffstat (limited to 'ACE/contrib/utility/Example/CommandLine')
-rw-r--r-- | ACE/contrib/utility/Example/CommandLine/Foo/command.cpp | 86 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/CommandLine/Foo/foo.cpp | 183 |
2 files changed, 269 insertions, 0 deletions
diff --git a/ACE/contrib/utility/Example/CommandLine/Foo/command.cpp b/ACE/contrib/utility/Example/CommandLine/Foo/command.cpp new file mode 100644 index 00000000000..50b78a15350 --- /dev/null +++ b/ACE/contrib/utility/Example/CommandLine/Foo/command.cpp @@ -0,0 +1,86 @@ +#include <string> +#include <iostream> + +using std::cerr; +using std::endl; + +class Command +{ +public: + enum Value + { + HELP = 0, + VERSION, + DEFAULT + }; + + Command (Value v = Command::DEFAULT) + : v_ (v) + { + } + + operator Value () const + { + return v_; + } + + friend std::ostream& + operator<< (std::ostream& os, Command c); + + friend std::istream& + operator>> (std::istream& is, Command& c); + +private: + Value v_; + static char* labels_[]; +}; + +char* Command::labels_[] = {"help", "version", "default"}; + + +std::ostream& +operator<< (std::ostream& os, Command c) +{ + return os << Command::labels_[c.v_]; +} + +std::istream& +operator>> (std::istream& is, Command& c) +{ + std::string s; + is >> s; + if (is) + { + if (s == Command::labels_[Command::HELP]) c.v_ = Command::HELP; + else if (s == Command::labels_[Command::VERSION]) c.v_ = Command::VERSION; + else is.setstate (std::ios::failbit); + } + return is; +} + +int +main () +{ + Command c = Command::HELP; + + c = Command::DEFAULT; + + Command c1 (Command::HELP); + + c = c1; + + cerr << c << endl; + + switch (c) + { + case Command::HELP: + { + cerr << "iiihuuu!!!" << endl; + } + } + + std::cin >> c1; + if (std::cin) cerr << c1 << endl; + else cerr << "*failed" << endl; +} +//$Id$ diff --git a/ACE/contrib/utility/Example/CommandLine/Foo/foo.cpp b/ACE/contrib/utility/Example/CommandLine/Foo/foo.cpp new file mode 100644 index 00000000000..a389f8d822a --- /dev/null +++ b/ACE/contrib/utility/Example/CommandLine/Foo/foo.cpp @@ -0,0 +1,183 @@ +#include <typeinfo> +#include <string> +#include <iostream> + + +namespace CommandLine +{ + struct Parser + { + }; +} + +using std::string; +using std::cerr; +using std::endl; + +using namespace CommandLine; + +class Command +{ +public: + enum Value + { + HELP, + VERSION, + DEFAULT + }; + + Command (Value v = Command::DEFAULT) + : v_ (v) + { + } + + operator Value () const + { + return v_; + } + +private: + Value v_; +}; + + +int +version (); + +int +help (int argc, char* argv[]); + +int +main (int argc, char* argv[]) +{ + + // Step 1: determine command + // + // * there is usually one command + // * command can be optional + // * command usually takes up one argument + // + + CommandParser<Command> cp; + + switch (cp.parse (argc, argv)) + { + case Command::VERSION: + { + return version (); + } + case Command::HELP: + { + return help (argc, argv); + } + } + + // Step 2: parse options + // + // * options are usually optional + // * options are usually position-independant + // * options usually do not repeat + // * options can take up more than one argument + // + + OptionMap om; + + CompositeParser op; + + op.add (OptionParser<string> ("string", "--string", "-s")); + op.add (OptionParser<unsigned long> ("number", "--number", "-n")); + + while (argc != 1 && !op.empty ()) + { + om.insert (op.parse (argc, argv)); + } + + // Step 3: parse operands + // + // * operands usually position-dependant + // * operand usually take up one argument + // + + OperandParser<string> odp; + + string str = odp.parse (argc, argv); + + unsigned long num = 0; + + if (argc != 1) + { + OperandParser<unsigned long> op; + num = op.parse (argc, argv); + } + + string s = om.count ("string") ? om["string"] : "default"; + unsigned long l = om["number"]; + + // om.at () + // om.get () + // om.resolve () + // om.option () + // om.value () + + cerr << "opreation settings are:" << endl << endl + << "option string : " << om.get<string> ("string", "default") << endl + << "option number : " << om.get ("number", 10UL) << endl + << "operand string : " << str << endl + << "operand number : " << num << endl; +} + + +// +// +// +int +version () +{ + cerr << "foo 1.0" << endl; + return 0; +} + + +// +// +// +int +help (int argc, char* argv[]) +{ + Command subject; + + if (argc != 1) + { + OperandParser<Command> op; + subject = op.parse (argc, argv); + } + + switch (subject) + { + case Command::HELP: + { + cerr << "foo help [<command>]" << endl << endl + << "\t If <command> is specified then print extended help" << endl + << "\t information for specified command. Otherwise print" << endl + << "\t general usage information." << endl; + break; + } + case Command::VERSION: + { + cerr << "foo version" << endl << endl + << "\t Print version information." << endl; + break; + } + default: + { + cerr << "foo version" << endl + << "foo help [<command>]" << endl + << "foo [-s|--string <str>] [-n|--number <num>] <str> [<num>]" + << endl; + break; + } + } + + return 0; +} +//$Id$ |