comparison makeitso/python_package/{{package}}/{{main}}.py @ 188:1bcf60bcc455

update to argparse
author Jeff Hammel <k0scist@gmail.com>
date Fri, 11 Apr 2014 18:50:40 -0700
parents 74f41d53b057
children 3cb66c9e5ce8
comparison
equal deleted inserted replaced
187:74f41d53b057 188:1bcf60bcc455
4 """ 4 """
5 {{description}} 5 {{description}}
6 """ 6 """
7 7
8 # imports 8 # imports
9 import optparse 9 import argparse
10 import os 10 import os
11 import subprocess 11 import subprocess
12 import sys 12 import sys
13 13
14 __all__ = ['main'] 14 __all__ = ['main']
15 here = os.path.dirname(os.path.realpath(__file__)) 15 here = os.path.dirname(os.path.realpath(__file__))
16 string = (str, unicode)
16 17
17 def add_options(parser): 18 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
18 """add options to the OptionParser instance""" 19 """description formatter for console script entry point"""
20 def format_description(self, description):
21 if description:
22 return description.strip() + '\n'
23 else:
24 return ''
25
26
27 class Parser(argparse.ArgumentParser):
28 """CLI option parser"""
29 def __init__(self):
30 argparse.ArgumentParser.__init__(self, description=__doc__)
31
19 32
20 def main(args=sys.argv[1:]): 33 def main(args=sys.argv[1:]):
34 """CLI"""
21 35
22 # parse command line options 36 # parse command line options
23 usage = '%prog [options] ...' 37 parser = Parser()
24 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): 38 options = parser.parse_args(args)
25 """description formatter for console script entry point"""
26 def format_description(self, description):
27 if description:
28 return description.strip() + '\n'
29 else:
30 return ''
31 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
32 options, args = parser.parse_args(args)
33 39
34 if __name__ == '__main__': 40 if __name__ == '__main__':
35 main() 41 main()