Use optparse in nanopb_generator.py
This commit is contained in:
@@ -22,6 +22,16 @@ except:
|
|||||||
print
|
print
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Generation of single fields
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
# Values are tuple (c type, pb ltype)
|
# Values are tuple (c type, pb ltype)
|
||||||
@@ -264,6 +274,15 @@ class Field:
|
|||||||
return max(self.tag, self.max_size, self.max_count)
|
return max(self.tag, self.max_size, self.max_count)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Generation of messages (structures)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
class Message:
|
class Message:
|
||||||
def __init__(self, names, desc):
|
def __init__(self, names, desc):
|
||||||
self.name = names
|
self.name = names
|
||||||
@@ -313,6 +332,16 @@ class Message:
|
|||||||
result += ' PB_LAST_FIELD\n};'
|
result += ' PB_LAST_FIELD\n};'
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Processing of entire .proto files
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def iterate_messages(desc, names = Names()):
|
def iterate_messages(desc, names = Names()):
|
||||||
'''Recursively find all messages. For each, yield name, DescriptorProto.'''
|
'''Recursively find all messages. For each, yield name, DescriptorProto.'''
|
||||||
if hasattr(desc, 'message_type'):
|
if hasattr(desc, 'message_type'):
|
||||||
@@ -475,26 +504,43 @@ def generate_source(headername, enums, messages):
|
|||||||
for msg in messages:
|
for msg in messages:
|
||||||
yield msg.fields_definition() + '\n\n'
|
yield msg.fields_definition() + '\n\n'
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Command line interface
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
|
from optparse import OptionParser
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
optparser = OptionParser(
|
||||||
print "Usage: " + sys.argv[0] + " file.pb"
|
usage = "Usage: nanopb_generator.py [options] file.pb ...",
|
||||||
print "where file.pb has been compiled from .proto by:"
|
epilog = "Compile file.pb from file.proto by: 'protoc -ofile.pb file.proto'. " +
|
||||||
print "protoc -ofile.pb file.proto"
|
"Output will be written to file.pb.h and file.pb.c.")
|
||||||
print "Output fill be written to file.pb.h and file.pb.c"
|
optparser.add_option("-x", dest="exclude", metavar="FILE", action="append", default=[],
|
||||||
sys.exit(1)
|
help="Exclude file from generated #include list.")
|
||||||
|
optparser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False,
|
||||||
|
help="Don't print anything except errors.")
|
||||||
|
|
||||||
data = open(sys.argv[1], 'rb').read()
|
def process(filenames, options):
|
||||||
|
'''Process the files given on the command line.'''
|
||||||
|
|
||||||
|
if not filenames:
|
||||||
|
optparser.print_help()
|
||||||
|
return False
|
||||||
|
|
||||||
|
for filename in filenames:
|
||||||
|
data = open(filename, 'rb').read()
|
||||||
fdesc = descriptor.FileDescriptorSet.FromString(data)
|
fdesc = descriptor.FileDescriptorSet.FromString(data)
|
||||||
enums, messages = parse_file(fdesc.file[0])
|
enums, messages = parse_file(fdesc.file[0])
|
||||||
|
|
||||||
noext = os.path.splitext(sys.argv[1])[0]
|
noext = os.path.splitext(filename)[0]
|
||||||
headername = noext + '.pb.h'
|
headername = noext + '.pb.h'
|
||||||
sourcename = noext + '.pb.c'
|
sourcename = noext + '.pb.c'
|
||||||
headerbasename = os.path.basename(headername)
|
headerbasename = os.path.basename(headername)
|
||||||
|
|
||||||
|
if not options.quiet:
|
||||||
print "Writing to " + headername + " and " + sourcename
|
print "Writing to " + headername + " and " + sourcename
|
||||||
|
|
||||||
# List of .proto files that should not be included in the C header file
|
# List of .proto files that should not be included in the C header file
|
||||||
@@ -510,4 +556,12 @@ if __name__ == '__main__':
|
|||||||
for part in generate_source(headerbasename, enums, messages):
|
for part in generate_source(headerbasename, enums, messages):
|
||||||
source.write(part)
|
source.write(part)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
options, filenames = optparser.parse_args()
|
||||||
|
status = process(filenames, options)
|
||||||
|
|
||||||
|
if not status:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user