Mercurial > hg > martINI
comparison martini/main.py @ 2:d172c078f4a2
add -O option for specifying output file
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 08 Dec 2009 16:23:50 -0500 |
parents | 5627074cd79b |
children | f7f335561ceb |
comparison
equal
deleted
inserted
replaced
1:5627074cd79b | 2:d172c078f4a2 |
---|---|
3 usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3" | 3 usage = "%s file1 [file2] [...] --section1 option1=value1 option2=value2 --section2 option3=value3" |
4 | 4 |
5 import config | 5 import config |
6 import os | 6 import os |
7 import sys | 7 import sys |
8 from optparse import OptionParser | |
8 | 9 |
9 sep = '--' # section separator | 10 sep = '--' # section separator |
10 | 11 |
11 def parse_args(*args): | 12 def parse_args(*args): |
12 """ | 13 """ |
147 def munge(args=None): | 148 def munge(args=None): |
148 | 149 |
149 # process arguments | 150 # process arguments |
150 if args is None: | 151 if args is None: |
151 args = sys.argv[1:] | 152 args = sys.argv[1:] |
153 | |
154 parser = OptionParser() | |
155 parser.add_option('-O', dest='output', | |
156 help="name of file to output (stdout if not specified)") | |
157 options, args = parser.parse_args(args) | |
152 files, sections = parse_options(*args) | 158 files, sections = parse_options(*args) |
153 | 159 |
154 # display usage information | 160 # display usage information |
155 if not files: | 161 if not files: |
156 print 'Usage:' | 162 print 'Usage:' |
159 | 165 |
160 # munge the files | 166 # munge the files |
161 conf = list(files) | 167 conf = list(files) |
162 conf.append(sections) | 168 conf.append(sections) |
163 munger = config.ConfigMunger(*conf) | 169 munger = config.ConfigMunger(*conf) |
164 munger.write(sys.stdout) | 170 if options.output: |
171 fp = file(options.output, 'w') | |
172 else: | |
173 fp = sys.stdout | |
174 munger.write(fp) | |
165 | 175 |
166 if __name__ == '__main__': | 176 if __name__ == '__main__': |
167 set(sys.argv[1:]) | 177 set(sys.argv[1:]) |