Improve shell completion support

This commit is contained in:
Tobias Gruetzmacher 2023-11-19 22:15:24 +01:00 committed by D. Moonfire
parent 11cc57c190
commit bff9a86d81

View file

@ -2,9 +2,15 @@
# SPDX-FileCopyrightText: © 2004 Tristan Seligmann and Jonathan Jacobs # SPDX-FileCopyrightText: © 2004 Tristan Seligmann and Jonathan Jacobs
# SPDX-FileCopyrightText: © 2012 Bastian Kleineidam # SPDX-FileCopyrightText: © 2012 Bastian Kleineidam
# SPDX-FileCopyrightText: © 2015 Tobias Gruetzmacher # SPDX-FileCopyrightText: © 2015 Tobias Gruetzmacher
# PYTHON_ARGCOMPLETE_OK
from __future__ import annotations
import argparse import argparse
import contextlib
import importlib
import os import os
import platform import platform
from collections.abc import Iterable
from platformdirs import PlatformDirs from platformdirs import PlatformDirs
@ -18,7 +24,7 @@ from .util import internal_error, strlimit
class ArgumentParser(argparse.ArgumentParser): class ArgumentParser(argparse.ArgumentParser):
"""Custom argument parser.""" """Custom argument parser."""
def print_help(self, file=None): def print_help(self, file=None) -> None:
"""Paginate help message on TTYs.""" """Paginate help message on TTYs."""
with out.pager(): with out.pager():
out.info(self.format_help()) out.info(self.format_help())
@ -46,7 +52,7 @@ User plugin directory: {user_plugin_path}
""" """
def setup_options(): def setup_options() -> ArgumentParser:
"""Construct option parser. """Construct option parser.
@return: new option parser @return: new option parser
@rtype argparse.ArgumentParser @rtype argparse.ArgumentParser
@ -65,8 +71,8 @@ def setup_options():
help='traverse and retrieve all comic strips') help='traverse and retrieve all comic strips')
parser.add_argument('-c', '--continue', action='store_true', dest='cont', parser.add_argument('-c', '--continue', action='store_true', dest='cont',
help='traverse and retrieve comic strips until an existing one is found') help='traverse and retrieve comic strips until an existing one is found')
parser.add_argument('-b', '--basepath', action='store', default='Comics', basepath_opt = parser.add_argument('-b', '--basepath', action='store',
metavar='PATH', default='Comics', metavar='PATH',
help='set the path to create invidivual comic directories in, default is Comics') help='set the path to create invidivual comic directories in, default is Comics')
parser.add_argument('--baseurl', action='store', metavar='PATH', parser.add_argument('--baseurl', action='store', metavar='PATH',
help='the base URL of your comics directory (for RSS, HTML, etc.);' help='the base URL of your comics directory (for RSS, HTML, etc.);'
@ -103,16 +109,22 @@ def setup_options():
# are not "real" (moved & removed) # are not "real" (moved & removed)
parser.add_argument('--list-all', action='store_true', parser.add_argument('--list-all', action='store_true',
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
parser.add_argument('comic', nargs='*', comic_arg = parser.add_argument('comic', nargs='*',
help='comic module name (including case insensitive substrings)') help='comic module name (including case insensitive substrings)')
try: comic_arg.completer = scraper_completion
import argcomplete with contextlib.suppress(ImportError):
argcomplete.autocomplete(parser) completers = importlib.import_module('argcomplete.completers')
except ImportError: basepath_opt.completer = completers.DirectoriesCompleter()
pass importlib.import_module('argcomplete').autocomplete(parser)
return parser return parser
def scraper_completion(**kwargs) -> Iterable[str]:
"""Completion helper for argcomplete."""
scrapercache.adddir(user_plugin_path)
return (comic.name for comic in scrapercache.all())
def display_version(verbose): def display_version(verbose):
"""Display application name, version, copyright and license.""" """Display application name, version, copyright and license."""
print(configuration.App) print(configuration.App)