mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-06 07:03:52 +00:00
Add support for making custom versions of CLI11 single header
This commit is contained in:
parent
e7dc4c23f7
commit
b9492305c0
@ -54,6 +54,8 @@ class HeaderFile(object):
|
|||||||
|
|
||||||
self.body = '\n// From {}:\n\n'.format(inc) + inner[inner.find('namespace'):]
|
self.body = '\n// From {}:\n\n'.format(inc) + inner[inner.find('namespace'):]
|
||||||
|
|
||||||
|
self.namespace = None
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
out = copy(self)
|
out = copy(self)
|
||||||
out.headers |= other.headers
|
out.headers |= other.headers
|
||||||
@ -69,8 +71,16 @@ class HeaderFile(object):
|
|||||||
def verbatim_str(self):
|
def verbatim_str(self):
|
||||||
return '\n'.join(self.verbatim)
|
return '\n'.join(self.verbatim)
|
||||||
|
|
||||||
|
def insert_namespace(self, namespace):
|
||||||
|
self.namespace = namespace
|
||||||
|
|
||||||
|
def macro_replacement(self, before, after):
|
||||||
|
self.verbatim = [x.replace(before, after) for x in self.verbatim]
|
||||||
|
self.body = self.body.replace(before, after)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '''\
|
result = '''\
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// CLI11: Version {self.VERSION}
|
// CLI11: Version {self.VERSION}
|
||||||
@ -87,12 +97,18 @@ class HeaderFile(object):
|
|||||||
// Standard combined includes:
|
// Standard combined includes:
|
||||||
|
|
||||||
{self.header_str}
|
{self.header_str}
|
||||||
{self.verbatim_str}
|
|
||||||
{self.body}
|
|
||||||
'''.format(self=self)
|
'''.format(self=self)
|
||||||
|
|
||||||
|
if self.namespace:
|
||||||
|
result += '\nnamespace ' + self.namespace + ' {\n\n'
|
||||||
|
result += '{self.verbatim_str}\n{self.body}\n'.format(self=self)
|
||||||
|
if self.namespace:
|
||||||
|
result += '} // namespace ' + self.namespace + '\n\n'
|
||||||
|
|
||||||
def MakeHeader(output, main_header, include_dir = '../include'):
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def MakeHeader(output, main_header, include_dir = '../include', namespace=None, macro=None):
|
||||||
# Set tag if possible to class variable
|
# Set tag if possible to class variable
|
||||||
try:
|
try:
|
||||||
HeaderFile.TAG = check_output(['git', 'describe', '--tags', '--always'], cwd=str(DIR)).decode("utf-8").strip()
|
HeaderFile.TAG = check_output(['git', 'describe', '--tags', '--always'], cwd=str(DIR)).decode("utf-8").strip()
|
||||||
@ -114,18 +130,29 @@ def MakeHeader(output, main_header, include_dir = '../include'):
|
|||||||
headers = [HeaderFile(base_dir, inc) for inc in include_files]
|
headers = [HeaderFile(base_dir, inc) for inc in include_files]
|
||||||
single_header = reduce(operator.add, headers)
|
single_header = reduce(operator.add, headers)
|
||||||
|
|
||||||
|
if macro:
|
||||||
|
before, after = macro
|
||||||
|
print("Converting macros", before, "->", after)
|
||||||
|
single_header.macro_replacement(before, after)
|
||||||
|
|
||||||
|
if namespace:
|
||||||
|
print("Adding namespace", namespace)
|
||||||
|
single_header.insert_namespace(namespace)
|
||||||
|
|
||||||
with open(output, 'w') as f:
|
with open(output, 'w') as f:
|
||||||
f.write(str(single_header))
|
f.write(str(single_header))
|
||||||
|
|
||||||
print("Created", output)
|
print("Created", output)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser(usage='Convert source to single header include. Can optionally add namespace and search-replace replacements (for macros).')
|
||||||
parser.add_argument("output", help="Single header file output")
|
parser.add_argument("output", help="Single header file output")
|
||||||
parser.add_argument("--main", default='CLI/CLI.hpp', help="The main include file that defines the other files")
|
parser.add_argument("--main", default='CLI/CLI.hpp', help="The main include file that defines the other files")
|
||||||
parser.add_argument("--include", default='../include')
|
parser.add_argument("--include", default='../include', help="The include directory")
|
||||||
|
parser.add_argument("--namespace", help="Add an optional namespace")
|
||||||
|
parser.add_argument("--macro", nargs=2, help="Macro replacement: CLI11_ NEW_PREFIX_")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
MakeHeader(args.output, args.main, args.include)
|
|
||||||
|
MakeHeader(args.output, args.main, args.include, args.namespace, args.macro)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user