diff options
author | jai <jai@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2007-01-29 21:15:18 +0000 |
---|---|---|
committer | jai <jai@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 2007-01-29 21:15:18 +0000 |
commit | b71531b42b3325fd6079a7039aae8641262c8adf (patch) | |
tree | a5b9aa16924c541fcb424ee9460b1ac7f5a89352 /modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp | |
parent | a0f67cc97c0050d907145e312135b60c0125e56e (diff) | |
download | ATCD-b71531b42b3325fd6079a7039aae8641262c8adf.tar.gz |
branching/taggingDS-main
Diffstat (limited to 'modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp')
-rw-r--r-- | modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp b/modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp new file mode 100644 index 00000000000..5f7d1d603bd --- /dev/null +++ b/modules/CIAO/CCF/CCF/CodeGenerationKit/CommandLineParser.cpp @@ -0,0 +1,120 @@ +// file : CCF/CodeGenerationKit/CommandLineParser.cpp +// author : Boris Kolpackov <boris@dre.vanderbilt.edu> +// cvs-id : $Id$ + +#include "CCF/CodeGenerationKit/CommandLineParser.hpp" + +#include <string> +#include <iostream> + +using std::cerr; +using std::endl; + +bool +parse (int argc, char* argv[], CL::Description const& cld, CommandLine& cl) +{ + cl.command = argv[0]; + + bool seen_double_dash (false); + + for (int i (1); i < argc; ++i) + { + std::string arg (argv[i]); + + if (seen_double_dash) + { + cl.arguments.push_back (arg); + continue; + } + + if (arg == "--") + { + seen_double_dash = true; + continue; + } + + if (arg.length () > 0 && arg[0] == '-') + { + if (arg.length () > 1 && arg[1] == '-') + { + // Double-dash (long) option. + // + + std::string op (arg, 2); + + if (CL::OptionDescription const* d = cld.find_option (op)) + { + if (d->type () == CL::OptionType::value) + { + if (++i >= argc) + { + cerr << argv[0] << ": argument expected for option '--" + << op << "'" << endl; + return false; + } + + // cerr << "--" << op << ": " << argv[i] << endl; + + cl.options.push_back (CommandLine::Option (op, argv[i])); + } + else + cl.options.push_back (CommandLine::Option (op)); + + continue; + } + } + else + { + // Single-dash (short) option. We support two formats: '-D foo' and + // -Dfoo. + // + std::string op (arg, 1, 1); + + if (CL::OptionDescription const* d = cld.find_option (op)) + { + if (d->type () == CL::OptionType::value) + { + std::string value; + + if (arg.length () > 2) + { + value.assign (arg, 2, arg.size () - 2); + } + else + { + if (++i >= argc) + { + cerr << argv[0] << ": argument expected for option '-" + << op << "'" << endl; + return false; + } + + value = argv[i]; + } + + // cerr << "-" << op << ": " << value << endl; + + cl.options.push_back (CommandLine::Option (op, value)); + } + else + { + if (arg.length () > 2) + { + cerr << argv[0] << ": argument not expected for option '-" + << op << "' in '" << arg << "'" << endl; + return false; + } + + cl.options.push_back (CommandLine::Option (op)); + } + + continue; + } + } + } + + cl.arguments.push_back (arg); + } + + return true; +} |