diff --git a/README.md b/README.md index faa015d..b335385 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,29 @@ with open('out.nbt', 'rb') as io: print(tag) ``` +## Using the CLI + +```text +usage: pynbt.py [-h] [-l] [file] + +Print content of a NBT file in a human-readable format + +positional arguments: + file file to read from or standard input if omitted + +options: + -h, --help show this help message and exit + -l, --little-endian use little endian instead of big endian byte order +``` + +```console +python3 -m pynbt hotbar.nbt +python3 -m pynbt -l house.mcstructure +``` + +_On Windows you might have to replace `python3` with `py` or `python` if +you have installed Python through the Microsoft Store._ + ## Changelog These changelogs are summaries only and not comprehensive. See diff --git a/pynbt.py b/pynbt.py index 5b4c0b3..a97a6aa 100644 --- a/pynbt.py +++ b/pynbt.py @@ -384,3 +384,32 @@ def save(self, io, little_endian=False): write.dst = io self.write(write) + + +if __name__ == "__main__": + from argparse import ArgumentParser, FileType + import sys + + parser = ArgumentParser( + description="Print content of a NBT file in a human-readable format" + ) + + parser.add_argument( + "-l", + "--little-endian", + help="use little endian instead of big endian byte order", + action="store_true", + default=False, + ) + parser.add_argument( + "file", + help="file to read from or standard input if omitted", + nargs="?", + type=FileType("rb"), + default=sys.stdin.buffer, + ) + + args = parser.parse_args() + + nbt = NBTFile(io=args.file, little_endian=args.little_endian) + print(nbt.pretty())