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.

146 lines
4.2 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.gooey_decorator 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. @Gooey
  36. def main(argv=None): # IGNORE:C0111
  37. '''Command line options.'''
  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. # Setup argument parser
  57. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  58. parser.add_argument("filename", help="filename")
  59. parser.add_argument("-r", "--recursive", dest="recurse", action="store_true",
  60. help="recurse into subfolders [default: %(default)s]")
  61. parser.add_argument("-v", "--verbose", dest="verbose", action="count",
  62. help="set verbosity level [default: %(default)s]")
  63. parser.add_argument("-i", "--include", action="append",
  64. help="only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]",
  65. metavar="RE")
  66. parser.add_argument("-m", "--mycoolargument", help="mycoolargument")
  67. parser.add_argument("-e", "--exclude", dest="exclude",
  68. help="exclude paths matching this regex pattern. [default: %(default)s]", metavar="RE")
  69. parser.add_argument('-V', '--version', action='version')
  70. parser.add_argument('-T', '--tester', choices=['yes', 'no'])
  71. parser.add_argument(dest="paths", help="paths to folder(s) with source file(s) [default: %(default)s]",
  72. metavar="path", nargs='+')
  73. # for i in parser._actions:
  74. # print i
  75. # Process arguments
  76. args = parser.parse_args()
  77. paths = args.paths
  78. verbose = args.verbose
  79. recurse = args.recurse
  80. inpat = args.include
  81. expat = args.exclude
  82. if verbose > 0:
  83. print("Verbose mode on")
  84. if recurse:
  85. print("Recursive mode on")
  86. else:
  87. print("Recursive mode off")
  88. if inpat and expat and inpat == expat:
  89. raise CLIError("include and exclude pattern are equal! Nothing will be processed.")
  90. for inpath in paths:
  91. ### do something with inpath ###
  92. print(inpath)
  93. return 0
  94. if __name__ == "__main__":
  95. if DEBUG:
  96. sys.argv.append("-h")
  97. # sys.argv.append("-v")
  98. sys.argv.append("-r")
  99. main()
  100. sys.exit()
  101. if TESTRUN:
  102. import doctest
  103. doctest.testmod()
  104. if PROFILE:
  105. import cProfile
  106. import pstats
  107. profile_filename = 'bin.example_argparse_souce_profile.txt'
  108. cProfile.run('main()', profile_filename)
  109. statsfile = open("profile_stats.txt", "wb")
  110. p = pstats.Stats(profile_filename, stream=statsfile)
  111. stats = p.strip_dirs().sort_stats('cumulative')
  112. stats.print_stats()
  113. statsfile.close()
  114. sys.exit(0)
  115. sys.exit(main())