import re
import sys


def usage():
  print('usage: python3 GetLz4LibraryVersion.py <path/to/lz4.h>')
  sys.exit(1)


def find_version(filepath):
  version_file_data = None
  with open(filepath) as fd:
    version_file_data = fd.read()

  patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$
"""
  regex = re.compile(patterns, re.MULTILINE)
  version_match = regex.search(version_file_data)
  if version_match:
    return version_match.groups()
  raise Exception("Unable to find version string.")


def main():
  if len(sys.argv) < 2:
    usage()

  filepath = sys.argv[1]
  version_tup = find_version(filepath)
  print('.'.join(version_tup))


if __name__ == '__main__':
  main()

import re
import sys


def find_version(filepath):
  version_file_data = None
  with open(filepath) as fd:
    version_file_data = fd.read()

  patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$
"""
  regex = re.compile(patterns, re.MULTILINE)
  version_match = regex.search(version_file_data)
  if version_match:
    return version_match.groups()
  raise Exception("Unable to find version string.")


def main():
  import argparse
  parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h')
  parser.add_argument('file', help='path to lib/lz4.h')
  args = parser.parse_args()
  filepath = args.file
  version_tup = find_version(filepath)
  print('.'.join(version_tup))


if __name__ == '__main__':
  main()

Use argparse instead of manually parsing
Use argparse instead of manually parsing [skip ci]

  import re
  import sys
- 
- 
- def usage():
-   print('usage: python3 GetLz4LibraryVersion.py <path/to/lz4.h>')
-   sys.exit(1)
  
  
  def find_version(filepath):
    version_file_data = None
    with open(filepath) as fd:
      version_file_data = fd.read()
  
    patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$
  #\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$
  #\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$
  """
    regex = re.compile(patterns, re.MULTILINE)
    version_match = regex.search(version_file_data)
    if version_match:
      return version_match.groups()
    raise Exception("Unable to find version string.")
  
  
  def main():
-   if len(sys.argv) < 2:
-     usage()
- 
-   filepath = sys.argv[1]
+   import argparse
+   parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h')
+   parser.add_argument('file', help='path to lib/lz4.h')
+   args = parser.parse_args()
+   filepath = args.file
    version_tup = find_version(filepath)
    print('.'.join(version_tup))
  
  
  if __name__ == '__main__':
    main()
  
Use argparse instead of manually parsing
## Code Before:
import re
import sys


def usage():
  print('usage: python3 GetLz4LibraryVersion.py <path/to/lz4.h>')
  sys.exit(1)


def find_version(filepath):
  version_file_data = None
  with open(filepath) as fd:
    version_file_data = fd.read()

  patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$
"""
  regex = re.compile(patterns, re.MULTILINE)
  version_match = regex.search(version_file_data)
  if version_match:
    return version_match.groups()
  raise Exception("Unable to find version string.")


def main():
  if len(sys.argv) < 2:
    usage()

  filepath = sys.argv[1]
  version_tup = find_version(filepath)
  print('.'.join(version_tup))


if __name__ == '__main__':
  main()

## Instruction:
Use argparse instead of manually parsing
## Code After:
import re
import sys


def find_version(filepath):
  version_file_data = None
  with open(filepath) as fd:
    version_file_data = fd.read()

  patterns = r"""#\s*define\s+LZ4_VERSION_MAJOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_MINOR\s+([0-9]+).*$
#\s*define\s+LZ4_VERSION_RELEASE\s+([0-9]+).*$
"""
  regex = re.compile(patterns, re.MULTILINE)
  version_match = regex.search(version_file_data)
  if version_match:
    return version_match.groups()
  raise Exception("Unable to find version string.")


def main():
  import argparse
  parser = argparse.ArgumentParser(description='Print lz4 version from lib/lz4.h')
  parser.add_argument('file', help='path to lib/lz4.h')
  args = parser.parse_args()
  filepath = args.file
  version_tup = find_version(filepath)
  print('.'.join(version_tup))


if __name__ == '__main__':
  main()
