Created
July 7, 2019 15:19
-
-
Save liangyy/67f3562135509674fad862fb405b58bb to your computer and use it in GitHub Desktop.
String formatting from template and nargs in python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
parser = argparse.ArgumentParser(description='generate script from template') | |
parser.add_argument('word', metavar='KEY=VALUE', type=str, nargs='+', | |
help='key-value pairs') | |
parser.add_argument('--template', metavar='input filename', type=str, | |
help='template file') | |
parser.add_argument('--output', metavar='output filename', type=str, | |
help='output') | |
args = parser.parse_args() | |
mydic = {} | |
for i in args.word: | |
i = i.split('=') | |
mydic[i[0]] = i[1] | |
with open(args.template, 'r') as f: | |
text = f.read() | |
text = text.format(**mydic) | |
o = open(args.output, 'w') | |
o.write(text + '\n') | |
o.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment