Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pynbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())