python-3.xfilescriptingmetadatayaml-front-matter

Is possible to have a Front Matter header in a script?


I was thinking in how to include some metadata in my script files in a way that other scripts could process this information without interfering in its ./ execution.

YAML Front Matter came to my mind, but obviously its --- syntax produce error either in #!/bin/bash as in #!/usr/bin/python3, for example.

  1. Is there some simple way to allow a non-comment block (as Front Metter's one) to be ignored by script ./ execution?
  2. Or any other known way to make a file carry some metadatas possible to be accessed anyway without interfer in its generic execution?

Solution

  • If you can't use a comment, you could use a Python object of some kind.

    This is a bit like the way doctstrings work in Python: The string is an expression but it's evaluation doesn't have side effects and Python uses it as metadata.

    However if you need a shebang (#!...) that does have to come first. Your metadata processor will have to skip that line.