Clients: consolidate argument parsing
This commit is contained in:
		
							parent
							
								
									2007549e01
								
							
						
					
					
						commit
						d42d77d3d3
					
				| 
						 | 
				
			
			@ -17,7 +17,8 @@ from worlds import network_data_package, AutoWorldRegister
 | 
			
		|||
 | 
			
		||||
logger = logging.getLogger("Client")
 | 
			
		||||
 | 
			
		||||
gui_enabled = Utils.is_frozen() or "--nogui" not in sys.argv
 | 
			
		||||
# without terminal we have to use gui mode
 | 
			
		||||
gui_enabled = not sys.stdout or "--nogui" not in sys.argv
 | 
			
		||||
 | 
			
		||||
log_folder = Utils.local_path("logs")
 | 
			
		||||
os.makedirs(log_folder, exist_ok=True)
 | 
			
		||||
| 
						 | 
				
			
			@ -57,7 +58,7 @@ class ClientCommandProcessor(CommandProcessor):
 | 
			
		|||
        """List all missing location checks, from your local game state"""
 | 
			
		||||
        if not self.ctx.game:
 | 
			
		||||
            self.output("No game set, cannot determine missing checks.")
 | 
			
		||||
            return
 | 
			
		||||
            return False
 | 
			
		||||
        count = 0
 | 
			
		||||
        checked_count = 0
 | 
			
		||||
        for location, location_id in AutoWorldRegister.world_types[self.ctx.game].location_name_to_id.items():
 | 
			
		||||
| 
						 | 
				
			
			@ -531,11 +532,20 @@ def init_logging(name: str):
 | 
			
		|||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_base_parser(description=None):
 | 
			
		||||
    import argparse
 | 
			
		||||
    parser = argparse.ArgumentParser(description=description)
 | 
			
		||||
    parser.add_argument('--connect', default=None, help='Address of the multiworld host.')
 | 
			
		||||
    parser.add_argument('--password', default=None, help='Password of the multiworld host.')
 | 
			
		||||
    if sys.stdout:  # If terminal output exists, offer gui-less mode
 | 
			
		||||
        parser.add_argument('--nogui', default=False, action='store_true', help="Turns off Client GUI.")
 | 
			
		||||
    return parser
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    # Text Mode to use !hint and such with games that have no text entry
 | 
			
		||||
    init_logging("TextClient")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    class TextContext(CommonContext):
 | 
			
		||||
        tags = {"AP", "IgnoreGame"}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -586,15 +596,9 @@ if __name__ == '__main__':
 | 
			
		|||
        if input_task:
 | 
			
		||||
            input_task.cancel()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    import argparse
 | 
			
		||||
    import colorama
 | 
			
		||||
 | 
			
		||||
    parser = argparse.ArgumentParser(description="Gameless Archipelago Client, for text interfaction.")
 | 
			
		||||
    parser.add_argument('--connect', default=None, help='Address of the multiworld host.')
 | 
			
		||||
    parser.add_argument('--password', default=None, help='Password of the multiworld host.')
 | 
			
		||||
    if not Utils.is_frozen():  # Frozen state has no cmd window in the first place
 | 
			
		||||
        parser.add_argument('--nogui', default=False, action='store_true', help="Turns off Client GUI.")
 | 
			
		||||
    parser = get_base_parser(description="Gameless Archipelago Client, for text interfaction.")
 | 
			
		||||
 | 
			
		||||
    args, rest = parser.parse_known_args()
 | 
			
		||||
    colorama.init()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,7 @@ import colorama
 | 
			
		|||
import asyncio
 | 
			
		||||
from queue import Queue
 | 
			
		||||
from CommonClient import CommonContext, server_loop, console_loop, ClientCommandProcessor, logger, gui_enabled, \
 | 
			
		||||
    init_logging
 | 
			
		||||
    init_logging, get_base_parser
 | 
			
		||||
from MultiServer import mark_raw
 | 
			
		||||
 | 
			
		||||
import Utils
 | 
			
		||||
