You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.7 KiB

10 years ago
  1. #!/usr/local/bin/python2.7
  2. # encoding: utf-8
  3. '''
  4. TODO: Fix this. Currently breaks parser
  5. bin.example_argparse_souce -- shortdesc
  6. bin.example_argparse_souce is a description
  7. It defines classes_and_methods
  8. @author: user_name
  9. @copyright: 2013 organization_name. All rights reserved.
  10. @license: license
  11. @contact: user_email
  12. @deffield updated: Updated
  13. '''
  14. import sys
  15. import os
  16. from argparse import ArgumentParser
  17. from argparse import RawDescriptionHelpFormatter
  18. from gooey.python_bindings.gooey_decorator import Gooey
  19. __all__ = []
  20. __version__ = 0.1
  21. __date__ = '2013-12-13'
  22. __updated__ = '2013-12-13'
  23. DEBUG = 0
  24. TESTRUN = 0
  25. PROFILE = 0
  26. class CLIError(Exception):
  27. '''Generic exception to raise and log different fatal errors.'''
  28. def __init__(self, msg):
  29. super(CLIError).__init__(type(self))
  30. self.msg = "E: %s" % msg
  31. def __str__(self):
  32. return self.msg
  33. def __unicode__(self):
  34. return self.msg
  35. def main(argv=None): # IGNORE:C0111
  36. '''Command line options.'''
  37. raise NotImplementedError("This one isn't implemented yet. Please run any of the other examples. :) Sorry!")
  38. if argv is None:
  39. argv = sys.argv
  40. else:
  41. sys.argv.extend(argv)
  42. program_name = os.path.basename(sys.argv[0])
  43. program_version = "v%s" % __version__
  44. program_build_date = str(__updated__)
  45. program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date)
  46. program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
  47. program_license = '''%s
  48. Created by user_name on %s.
  49. Copyright 2013 organization_name. All rights reserved.
  50. Licensed under the Apache License 2.0
  51. http://www.apache.org/licenses/LICENSE-2.0
  52. Distributed on an "AS IS" basis without warranties
  53. or conditions of any kind, either express or implied.
  54. USAGE
  55. ''' % (program_shortdesc, str(__date__))
  56. try:
  57. # Setup argument parser
  58. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  59. parser.add_argument("filename", help="filename")
  60. parser.add_argument("-r", "--recursive", dest="recurse", action="store_true",
  61. help="recurse into subfolders [default: %(default)s]")
  62. parser.add_argument("-v", "--verbose", dest="verbose", action="count",
  63. help="set verbosity level [default: %(default)s]")
  64. parser.add_argument("-i", "--include", action="append", nargs='+',
  65. help="only include paths matching this regex pattern. Note: exclude is given preference over include. ",
  66. metavar="RE")
  67. parser.add_argument("-m", "--mycoolargument", help="mycoolargument")
  68. parser.add_argument("-e", "--exclude", dest="exclude",
  69. help="exclude paths matching this regex pattern. [default: %(default)s]", metavar="RE")
  70. parser.add_argument('-V', '--version', action='version')
  71. parser.add_argument('-T', '--tester', choices=['yes', 'no'])
  72. parser.add_argument(dest="paths", help="paths to folder(s) with source file(s) [default: %(default)s]",
  73. metavar="path", nargs='+')
  74. # for i in parser._actions:
  75. # print i
  76. # Process arguments
  77. args = parser.parse_args()
  78. paths = args.paths
  79. verbose = args.verbose
  80. recurse = args.recurse
  81. inpat = args.include
  82. expat = args.exclude
  83. if verbose > 0:
  84. print("Verbose mode on")
  85. if recurse:
  86. print("Recursive mode on")
  87. else:
  88. print("Recursive mode off")
  89. if inpat and expat and inpat == expat:
  90. raise CLIError("include and exclude pattern are equal! Nothing will be processed.")
  91. for inpath in paths:
  92. ### do something with inpath ###
  93. print(inpath)
  94. return 0
  95. except KeyboardInterrupt:
  96. ### handle keyboard interrupt ###
  97. return 0
  98. except Exception, e:
  99. if DEBUG or TESTRUN:
  100. raise (e)
  101. indent = len(program_name) * " "
  102. sys.stderr.write(program_name + ": " + repr(e) + "\n")
  103. sys.stderr.write(indent + " for help use --help")
  104. return 2
  105. if __name__ == "__main__":
  106. if DEBUG:
  107. sys.argv.append("-h")
  108. # sys.argv.append("-v")
  109. sys.argv.append("-r")
  110. main()
  111. sys.exit()
  112. if TESTRUN:
  113. import doctest
  114. doctest.testmod()
  115. if PROFILE:
  116. import cProfile
  117. import pstats
  118. profile_filename = 'bin.example_argparse_souce_profile.txt'
  119. cProfile.run('main()', profile_filename)
  120. statsfile = open("profile_stats.txt", "wb")
  121. p = pstats.Stats(profile_filename, stream=statsfile)
  122. stats = p.strip_dirs().sort_stats('cumulative')
  123. stats.print_stats()
  124. statsfile.close()
  125. sys.exit(0)
  126. sys.exit(main())