|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +# A part of NonVisual Desktop Access (NVDA) |
| 3 | +# Copyright (C) 2024 NV Access Limited, Cyrille Bougot |
| 4 | +# This file is covered by the GNU General Public License. |
| 5 | +# See the file COPYING for more details. |
| 6 | + |
| 7 | +import argparse |
| 8 | +import sys |
| 9 | +import winUser |
| 10 | + |
| 11 | +from typing import IO |
| 12 | + |
| 13 | + |
| 14 | +class _WideParserHelpFormatter(argparse.RawTextHelpFormatter): |
| 15 | + def __init__(self, prog: str, indent_increment: int = 2, max_help_position: int = 50, width: int = 1000): |
| 16 | + """ |
| 17 | + A custom formatter for argparse help messages that uses a wider width. |
| 18 | + :param prog: The program name. |
| 19 | + :param indent_increment: The number of spaces to indent for each level of nesting. |
| 20 | + :param max_help_position: The maximum starting column of the help text. |
| 21 | + :param width: The width of the help text. |
| 22 | + """ |
| 23 | + |
| 24 | + super().__init__(prog, indent_increment, max_help_position, width) |
| 25 | + |
| 26 | + |
| 27 | +class NoConsoleOptionParser(argparse.ArgumentParser): |
| 28 | + """ |
| 29 | + A commandline option parser that shows its messages using dialogs, |
| 30 | + as this pyw file has no dos console window associated with it. |
| 31 | + """ |
| 32 | + |
| 33 | + def print_help(self, file: IO[str] | None = None): |
| 34 | + """Shows help in a standard Windows message dialog""" |
| 35 | + winUser.MessageBox(0, self.format_help(), "Help", 0) |
| 36 | + |
| 37 | + def error(self, message: str): |
| 38 | + """Shows an error in a standard Windows message dialog, and then exits NVDA""" |
| 39 | + out = "" |
| 40 | + out = self.format_usage() |
| 41 | + out += f"\nerror: {message}" |
| 42 | + winUser.MessageBox(0, out, "Command-line Argument Error", winUser.MB_ICONERROR) |
| 43 | + sys.exit(2) |
| 44 | + |
| 45 | + |
| 46 | +def stringToBool(string): |
| 47 | + """Wrapper for configobj.validate.is_boolean to raise the proper exception for wrong values.""" |
| 48 | + from configobj.validate import is_boolean, ValidateError |
| 49 | + |
| 50 | + try: |
| 51 | + return is_boolean(string) |
| 52 | + except ValidateError as e: |
| 53 | + raise argparse.ArgumentTypeError(e.message) |
| 54 | + |
| 55 | + |
| 56 | +def stringToLang(value: str) -> str: |
| 57 | + """Perform basic case normalization for ease of use.""" |
| 58 | + import languageHandler |
| 59 | + |
| 60 | + if value.casefold() == "Windows".casefold(): |
| 61 | + normalizedLang = "Windows" |
| 62 | + else: |
| 63 | + normalizedLang = languageHandler.normalizeLanguage(value) |
| 64 | + possibleLangNames = languageHandler.listNVDALocales() |
| 65 | + if normalizedLang is not None and normalizedLang in possibleLangNames: |
| 66 | + return normalizedLang |
| 67 | + raise argparse.ArgumentTypeError(f"Language code should be one of:\n{', '.join(possibleLangNames)}.") |
| 68 | + |
| 69 | + |
| 70 | +_parser: NoConsoleOptionParser | None = None |
| 71 | +"""The arguments parser used by NVDA. |
| 72 | +""" |
| 73 | + |
| 74 | + |
| 75 | +def _createNVDAArgParser() -> NoConsoleOptionParser: |
| 76 | + """Create a parser to process NVDA option arguments.""" |
| 77 | + |
| 78 | + parser = NoConsoleOptionParser(formatter_class=_WideParserHelpFormatter, allow_abbrev=False) |
| 79 | + quitGroup = parser.add_mutually_exclusive_group() |
| 80 | + quitGroup.add_argument( |
| 81 | + "-q", |
| 82 | + "--quit", |
| 83 | + action="store_true", |
| 84 | + dest="quit", |
| 85 | + default=False, |
| 86 | + help="Quit already running copy of NVDA", |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + "-k", |
| 90 | + "--check-running", |
| 91 | + action="store_true", |
| 92 | + dest="check_running", |
| 93 | + default=False, |
| 94 | + help="Report whether NVDA is running via the exit code; 0 if running, 1 if not running", |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + "-f", |
| 98 | + "--log-file", |
| 99 | + dest="logFileName", |
| 100 | + type=str, |
| 101 | + help="The file to which log messages should be written.\n" |
| 102 | + 'Default destination is "%%TEMP%%\\nvda.log".\n' |
| 103 | + "Logging is always disabled if secure mode is enabled.\n", |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + "-l", |
| 107 | + "--log-level", |
| 108 | + dest="logLevel", |
| 109 | + type=int, |
| 110 | + default=0, # 0 means unspecified in command line. |
| 111 | + choices=[10, 12, 15, 20, 100], |
| 112 | + help="The lowest level of message logged (debug 10, input/output 12, debugwarning 15, info 20, off 100).\n" |
| 113 | + "Default value is 20 (info) or the user configured setting.\n" |
| 114 | + "Logging is always disabled if secure mode is enabled.\n", |
| 115 | + ) |
| 116 | + parser.add_argument( |
| 117 | + "-c", |
| 118 | + "--config-path", |
| 119 | + dest="configPath", |
| 120 | + default=None, |
| 121 | + type=str, |
| 122 | + help="The path where all settings for NVDA are stored.\n" |
| 123 | + "The default value is forced if secure mode is enabled.\n", |
| 124 | + ) |
| 125 | + parser.add_argument( |
| 126 | + "-n", |
| 127 | + "--lang", |
| 128 | + dest="language", |
| 129 | + default=None, |
| 130 | + type=stringToLang, |
| 131 | + help=( |
| 132 | + "Override the configured NVDA language.\n" |
| 133 | + 'Set to "Windows" for current user default, "en" for English, etc.' |
| 134 | + ), |
| 135 | + ) |
| 136 | + parser.add_argument( |
| 137 | + "-m", |
| 138 | + "--minimal", |
| 139 | + action="store_true", |
| 140 | + dest="minimal", |
| 141 | + default=False, |
| 142 | + help="No sounds, no interface, no start message etc", |
| 143 | + ) |
| 144 | + # --secure is used to force secure mode. |
| 145 | + # Documented in the userGuide in #SecureMode. |
| 146 | + parser.add_argument( |
| 147 | + "-s", |
| 148 | + "--secure", |
| 149 | + action="store_true", |
| 150 | + dest="secure", |
| 151 | + default=False, |
| 152 | + help="Starts NVDA in secure mode", |
| 153 | + ) |
| 154 | + parser.add_argument( |
| 155 | + "-d", |
| 156 | + "--disable-addons", |
| 157 | + action="store_true", |
| 158 | + dest="disableAddons", |
| 159 | + default=False, |
| 160 | + help="Disable all add-ons", |
| 161 | + ) |
| 162 | + parser.add_argument( |
| 163 | + "--debug-logging", |
| 164 | + action="store_true", |
| 165 | + dest="debugLogging", |
| 166 | + default=False, |
| 167 | + help="Enable debug level logging just for this run.\n" |
| 168 | + "This setting will override any other log level (--loglevel, -l) argument given, " |
| 169 | + "as well as no logging option.", |
| 170 | + ) |
| 171 | + parser.add_argument( |
| 172 | + "--no-logging", |
| 173 | + action="store_true", |
| 174 | + dest="noLogging", |
| 175 | + default=False, |
| 176 | + help="Disable logging completely for this run.\n" |
| 177 | + "This setting can be overwritten with other log level (--loglevel, -l) " |
| 178 | + "switch or if debug logging is specified.", |
| 179 | + ) |
| 180 | + parser.add_argument( |
| 181 | + "--no-sr-flag", |
| 182 | + action="store_false", |
| 183 | + dest="changeScreenReaderFlag", |
| 184 | + default=True, |
| 185 | + help="Don't change the global system screen reader flag", |
| 186 | + ) |
| 187 | + installGroup = parser.add_mutually_exclusive_group() |
| 188 | + installGroup.add_argument( |
| 189 | + "--install", |
| 190 | + action="store_true", |
| 191 | + dest="install", |
| 192 | + default=False, |
| 193 | + help="Installs NVDA (starting the new copy after installation)", |
| 194 | + ) |
| 195 | + installGroup.add_argument( |
| 196 | + "--install-silent", |
| 197 | + action="store_true", |
| 198 | + dest="installSilent", |
| 199 | + default=False, |
| 200 | + help="Installs NVDA silently (does not start the new copy after installation).", |
| 201 | + ) |
| 202 | + installGroup.add_argument( |
| 203 | + "--create-portable", |
| 204 | + action="store_true", |
| 205 | + dest="createPortable", |
| 206 | + default=False, |
| 207 | + help="Creates a portable copy of NVDA (and starts the new copy).\n" |
| 208 | + "Requires `--portable-path` to be specified.\n", |
| 209 | + ) |
| 210 | + installGroup.add_argument( |
| 211 | + "--create-portable-silent", |
| 212 | + action="store_true", |
| 213 | + dest="createPortableSilent", |
| 214 | + default=False, |
| 215 | + help="Creates a portable copy of NVDA (without starting the new copy).\n" |
| 216 | + "This option suppresses warnings when writing to non-empty directories " |
| 217 | + "and may overwrite files without warning.\n" |
| 218 | + "Requires --portable-path to be specified.\n", |
| 219 | + ) |
| 220 | + parser.add_argument( |
| 221 | + "--portable-path", |
| 222 | + dest="portablePath", |
| 223 | + default=None, |
| 224 | + type=str, |
| 225 | + help="The path where a portable copy will be created", |
| 226 | + ) |
| 227 | + parser.add_argument( |
| 228 | + "--launcher", |
| 229 | + action="store_true", |
| 230 | + dest="launcher", |
| 231 | + default=False, |
| 232 | + help="Started from the launcher", |
| 233 | + ) |
| 234 | + parser.add_argument( |
| 235 | + "--enable-start-on-logon", |
| 236 | + metavar="True|False", |
| 237 | + type=stringToBool, |
| 238 | + dest="enableStartOnLogon", |
| 239 | + default=None, |
| 240 | + help="When installing, enable NVDA's start on the logon screen", |
| 241 | + ) |
| 242 | + parser.add_argument( |
| 243 | + "--copy-portable-config", |
| 244 | + action="store_true", |
| 245 | + dest="copyPortableConfig", |
| 246 | + default=False, |
| 247 | + help=( |
| 248 | + "When installing, copy the portable configuration " |
| 249 | + "from the provided path (--config-path, -c) to the current user account" |
| 250 | + ), |
| 251 | + ) |
| 252 | + # This option is passed by Ease of Access so that if someone downgrades without uninstalling |
| 253 | + # (despite our discouragement), the downgraded copy won't be started in non-secure mode on secure desktops. |
| 254 | + # (Older versions always required the --secure option to start in secure mode.) |
| 255 | + # If this occurs, the user will see an obscure error, |
| 256 | + # but that's far better than a major security hazzard. |
| 257 | + # If this option is provided, NVDA will not replace an already running instance (#10179) |
| 258 | + parser.add_argument( |
| 259 | + "--ease-of-access", |
| 260 | + action="store_true", |
| 261 | + dest="easeOfAccess", |
| 262 | + default=False, |
| 263 | + help="Started by Windows Ease of Access", |
| 264 | + ) |
| 265 | + return parser |
| 266 | + |
| 267 | + |
| 268 | +def getParser() -> NoConsoleOptionParser: |
| 269 | + global _parser |
| 270 | + if not _parser: |
| 271 | + _parser = _createNVDAArgParser() |
| 272 | + return _parser |
0 commit comments