Merge pull request 'Refactored the way methods are parsed; Add clean method; Add myself to LICENSE' (#1) from rndtrash/mc-get:master into master
Reviewed-on: https://git.memer.work/Tea-Sanctuary/mc-get/pulls/1
This commit is contained in:
commit
9dbc2b3666
2 changed files with 28 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
|||
Copyright (c) 2022 Evgenij Titarenko
|
||||
Copyright (c) 2022 Evgenij Titarenko, Ivan Kuzmenko
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
44
mc-get.py
Normal file → Executable file
44
mc-get.py
Normal file → Executable file
|
@ -1,3 +1,4 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import api
|
||||
import mcfs
|
||||
|
@ -44,23 +45,32 @@ def install(projects:list):
|
|||
def search():
|
||||
pass
|
||||
|
||||
def clean():
|
||||
if mcfs.is_path_exist(mcfs.cache_dir):
|
||||
files = os.listdir(mcfs.cache_dir)
|
||||
if len(files) > 0:
|
||||
for file in files:
|
||||
os.remove(os.path.join(mcfs.cache_dir, file))
|
||||
print("Cache cleared successfully.")
|
||||
return
|
||||
|
||||
print("Nothing to clear.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
desc = '''Minecraft mods packet\
|
||||
manager based on modrinth API\n\nMethods:\n install - install mod\n \
|
||||
validate - validate mods installation\n search - search mods'''
|
||||
desc = "Minecraft mods packet manager based on Modrinth API"
|
||||
parser = argparse.ArgumentParser(description=desc,\
|
||||
formatter_class=argparse.RawTextHelpFormatter)
|
||||
parser.add_argument("method", choices=['install', 'search', 'validate'],\
|
||||
metavar="method")
|
||||
parser.add_argument("method_args", nargs="*")
|
||||
args = parser.parse_args()
|
||||
match args.method:
|
||||
case "install":
|
||||
install(args.method_args)
|
||||
case "search":
|
||||
search()
|
||||
case "validate":
|
||||
validate()
|
||||
case _:
|
||||
print("Unknown method", args.method)
|
||||
parser.print_help()
|
||||
subparsers = parser.add_subparsers(dest="method", required=True) # Переменная, в которую будет записано имя подкоманды
|
||||
|
||||
parser_install = subparsers.add_parser("install", help="Install one or more mods or resources")
|
||||
parser_install.add_argument("projects", nargs="+")
|
||||
|
||||
parser_search = subparsers.add_parser("search", help="Find a mod or a resource")
|
||||
|
||||
parser_validate = subparsers.add_parser("validate", help="Validate the installation")
|
||||
|
||||
parser_clean = subparsers.add_parser("clean", help="Clean the cache of this program")
|
||||
|
||||
kwargs = vars(parser.parse_args()) # Получаем все поля получившегося Namespace и пихаем в словарь
|
||||
globals()[kwargs.pop("method")](**kwargs) # Из глобального контекста получаем функцию с названием как в method, заодно вытаскивая название метода из списка аргументов,
|
||||
# затем вызываем функцию с распакованным словарём в качестве аргумента
|
||||
|
|
Loading…
Add table
Reference in a new issue