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.

145 lines
4.1 KiB

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