Parse custom options using CommonOptionsParser

Hello list,

I am currently working on a libTooling project and need to read custom
command line options for this.

Unfortunately I was not able to get the following sample code from
CommonOptionsParser [1] to run. Can you please give me an advice what
might be the problem here?

To make the sample code compiling I had to add the import of Tooling.h
and modify the Tool.run() call to retrieve the Front-end action via
.get() from the factory first. Also the category needs to be applied
on the new option, otherwise it's not shown in the help.

#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"

using namespace clang::tooling;
using namespace llvm;

static cl::OptionCategory MyToolCategory("My tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
static cl::opt<bool> YourOwnOption("abc", cl::cat(MyToolCategory));

int main(int argc, const char **argv) {
  CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  ClangTool Tool(OptionsParser.getCompilations(),
  OptionsParser.getSourcePathList());
  llvm::outs() << YourOwnOption.getValue();
  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}

Thank you very much in advance!

Best regards
Christoph

[1]
http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1CommonOptionsParser.html

Hi

Your code seems to work fine, what is your clang version?

Can you elaborate the problem ?

Also the category needs to be applied
on the new option, otherwise it’s not shown in the help.

That seems to be by design.

Examples of invocation:
$ ./bin/testtool ~/b.cpp -abc=1 – -std=c++11
1

$ ./bin/testtool --help

My tool options:

-abc -

Oh, thank you for your fast response. I actually did not came up with the idea
to put my option before the --.
This works, thank you!

The options after the – get passed to the clang driver.

Those generally come from the compilcation database when the tool is used.

Those generally come from the compilcation database when the tool is used.

Typo correction: 'compilation'