mirror of
https://github.com/derrod/legendary.git
synced 2024-12-22 17:55:27 +00:00
[cli/utils] Add aliases to download command
Eventually the "download" command will be changed to set the --no-install flag by default and "update" will become --update-only. For now remain backwards compatible to give people a chance to adjust their scripts and habits. Fixes #18
This commit is contained in:
parent
bd3095a6be
commit
4d0ac96bdc
|
@ -17,6 +17,7 @@ from sys import exit, stdout
|
||||||
from legendary import __version__, __codename__
|
from legendary import __version__, __codename__
|
||||||
from legendary.core import LegendaryCore
|
from legendary.core import LegendaryCore
|
||||||
from legendary.models.exceptions import InvalidCredentialsError
|
from legendary.models.exceptions import InvalidCredentialsError
|
||||||
|
from legendary.utils.custom_parser import AliasedSubParsersAction
|
||||||
|
|
||||||
# todo custom formatter for cli logger (clean info, highlighted error/warning)
|
# todo custom formatter for cli logger (clean info, highlighted error/warning)
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
|
@ -237,6 +238,10 @@ class LegendaryCLI:
|
||||||
subprocess.Popen(params, cwd=cwd, env=env)
|
subprocess.Popen(params, cwd=cwd, env=env)
|
||||||
|
|
||||||
def install_game(self, args):
|
def install_game(self, args):
|
||||||
|
if args.subparser_name == 'download':
|
||||||
|
logger.info('The "download" command will be changed to set the --no-install command by default '
|
||||||
|
'in the future, please adjust install scripts etc. to use "install" instead.')
|
||||||
|
|
||||||
if not self.core.login():
|
if not self.core.login():
|
||||||
logger.error('Login failed! Cannot continue with download process.')
|
logger.error('Login failed! Cannot continue with download process.')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
@ -410,6 +415,7 @@ class LegendaryCLI:
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description=f'Legendary v{__version__} - "{__codename__}"')
|
parser = argparse.ArgumentParser(description=f'Legendary v{__version__} - "{__codename__}"')
|
||||||
|
parser.register('action', 'parsers', AliasedSubParsersAction)
|
||||||
|
|
||||||
# general arguments
|
# general arguments
|
||||||
parser.add_argument('-v', dest='debug', action='store_true', help='Set loglevel to debug')
|
parser.add_argument('-v', dest='debug', action='store_true', help='Set loglevel to debug')
|
||||||
|
@ -419,7 +425,8 @@ def main():
|
||||||
# all the commands
|
# all the commands
|
||||||
subparsers = parser.add_subparsers(title='Commands', dest='subparser_name')
|
subparsers = parser.add_subparsers(title='Commands', dest='subparser_name')
|
||||||
auth_parser = subparsers.add_parser('auth', help='Authenticate with EPIC')
|
auth_parser = subparsers.add_parser('auth', help='Authenticate with EPIC')
|
||||||
install_parser = subparsers.add_parser('download', help='Download a game',
|
install_parser = subparsers.add_parser('install', help='Download a game',
|
||||||
|
aliases=('download', 'update'),
|
||||||
usage='%(prog)s <App Name> [options]')
|
usage='%(prog)s <App Name> [options]')
|
||||||
uninstall_parser = subparsers.add_parser('uninstall', help='Uninstall (delete) a game')
|
uninstall_parser = subparsers.add_parser('uninstall', help='Uninstall (delete) a game')
|
||||||
launch_parser = subparsers.add_parser('launch', help='Launch a game', usage='%(prog)s <App Name> [options]',
|
launch_parser = subparsers.add_parser('launch', help='Launch a game', usage='%(prog)s <App Name> [options]',
|
||||||
|
@ -518,13 +525,15 @@ def main():
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if args.subparser_name not in ('auth', 'list-games', 'list-installed', 'list-files',
|
if args.subparser_name not in ('auth', 'list-games', 'list-installed', 'list-files',
|
||||||
'launch', 'download', 'uninstall'):
|
'launch', 'download', 'uninstall', 'install', 'update'):
|
||||||
print(parser.format_help())
|
print(parser.format_help())
|
||||||
|
|
||||||
# Print the main help *and* the help for all of the subcommands. Thanks stackoverflow!
|
# Print the main help *and* the help for all of the subcommands. Thanks stackoverflow!
|
||||||
print('Individual command help:')
|
print('Individual command help:')
|
||||||
subparsers = next(a for a in parser._actions if isinstance(a, argparse._SubParsersAction))
|
subparsers = next(a for a in parser._actions if isinstance(a, argparse._SubParsersAction))
|
||||||
for choice, subparser in subparsers.choices.items():
|
for choice, subparser in subparsers.choices.items():
|
||||||
|
if choice in ('install', 'update'):
|
||||||
|
continue
|
||||||
print(f'\nCommand: {choice}')
|
print(f'\nCommand: {choice}')
|
||||||
print(subparser.format_help())
|
print(subparser.format_help())
|
||||||
return
|
return
|
||||||
|
@ -550,7 +559,7 @@ def main():
|
||||||
cli.list_installed(args)
|
cli.list_installed(args)
|
||||||
elif args.subparser_name == 'launch':
|
elif args.subparser_name == 'launch':
|
||||||
cli.launch_game(args, extra)
|
cli.launch_game(args, extra)
|
||||||
elif args.subparser_name == 'download':
|
elif args.subparser_name in ('download', 'install', 'update'):
|
||||||
cli.install_game(args)
|
cli.install_game(args)
|
||||||
elif args.subparser_name == 'uninstall':
|
elif args.subparser_name == 'uninstall':
|
||||||
cli.uninstall_game(args)
|
cli.uninstall_game(args)
|
||||||
|
|
29
legendary/utils/custom_parser.py
Normal file
29
legendary/utils/custom_parser.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
# reference: https://gist.github.com/sampsyo/471779#gistcomment-2886157
|
||||||
|
|
||||||
|
|
||||||
|
class AliasedSubParsersAction(argparse._SubParsersAction):
|
||||||
|
class _AliasedPseudoAction(argparse.Action):
|
||||||
|
def __init__(self, name, aliases, help):
|
||||||
|
dest = name
|
||||||
|
if aliases:
|
||||||
|
dest += ' (%s)' % ','.join(aliases)
|
||||||
|
sup = super(AliasedSubParsersAction._AliasedPseudoAction, self)
|
||||||
|
sup.__init__(option_strings=[], dest=dest, help=help)
|
||||||
|
|
||||||
|
def add_parser(self, name, **kwargs):
|
||||||
|
aliases = kwargs.pop('aliases', [])
|
||||||
|
parser = super(AliasedSubParsersAction, self).add_parser(name, **kwargs)
|
||||||
|
|
||||||
|
# Make the aliases work.
|
||||||
|
for alias in aliases:
|
||||||
|
self._name_parser_map[alias] = parser
|
||||||
|
# Make the help text reflect them, first removing old help entry.
|
||||||
|
if 'help' in kwargs:
|
||||||
|
help = kwargs.pop('help')
|
||||||
|
self._choices_actions.pop()
|
||||||
|
pseudo_action = self._AliasedPseudoAction(name, aliases, help)
|
||||||
|
self._choices_actions.append(pseudo_action)
|
||||||
|
|
||||||
|
return parser
|
Loading…
Reference in a new issue