| 
						 | 
				
			
			@ -353,17 +353,11 @@ class FactorioJSONtoTextParser(JSONtoTextParser):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    import argparse
 | 
			
		||||
 | 
			
		||||
    parser = argparse.ArgumentParser(description="Optional arguments to FactorioClient follow. "
 | 
			
		||||
                                                 "Remaining arguments get passed into bound Factorio instance."
 | 
			
		||||
                                                 "Refer to Factorio --help for those.")
 | 
			
		||||
    parser = get_base_parser(description="Optional arguments to FactorioClient follow. "
 | 
			
		||||
                                         "Remaining arguments get passed into bound Factorio instance."
 | 
			
		||||
                                         "Refer to Factorio --help for those.")
 | 
			
		||||
    parser.add_argument('--rcon-port', default='24242', type=int, help='Port to use to communicate with Factorio')
 | 
			
		||||
    parser.add_argument('--rcon-password', help='Password to authenticate with RCON.')
 | 
			
		||||
    parser.add_argument('--connect', default=None, help='Address of the multiworld host.')
 | 
			
		||||
    parser.add_argument('--password', default=None, help='Password of the multiworld host.')
 | 
			
		||||
    if not Utils.is_frozen():  # Frozen state has no cmd window in the first place
 | 
			
		||||
        parser.add_argument('--nogui', default=False, action='store_true', help="Turns off Client GUI.")
 | 
			
		||||
 | 
			
		||||
    args, rest = parser.parse_known_args()
 | 
			
		||||
    colorama.init()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
from __future__ import annotations
 | 
			
		||||
import argparse
 | 
			
		||||
import atexit
 | 
			
		||||
 | 
			
		||||
exit_func = atexit.register(input, "Press enter to close.")
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +26,8 @@ from worlds.alttp import Regions, Shops
 | 
			
		|||
from worlds.alttp import Items
 | 
			
		||||
from worlds.alttp.Rom import ROM_PLAYER_LIMIT
 | 
			
		||||
import Utils
 | 
			
		||||
from CommonClient import CommonContext, server_loop, console_loop, ClientCommandProcessor, gui_enabled, init_logging
 | 
			
		||||
from CommonClient import CommonContext, server_loop, console_loop, ClientCommandProcessor, gui_enabled, init_logging, \
 | 
			
		||||
    get_base_parser
 | 
			
		||||
 | 
			
		||||
init_logging("LttPClient")
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -473,8 +473,8 @@ def launch_sni(ctx: Context):
 | 
			
		|||
 | 
			
		||||
    if os.path.isfile(sni_path):
 | 
			
		||||
        snes_logger.info(f"Attempting to start {sni_path}")
 | 
			
		||||
        import subprocess
 | 
			
		||||
        if Utils.is_frozen():  # if it spawns a visible console, may as well populate it
 | 
			
		||||
        import sys
 | 
			
		||||
        if not sys.stdout:  # if it spawns a visible console, may as well populate it
 | 
			
		||||
            subprocess.Popen(sni_path, cwd=os.path.dirname(sni_path))
 | 
			
		||||
        else:
 | 
			
		||||
            subprocess.Popen(sni_path, cwd=os.path.dirname(sni_path), stdout=subprocess.DEVNULL,
 | 
			
		||||
| 
						 | 
				
			
			@ -951,17 +951,13 @@ async def run_game(romfile):
 | 
			
		|||
 | 
			
		||||
async def main():
 | 
			
		||||
    multiprocessing.freeze_support()
 | 
			
		||||
    parser = argparse.ArgumentParser()
 | 
			
		||||
    parser = get_base_parser()
 | 
			
		||||
    parser.add_argument('diff_file', default="", type=str, nargs="?",
 | 
			
		||||
                        help='Path to a Archipelago Binary Patch file')
 | 
			
		||||
    parser.add_argument('--snes', default='localhost:8080', help='Address of the SNI server.')
 | 
			
		||||
    parser.add_argument('--connect', default=None, help='Address of the multiworld host.')
 | 
			
		||||
    parser.add_argument('--password', default=None, help='Password of the multiworld host.')
 | 
			
		||||
    parser.add_argument('--loglevel', default='info', choices=['debug', 'info', 'warning', 'error', 'critical'])
 | 
			
		||||
    if not Utils.is_frozen():  # Frozen state has no cmd window in the first place
 | 
			
		||||
        parser.add_argument('--nogui', default=False, action='store_true', help="Turns off Client GUI.")
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
    logging.basicConfig(format='%(message)s', level=getattr(logging, args.loglevel.upper(), logging.INFO))
 | 
			
		||||
 | 
			
		||||
    if args.diff_file:
 | 
			
		||||
        import Patch
 | 
			
		||||
        logging.info("Patch file was supplied. Creating sfc rom..")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue