{"id":7101,"library":"comment-parser","title":"Comment Parser","description":"comment-parser is a Python module designed to extract comments from various source code files. It supports common languages like C, C++, Java, JavaScript, Python, and others, handling both single-line and multi-line comment formats. The library, currently at version 1.2.5, features an active release schedule, with recent updates focusing on setup stability and adding typing information.","status":"active","version":"1.2.5","language":"en","source_language":"en","source_url":"http://github.com/jeanralphaviles/comment_parser","tags":["parser","comments","code analysis","source code","multi-language"],"install":[{"cmd":"pip install comment-parser","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Required for automatic MIME type deduction on OSX and Windows. Linux/Unix systems may require the 'libmagic' system library.","package":"python-magic","optional":true}],"imports":[{"note":"The main entry point for the library's parsing functions.","symbol":"comment_parser","correct":"from comment_parser import comment_parser"},{"note":"The object type returned by parsing functions, representing an extracted comment. Often accessed directly from the list returned by `extract_comments`.","symbol":"Comment","correct":"from comment_parser.parsers.common import Comment"}],"quickstart":{"code":"import os\nimport tempfile\nfrom comment_parser import comment_parser\n\n# Example 1: Extract comments from a string\npython_code = \"\"\"\n# This is a single-line Python comment\ndef example_function():\n    '''\n    This is a docstring, not a comment parsed by comment-parser by default.\n    '''\n    x = 10  # Inline comment\n\n# Another line\n\"\"\"\n\ntry:\n    # Explicitly specify MIME type for robust parsing\n    comments_from_str = comment_parser.extract_comments_from_str(python_code, mime='text/x-python')\n    print(\"\\n--- Comments from string ---\")\n    for comment in comments_from_str:\n        print(f\"[Line {comment.line_number}] {comment.text} (Multiline: {comment.is_multiline})\")\nexcept Exception as e:\n    print(f\"Error parsing string: {e}\")\n\n# Example 2: Extract comments from a file\n# Create a dummy file for demonstration\ntemp_dir = tempfile.gettempdir()\nfile_path = os.path.join(temp_dir, 'example.c')\n\nc_code = \"\"\"\n/* This is a multi-line C comment\n * spanning several lines. */\n#include <stdio.h>\n\n// Single line C comment\nint main() {\n    printf(\"Hello, World!\");\n    return 0;\n}\n\"\"\"\n\nwith open(file_path, 'w') as f:\n    f.write(c_code)\n\ntry:\n    # Using extract_comments with a file path\n    comments_from_file = comment_parser.extract_comments(file_path, mime='text/x-c')\n    print(\"\\n--- Comments from file ---\")\n    for comment in comments_from_file:\n        print(f\"[Line {comment.line_number}] {comment.text} (Multiline: {comment.is_multiline})\")\nexcept Exception as e:\n    print(f\"Error parsing file: {e}\")\nfinally:\n    # Clean up the dummy file\n    if os.path.exists(file_path):\n        os.remove(file_path)\n","lang":"python","description":"This quickstart demonstrates how to extract comments from both a string and a temporary file. It uses `extract_comments_from_str` and `extract_comments` by explicitly specifying the MIME type for the code, which is recommended for reliable parsing across different languages. The output for each comment includes its text, line number, and whether it's a multi-line comment."},"warnings":[{"fix":"On OSX/Windows, `pip install python-magic`. On Linux/Unix, install `libmagic` via your system package manager (e.g., `sudo apt-get install libmagic-dev` or `sudo yum install file-devel`) in addition to `pip install comment-parser`. Consider explicitly passing the `mime` argument to `extract_comments` or `extract_comments_from_str` for unsupported types or when autodetection fails.","message":"Automatic MIME type detection requires external dependencies (`python-magic` for Python and `libmagic` system library). Without these, the parser might fail to correctly identify file types, leading to `UnsupportedError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always check the return value or handle the `UnsupportedError` exception. If the MIME type is known, pass it explicitly using the `mime` argument (e.g., `mime='text/x-python'`). Refer to the project's `comment_parser.py` source for the definitive list of supported MIME types.","message":"The library raises an `UnsupportedError` if it encounters a file or string with a MIME type that it does not have a parser for, or if MIME type deduction fails. While many common languages are supported (C, C++, Java, Python, JavaScript, HTML, XML, Go, Ruby, Shell), niche or custom language files may not be.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are using `pip install comment-parser` and importing `from comment_parser import comment_parser` to use the Python version of the library.","message":"This Python library `comment-parser` is distinct from a similarly named JavaScript library (`comment-parser` on NPM) and a Rust crate (`comment-parser` on crates.io). Features, APIs, and breaking changes from those other ecosystems do not apply here.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Explicitly provide the `mime` argument (e.g., `comment_parser.extract_comments(filename, mime='text/x-python')`). If the language is genuinely unsupported, consider using a different tool or contributing a parser. Ensure `python-magic` and `libmagic` (for Linux/Unix) are installed if relying on auto-detection.","cause":"The library does not have a parser for the detected or provided MIME type, or `python-magic` (and `libmagic`) is not installed/configured to correctly deduce the MIME type.","error":"comment_parser.errors.UnsupportedError: The given mime type for the file is not supported."},{"fix":"Install the `python-magic` package: `pip install python-magic`. On Linux/Unix, also ensure the `libmagic` system library is installed (e.g., `sudo apt-get install libmagic-dev` or `sudo yum install file-devel`).","cause":"The `python-magic` library, which `comment-parser` uses for automatic MIME type deduction on some operating systems, is not installed.","error":"ModuleNotFoundError: No module named 'magic'"}]}