qtutils: rename fn variables to func
[git-cola.git] / qtpy / cli.py
blob3a6299703fb85aba8efd14eeda23341ca6fc481d
1 # -----------------------------------------------------------------------------
2 # Copyright © 2009- The QtPy Contributors
4 # Released under the terms of the MIT License
5 # (see LICENSE.txt for details)
6 # -----------------------------------------------------------------------------
8 """Provide a CLI to allow configuring developer settings, including mypy."""
10 # Standard library imports
11 import argparse
12 import textwrap
15 def print_version():
16 """Print the current version of the package."""
17 import qtpy
18 print('QtPy version', qtpy.__version__)
21 def generate_mypy_args():
22 """Generate a string with always-true/false args to pass to mypy."""
23 options = {False: '--always-false', True: '--always-true'}
25 import qtpy
27 apis_active = {name: qtpy.API == name for name in qtpy.API_NAMES}
28 mypy_args = ' '.join(
29 f'{options[is_active]}={name.upper()}'
30 for name, is_active in apis_active.items()
32 return mypy_args
35 def print_mypy_args():
36 """Print the generated mypy args to stdout."""
37 print(generate_mypy_args())
40 def generate_arg_parser():
41 """Generate the argument parser for the dev CLI for QtPy."""
42 parser = argparse.ArgumentParser(
43 description='Features to support development with QtPy.',
45 parser.set_defaults(func=parser.print_help)
47 parser.add_argument(
48 '--version', action='store_const', dest='func', const=print_version,
49 help='If passed, will print the version and exit')
51 cli_subparsers = parser.add_subparsers(
52 title='Subcommands', help='Subcommand to run', metavar='Subcommand')
54 # Parser for the MyPy args subcommand
55 mypy_args_parser = cli_subparsers.add_parser(
56 name='mypy-args',
57 help='Generate command line arguments for using mypy with QtPy.',
58 formatter_class=argparse.RawTextHelpFormatter,
59 description=textwrap.dedent(
60 """
61 Generate command line arguments for using mypy with QtPy.
63 This will generate strings similar to the following
64 which help guide mypy through which library QtPy would have used
65 so that mypy can get the proper underlying type hints.
67 --always-false=PYQT5 --always-false=PYQT6 --always-true=PYSIDE2 --always-false=PYSIDE6
69 It can be used as follows on Bash or a similar shell:
71 mypy --package mypackage $(qtpy mypy-args)
72 """
75 mypy_args_parser.set_defaults(func=print_mypy_args)
77 return parser
80 def main(args=None):
81 """Run the development CLI for QtPy."""
82 parser = generate_arg_parser()
83 parsed_args = parser.parse_args(args=args)
85 reserved_params = {'func'}
86 cleaned_args = {key: value for key, value in vars(parsed_args).items()
87 if key not in reserved_params}
88 parsed_args.func(**cleaned_args)