Replace getopt code with argparse as its more modern.

This commit is contained in:
James David Clarke 2024-01-09 08:16:05 +00:00 committed by the-real-herowl
parent 6324c805eb
commit 254fe4d98e

@ -8,33 +8,19 @@ __author__ = "Wuzzy"
__license__ = "MIT License" __license__ = "MIT License"
__status__ = "Development" __status__ = "Development"
import shutil, csv, os, tempfile, sys, getopt, glob import shutil, csv, os, tempfile, sys, argparse, glob
from PIL import Image from PIL import Image
from collections import Counter from collections import Counter
# Constants
SUPPORTED_MINECRAFT_VERSION="1.20"
# Helper vars # Helper vars
home = os.environ["HOME"] home = os.environ["HOME"]
mineclone2_path = home + "/.minetest/games/mineclone2" mineclone2_path = home + "/.minetest/games/mineclone2"
working_dir = os.getcwd() working_dir = os.getcwd()
appname = "Texture_Converter.py" appname = "Texture_Converter.py"
### SETTINGS ###
output_dir = working_dir
base_dir = None
# If True, will only make console output but not convert anything.
dry_run = False
# If True, textures will be put into a texture pack directory structure.
# If False, textures will be put into MineClone 2 directories.
make_texture_pack = True
# If True, prints all copying actions
verbose = False
PXSIZE = None
def detect_pixel_size(directory): def detect_pixel_size(directory):
sizes = [] sizes = []
for filename in glob.glob(directory + '/**/*.png', recursive=True): for filename in glob.glob(directory + '/**/*.png', recursive=True):
@ -46,55 +32,32 @@ def detect_pixel_size(directory):
print(f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}") print(f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}")
return most_common_size[0] return most_common_size[0]
syntax_help = appname+""" -i <input dir> [-o <output dir>] [-d] [-v|-q] [-h] # Argument parsing
Mandatory argument: description_text = f"""This is the official MineClone 2 Texture Converter.
-i <input directory>
Directory of Minecraft resource pack to convert
Optional arguments:
-p <texture size>
Specify the size (in pixels) of the original textures (default: 16)
-o <output directory>
Directory in which to put the resulting Minetest texture pack
(default: working directory)
-d
Just pretend to convert textures and just print output, but do not actually
change any files.
-v
Print out all copying actions
-h
Show this help and exit"""
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:o:p:dv")
except getopt.GetoptError:
print(
"""ERROR! The options you gave me make no sense!
Here's the syntax reference:""")
print(syntax_help)
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print(
"""This is the official MineClone 2 Texture Converter.
This will convert textures from Minecraft resource packs to This will convert textures from Minecraft resource packs to
a Minetest texture pack. a Minetest texture pack.
Supported Minecraft version: 1.20 (Java Edition) Supported Minecraft version: {SUPPORTED_MINECRAFT_VERSION} (Java Edition)
"""
parser = argparse.ArgumentParser(description=description_text)
parser.add_argument("-i", "--input", required=True, help="Directory of Minecraft resource pack to convert")
parser.add_argument("-o", "--output", default=working_dir, help="Directory in which to put the resulting Minetest texture pack")
parser.add_argument("-p", "--pixelsize", type=int, help="Size (in pixels) of the original textures")
parser.add_argument("-d", "--dry_run", action="store_true", help="Pretend to convert textures without changing any files")
parser.add_argument("-v", "--verbose", action="store_true", help="Print out all copying actions")
args = parser.parse_args()
Syntax:""") ### SETTINGS ###
print(syntax_help) base_dir = args.input
sys.exit() output_dir = args.output
elif opt == "-d": PXSIZE = args.pixelsize
dry_run = True # If True, will only make console output but not convert anything.
elif opt == "-v": dry_run = args.dry_run
verbose = True # If True, prints all copying actions
elif opt == "-i": verbose = args.verbose
base_dir = arg # If True, textures will be put into a texture pack directory structure.
elif opt == "-o": # If False, textures will be put into MineClone 2 directories.
output_dir = arg make_texture_pack = True # Adjust as needed
elif opt == "-p":
PXSIZE = int(arg)
if PXSIZE is None: if PXSIZE is None:
PXSIZE = detect_pixel_size(base_dir) PXSIZE = detect_pixel_size(base_dir)