diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2022-11-25 12:38:07 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2022-11-25 15:06:37 +0300 |
commit | d8b6da1cf7028aad6d584dec5afe15e2fc2d23c5 (patch) | |
tree | 683ea69f0e089e0a9e26c5931192a6a65f4b4894 | |
parent | 176da025af39e788877446183b530701a54f5057 (diff) |
Allow specifying include format without special characters (#810)
On various platforms, passing a format string containing # and ""
is difficult for the --library-include-format generator argument.
This commit allows specifying the most common styles with the names
"bracket" and "quote".
Also unified the option parsing between protoc plugin and direct invocation modes.
-rw-r--r-- | BUILD.bazel | 2 | ||||
-rwxr-xr-x | generator/nanopb_generator.py | 78 |
2 files changed, 41 insertions, 39 deletions
diff --git a/BUILD.bazel b/BUILD.bazel index 5ab3df5..50b616c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -49,7 +49,7 @@ py_binary( proto_plugin( name = "nanopb_plugin", options = [ - "--library-include-format='#include\"%s\"'", + '--library-include-format=quote', ], outputs = [ "{protopath}.pb.h", diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 1fc9f73..2ebf9b9 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -2292,10 +2292,10 @@ optparser.add_option("-D", "--output-dir", dest="output_dir", help="Output directory of .pb.h and .pb.c files") optparser.add_option("-Q", "--generated-include-format", dest="genformat", metavar="FORMAT", default='#include "%s"', - help="Set format string to use for including other .pb.h files. [default: %default]") + help="Set format string to use for including other .pb.h files. Value can be 'quote', 'bracket' or a format string. [default: %default]") optparser.add_option("-L", "--library-include-format", dest="libformat", metavar="FORMAT", default='#include <%s>', - help="Set format string to use for including the nanopb pb.h header. [default: %default]") + help="Set format string to use for including the nanopb pb.h header. Value can be 'quote', 'bracket' or a format string. [default: %default]") optparser.add_option("--strip-path", dest="strip_path", action="store_true", default=False, help="Strip directory path from #included .pb.h file name") optparser.add_option("--no-strip-path", dest="strip_path", action="store_false", @@ -2317,6 +2317,42 @@ optparser.add_option("--protoc-insertion-points", dest="protoc_insertion_points" optparser.add_option("-C", "--c-style", dest="c_style", action="store_true", default=False, help="Use C naming convention.") +def process_cmdline(args, is_plugin): + '''Process command line options. Returns list of options, filenames.''' + + options, filenames = optparser.parse_args(args) + + if options.version: + if is_plugin: + sys.stderr.write('%s\n' % (nanopb_version)) + else: + print(nanopb_version) + sys.exit(0) + + if not filenames and not is_plugin: + optparser.print_help() + sys.exit(1) + + if options.quiet: + options.verbose = False + + include_formats = {'quote': '#include "%s"', 'bracket': '#include <%s>'} + options.libformat = include_formats.get(options.libformat, options.libformat) + options.genformat = include_formats.get(options.genformat, options.genformat) + + if options.c_style: + Globals.naming_style = NamingStyleC() + + Globals.verbose_options = options.verbose + + if options.verbose: + sys.stderr.write("Nanopb version %s\n" % nanopb_version) + sys.stderr.write('Google Python protobuf library imported from %s, version %s\n' + % (google.protobuf.__file__, google.protobuf.__version__)) + + return options, filenames + + def parse_file(filename, fdesc, options): '''Parse a single file. Returns a ProtoFile instance.''' toplevel_options = nanopb_pb2.NanoPBOptions() @@ -2424,32 +2460,13 @@ def process_file(filename, fdesc, options, other_files = {}): def main_cli(): '''Main function when invoked directly from the command line.''' - options, filenames = optparser.parse_args() - - if options.version: - print(nanopb_version) - sys.exit(0) - - if not filenames: - optparser.print_help() - sys.exit(1) - - if options.quiet: - options.verbose = False + options, filenames = process_cmdline(sys.argv[1:], is_plugin = False) if options.output_dir and not os.path.exists(options.output_dir): optparser.print_help() sys.stderr.write("\noutput_dir does not exist: %s\n" % options.output_dir) sys.exit(1) - if options.verbose: - sys.stderr.write("Nanopb version %s\n" % nanopb_version) - sys.stderr.write('Google Python protobuf library imported from %s, version %s\n' - % (google.protobuf.__file__, google.protobuf.__version__)) - - if options.c_style: - Globals.naming_style = NamingStyleC() - # Load .pb files into memory and compile any .proto files. include_path = ['-I%s' % p for p in options.options_path] all_fdescs = {} @@ -2479,7 +2496,6 @@ def main_cli(): other_files[fdesc.name] = parse_file(fdesc.name, fdesc, options) # Then generate the headers / sources - Globals.verbose_options = options.verbose for fdesc in out_fdescs.values(): results = process_file(fdesc.name, fdesc, options, other_files) @@ -2543,21 +2559,7 @@ def main_plugin(): optparser.print_help(sys.stderr) sys.exit(1) - options, dummy = optparser.parse_args(args) - - if options.version: - sys.stderr.write('%s\n' % (nanopb_version)) - sys.exit(0) - - Globals.verbose_options = options.verbose - - if options.c_style: - Globals.naming_style = NamingStyleC() - - if options.verbose: - sys.stderr.write("Nanopb version %s\n" % nanopb_version) - sys.stderr.write('Google Python protobuf library imported from %s, version %s\n' - % (google.protobuf.__file__, google.protobuf.__version__)) + options, dummy = process_cmdline(args, is_plugin = True) response = plugin_pb2.CodeGeneratorResponse() |