protoc生成命令的详细解释

经常会用到grpc,但是对protoc生成指令一直没有深入了解过,今天借着这个机会,来仔细了解一下。

首先,我们在安装好protoc工具后执行以下指令,会获得对应的帮助文档。我们先来翻译一下这个文档。

protoc --help
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  --descriptor_set_in=FILES   Specifies a delimited list of FILES
                              each containing a FileDescriptorSet (a
                              protocol buffer defined in descriptor.proto).
                              The FileDescriptor for each of the PROTO_FILES
                              provided will be loaded from these
                              FileDescriptorSets. If a FileDescriptor
                              appears multiple times, the first occurrence
                              will be used.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent 
                              message. Extension ranges are counted as 
                              occupied fields numbers.

  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.
  @                 Read options and filenames from file. If a
                              relative file path is specified, the file
                              will be searched in the working directory.
                              The --proto_path option will not affect how
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @ as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.
使用方法: protoc [OPTION] PROTO_FILES
解析PROTO_FILES并根据以下给定的选项生成输出:
  -IPATH, --proto_path=PATH   指定要在其中搜索导入的目录。可以多次指定;
                              目录将按顺序搜索。如果未给出,则使用当前工作目录。
  --version                   显示版本信息并退出。
  -h, --help                  显示此文本并退出。
  --encode=MESSAGE_TYPE       从标准输入读取给定类型的文本格式消息,并将其以二进制形式写入标准输出。 
                              消息类型必须在PROTO_FILES或它们的导入中定义。
  --decode=MESSAGE_TYPE       从标准输入读取给定类型的二进制消息,并将其以文本格式写入标准输出。 
                              消息类型必须在PROTO_FILES或它们的导入中定义。
  --decode_raw                从标准输入读取任意协议消息,并将文本格式的原始标记/值对写入标准输出。 
                              使用此标志时,不应提供PROTO_FILES。
  --descriptor_set_in=FILES   指定分隔的文件列表,每个文件包含一个FileDescriptorSet(在descriptor.proto中定义的协议缓冲                                                                                          
                              区)。 提供的每个PROTO_FILES的FileDescriptor将从这些FileDescriptorSets中加载。 
                              如果FileDescriptor出现多次,则将使用第一个出现的文件。
  -oFILE,                     将包含所有输入文件的FileDescriptorSet(协议缓冲区,-descriptor_set_out = FILE在                                                            
                              descriptor.proto中定义)写入到FILE。
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent 
                              message. Extension ranges are counted as 
                              occupied fields numbers.

  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.
  @                 Read options and filenames from file. If a
                              relative file path is specified, the file
                              will be searched in the working directory.
                              The --proto_path option will not affect how
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @ as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.

然后我在写DataGen代码时有如下目录结构

👇foundation
    👇proto
        👇common
            common.proto
        👇executor
            executor.proto
        👇pipeline
            pipeline.proto
        👇resolver
            resolver.proto

而我在执行命令时的当前目录在

~/go/src/github.com/cyclegen/datagen/foundation/proto

我执行了如下命令:

protoc --proto_path=. --proto_path=./executor --go_out=plugins=grpc:./executor executor/executor.proto

生成的目录结构如下:

👇foundation
    👇proto
        👇common
            common.proto
        👇executor
            executor.proto
            👇executor
                ➕executor.pb.go
        👇pipeline
            pipeline.proto
        👇resolver
            resolver.proto

可以看到在executor文件夹下又创建了一个executor的文件夹并在内部生成了pb.go文件。
下面我们修改一下命令:

protoc --proto_path=. --proto_path=./executor --go_out=plugins=grpc:. executor/executor.proto

生成的目录结构如下:

👇foundation
    👇proto
        👇common
            common.proto
        👇executor
            executor.proto
            ➕executor.pb.go
        👇pipeline
            pipeline.proto
        👇resolver
            resolver.proto

可以看到,生成的pb.go直接在executor.proto同级的目录下生成。
再次修改命令:

protoc --proto_path=. --proto_path=./executor --go_out=plugins=grpc:.. executor/executor.proto

生成的目录结构如下:

👇foundation
    👇executor
        ➕executor.pb.go
    👇proto
        👇common
            common.proto
        👇executor
            executor.proto
            ➕executor.pb.go
        👇pipeline
            pipeline.proto
        👇resolver
            resolver.proto

很神奇,生成的pb.go位于和proto/即当前目录的上层目录下新创建的executor文件夹中,这里我们的–go_out=..似乎是在指当前文件夹的上层文件夹。
我们修改一下当前文件夹,回到foundation/目录下,执行以下命令:

protoc --proto_path=./proto --proto_path=./proto/executor --go_out=plugins=grpc:.. proto/executor/executor.proto

生成的目录结构如下:

👇executor
    ➕executor.pb.go
👇foundation
    👇proto
        👇common
            common.proto
        👇executor
            executor.proto
            ➕executor.pb.go
        👇pipeline
            pipeline.proto
        👇resolver
            resolver.proto

果然,与我们料想的一样,–go_out=..参数是相对于当前目录而言的,而我们使用绝对路径时,该指令也是可以使用的,会创建对应的文件夹及内部的pb.go文件。
然后在测试中发现,PROTO_FILES的形态会影响是否递归创建目录,例如上面

protoc --proto_path=./proto --proto_path=./proto/executor --go_out=plugins=grpc:.. proto/executor/executor.proto

会导致创建proto/executor/executor.proto这样的文件路径结构,而当我们进入common.proto所在目录使用如下指令时则不会创建目录,只创建对应的pb.go文件

protoc --go_out=plugins=grpc:../.. common.proto

本文系作者 @ 原创发布在 CycleGen。未经许可,禁止转载。

喜欢()
评论 (0)
    热门搜索
    173 文章
    1 评论
    49 喜欢
    Top