My favorites | English | Sign in

More personalization in Google Friend Connect New!

Protocol Buffers

command_line_interface.h

#include <google/protobuf/compiler/command_line_interface.h>
namespace google::protobuf::compiler

Implements the Protocol Compiler front-end such that it may be reused by custom compilers written to support other languages.

Classes in this file

This class implements the command-line interface to the protocol compiler.

class CommandLineInterface

#include <google/protobuf/compiler/command_line_interface.h>
namespace google::protobuf::compiler

This class implements the command-line interface to the protocol compiler.

It is designed to make it very easy to create a custom protocol compiler supporting the languages of your choice. For example, if you wanted to create a custom protocol compiler binary which includes both the regular C++ support plus support for your own custom output "Foo", you would write a class "FooGenerator" which implements the CodeGenerator interface, then write a main() procedure like this:

int main(int argc, char* argv[]) {
  google::protobuf::compiler::CommandLineInterface cli;

 Support generation of C++ source and headers.
  google::protobuf::compiler::cpp::CppGenerator cpp_generator;
  cli.RegisterGenerator("--cpp_out", &cpp_generator,
    "Generate C++ source and header.");

 Support generation of Foo code.
  FooGenerator foo_generator;
  cli.RegisterGenerator("--foo_out", &foo_generator,
    "Generate Foo file.");

  return cli.Run(argc, argv);
}

The compiler is invoked with syntax like:

protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto

For a full description of the command-line syntax, invoke it with --help.

Members

CommandLineInterface()
~CommandLineInterface()
void
RegisterGenerator(const string & flag_name, CodeGenerator * generator, const string & help_text)
Register a code generator for a language. more...
int
Run(int argc, const char *const argv)
Run the Protocol Compiler with the given command-line parameters. more...
void
SetInputsAreProtoPathRelative(bool enable)
Call SetInputsAreCwdRelative(true) if the input files given on the command line should be interpreted relative to the proto import path specified using --proto_path or -I flags. more...
void
SetVersionInfo(const string & text)
Provides some text which will be printed when the --version flag is used. more...

void CommandLineInterface::RegisterGenerator(
        const string & flag_name,
        CodeGenerator * generator,
        const string & help_text)

Register a code generator for a language.

Parameters:

  • flag_name: The command-line flag used to specify an output file of this type. The name must start with a '-'. If the name is longer than one letter, it must start with two '-'s.
  • generator: The CodeGenerator which will be called to generate files of this type.
  • help_text: Text describing this flag in the --help output.

Some generators accept extra parameters. You can specify this parameter on the command-line by placing it before the output directory, separated by a colon:

protoc --foo_out=enable_bar:outdir

The text before the colon is passed to CodeGenerator::Generate() as the "parameter".


int CommandLineInterface::Run(
        int argc,
        const char *const argv)

Run the Protocol Compiler with the given command-line parameters.

Returns the error code which should be returned by main().

It may not be safe to call Run() in a multi-threaded environment because it calls strerror(). I'm not sure why you'd want to do this anyway.


void CommandLineInterface::SetInputsAreProtoPathRelative(
        bool enable)

Call SetInputsAreCwdRelative(true) if the input files given on the command line should be interpreted relative to the proto import path specified using --proto_path or -I flags.

Otherwise, input file names will be interpreted relative to the current working directory (or as absolute paths if they start with '/'), though they must still reside inside a directory given by --proto_path or the compiler will fail. The latter mode is generally more intuitive and easier to use, especially e.g. when defining implicit rules in Makefiles.


void CommandLineInterface::SetVersionInfo(
        const string & text)

Provides some text which will be printed when the --version flag is used.

The version of libprotoc will also be printed on the next line after this text.