//===-- CommandLine.cpp - Command line parser implementation --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This class implements a command line argument processor that is useful when
// creating a tool. It provides a simple, minimalistic interface that is easily
// extensible and supports nonlocal (library) command line options.
//
// Note that rather than trying to figure out what this code does, you could try
// reading the library documentation located in docs/CommandLine.html
//
//===----------------------------------------------------------------------===//
#include "llvm/Config/config.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/System/Path.h"
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <iostream>
#include <cstdlib>
#include <cerrno>
#include <cstring>
using namespace llvm;
using namespace cl;
//===----------------------------------------------------------------------===//
// Template instantiations and anchors.
//
TEMPLATE_INSTANTIATION(class basic_parser<bool>);
TEMPLATE_INSTANTIATION(class basic_parser<int>);
TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
TEMPLATE_INSTANTIATION(class basic_parser<double>);
TEMPLATE_INSTANTIATION(class basic_parser<float>);
TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
TEMPLATE_INSTANTIATION(class opt<unsigned>);
TEMPLATE_INSTANTIATION(class opt<int>);
TEMPLATE_INSTANTIATION(class opt<std::string>);
TEMPLATE_INSTANTIATION(class opt<bool>);
void Option::anchor() {}
void basic_parser_impl::anchor() {}
void parser<bool>::anchor() {}
void parser<int>::anchor() {}
void parser<unsigned>::anchor() {}
void parser<double>::anchor() {}
void parser<float>::anchor() {}
void parser<std::string>::anchor() {}
//===----------------------------------------------------------------------===//
// Globals for name and overview of program
static std::string ProgramName = "<premain>";
static const char *ProgramOverview = 0;
// This collects additional help to be printed.
static ManagedStatic<std::vector<const char*> > MoreHelp;
extrahelp::extrahelp(const char *Help)
: morehelp(Help) {
MoreHelp->push_back(Help);
}
//===----------------------------------------------------------------------===//
// Basic, shared command line option processing machinery.
//
static ManagedStatic<std::map<std::string, Option*> > Options;
static ManagedStatic<std::vector<Option*> > PositionalOptions;
static Option *getOption(const std::string &Str) {
std::map<std::string,Option*>::iterator I = Options->find(Str);
return I != Options->end() ? I->second : 0;
}
static void AddArgument(const char *ArgName, Option *Opt) {
if (getOption(ArgName)) {
std::cerr << ProgramName << ": CommandLine Error: Argument '"
<< ArgName << "' defined more than once!\n";
} else {
// Add argument to the argument map!
(*Options)[ArgName] = Opt;
}
}
// RemoveArgument - It's possible that the argument is no longer in the map if
// options have already been processed and the map has been deleted!
//
static void RemoveArgument(const char *ArgName, Option *Opt) {
if (Options->empty()) return;
#ifndef NDEBUG
// This disgusting HACK is brought to you courtesy of GCC 3.3.2, which ICE's
// If we pass ArgName directly into getOption here.
std::string Tmp = ArgName;
assert(getOption(Tmp) == Opt && "Arg not in map!");
#endif
Options->erase(ArgName);
}
static inline bool ProvideOption(Option *Handler, const char *ArgName,
const char *Value, int argc, char **argv,
int &i) {
// Enforce value requirements
switch (Handler->getValueExpectedFlag()) {
case ValueRequired:
if (Value == 0) { // No value specified?
if (i+1 < argc) { // Steal the next argument, like for '-o filename'
Value = argv[++i];
} else {
return Handler->error(" requires a value!");
}
}
break