Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI11 Branding #150

Merged
merged 1 commit into from
Jul 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions scripts/MakeSingleHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, base, inc):

self.body = '\n// From {}:\n\n'.format(inc) + inner[inner.find('namespace'):]

self.namespace = None

def __add__(self, other):
out = copy(self)
out.headers |= other.headers
Expand All @@ -69,8 +71,16 @@ def header_str(self):
def verbatim_str(self):
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):
return '''\
result = '''\
#pragma once

// CLI11: Version {self.VERSION}
Expand All @@ -87,12 +97,18 @@ def __str__(self):
// Standard combined includes:

{self.header_str}
{self.verbatim_str}
{self.body}
'''.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'

return result

def MakeHeader(output, main_header, include_dir = '../include'):

def MakeHeader(output, main_header, include_dir = '../include', namespace=None, macro=None):
# Set tag if possible to class variable
try:
HeaderFile.TAG = check_output(['git', 'describe', '--tags', '--always'], cwd=str(DIR)).decode("utf-8").strip()
Expand All @@ -114,18 +130,29 @@ def MakeHeader(output, main_header, include_dir = '../include'):
headers = [HeaderFile(base_dir, inc) for inc in include_files]
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:
f.write(str(single_header))

print("Created", output)


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("--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()

MakeHeader(args.output, args.main, args.include)

MakeHeader(args.output, args.main, args.include, args.namespace, args.macro)