| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import sys |
| 5 |
from argparse import RawDescriptionHelpFormatter |
| 6 |
|
| 7 |
from briefcase import __version__ |
| 8 |
from briefcase.commands import ( |
| 9 |
BuildCommand, |
| 10 |
ConvertCommand, |
| 11 |
CreateCommand, |
| 12 |
DevCommand, |
| 13 |
NewCommand, |
| 14 |
OpenCommand, |
| 15 |
PackageCommand, |
| 16 |
PublishCommand, |
| 17 |
RunCommand, |
| 18 |
UpdateCommand, |
| 19 |
UpgradeCommand, |
| 20 |
) |
| 21 |
from briefcase.commands.base import split_passthrough |
| 22 |
from briefcase.console import MAX_TEXT_WIDTH, Console |
| 23 |
from briefcase.platforms import get_output_formats, get_platforms |
| 24 |
|
| 25 |
from .exceptions import ( |
| 26 |
InvalidFormatError, |
| 27 |
InvalidPlatformError, |
| 28 |
NoCommandError, |
| 29 |
UnsupportedCommandError, |
| 30 |
) |
| 31 |
|
| 32 |
COMMANDS = [ |
| 33 |
NewCommand, |
| 34 |
DevCommand, |
| 35 |
ConvertCommand, |
| 36 |
CreateCommand, |
| 37 |
OpenCommand, |
| 38 |
BuildCommand, |
| 39 |
UpdateCommand, |
| 40 |
RunCommand, |
| 41 |
PackageCommand, |
| 42 |
PublishCommand, |
| 43 |
UpgradeCommand, |
| 44 |
] |
| 45 |
|
| 46 |
|
| 47 |
def parse_cmdline(args, console: Console | None = None): |
| 48 |
"""Parses the command line to determine the Command and its arguments. |
| 49 |
|
| 50 |
:param args: the arguments provided at the command line |
| 51 |
:param console: interface for interacting with the console |
| 52 |
:return: Command and command-specific arguments |
| 53 |
""" |
| 54 |
if console is None: |
| 55 |
console = Console() |
| 56 |
|
| 57 |
platforms = get_platforms() |
| 58 |
|
| 59 |
briefcase_description = ( |
| 60 |
"Briefcase is a tool for converting a Python project " |
| 61 |
"into a standalone native application for distribution." |
| 62 |
) |
| 63 |
|
| 64 |
description_max_pad_len = max(len(cmd.command) for cmd in COMMANDS) + 2 |
| 65 |
command_description_list = "\n".join( |
| 66 |
f" {cmd.command:<{description_max_pad_len}}{cmd.description}" |
| 67 |
for cmd in COMMANDS |
| 68 |
) |
| 69 |
|
| 70 |
platform_list = ", ".join(sorted(platforms, key=str.lower)) |
| 71 |
|
| 72 |
additional_instruction = ( |
| 73 |
"Each command, platform, and format has additional options. " |
| 74 |
"Use the -h option on a specific command for more details." |
| 75 |
) |
| 76 |
|
| 77 |
parser = argparse.ArgumentParser( |
| 78 |
prog="briefcase", |
| 79 |
description=console.textwrap( |
| 80 |
f"{briefcase_description}\n" |
| 81 |
"\n" |
| 82 |
"Commands:\n" |
| 83 |
f"{command_description_list}\n" |
| 84 |
"\n" |
| 85 |
"Platforms:\n" |
| 86 |
f" {platform_list}\n" |
| 87 |
"\n" |
| 88 |
f"{additional_instruction}" |
| 89 |
), |
| 90 |
usage="briefcase [-h] <command> [<platform>] [<format>] ...", |
| 91 |
add_help=False, |
| 92 |
formatter_class=( |
| 93 |
lambda prog: RawDescriptionHelpFormatter(prog, width=MAX_TEXT_WIDTH) |
| 94 |
), |
| 95 |
) |
| 96 |
parser.add_argument("-V", "--version", action="version", version=__version__) |
| 97 |
|
| 98 |
# <command> isn't actually optional; but if it's marked as required, |
| 99 |
# there's no way to get help for subcommands. So; treat <command> |
| 100 |
# as optional, handle the case where <command> isn't provided |
| 101 |
# as the case where top-level help is displayed, and provide an explicit |
| 102 |
# usage string so that the instructions displayed are correct |
| 103 |
parser.add_argument( |
| 104 |
"command", |
| 105 |
choices=[cmd.command for cmd in COMMANDS], |
| 106 |
metavar="command", |
| 107 |
nargs="?", |
| 108 |
help=argparse.SUPPRESS, |
| 109 |
) |
| 110 |
|
| 111 |
# argparse handles `--` specially, so make the passthrough args bypass the parser. |
| 112 |
def parse_known_args(args: list[str]) -> tuple[argparse.Namespace, list[str]]: |
| 113 |
args, passthrough = split_passthrough(args) |
| 114 |
options, extra = parser.parse_known_args(args) |
| 115 |
if passthrough: |
| 116 |
extra += ["--", *passthrough] |
| 117 |
return options, extra |
| 118 |
|
| 119 |
# Use parse_known_args to ensure any extra arguments can be ignored, |
| 120 |
# and parsed as part of subcommand handling. This will capture the |
| 121 |
# command, platform (filling a default if unspecified) and format |
| 122 |
# (with no value if unspecified). |
| 123 |
options, extra = parse_known_args(args) |
| 124 |
|
| 125 |
# If no command has been provided, display top-level help. |
| 126 |
if options.command is None: |
| 127 |
raise NoCommandError(parser.format_help()) |
| 128 |
|
| 129 |
# Commands agnostic to the platform and format |
| 130 |
if options.command == "convert": |
| 131 |
Command = ConvertCommand |
| 132 |
elif options.command == "new": |
| 133 |
Command = NewCommand |
| 134 |
elif options.command == "upgrade": |
| 135 |
Command = UpgradeCommand |
| 136 |
else: |
| 137 |
# Commands dependent on the platform and format. The general form of such a |
| 138 |
# command is `briefcase <cmd> <platform> <format>`; but the format will be |
| 139 |
# inferred from the platform if one isn't specified, and the platform will be |
| 140 |
# inferred from the operating system if it isn't explicitly given. |
| 141 |
# |
| 142 |
# <platform> and <format> aren't parsed as regular arguments due to ambiguities |
| 143 |
# in interpreting those arguments; instead, they're handled directly from the |
| 144 |
# argument list, with the expectation that they *must* be the first and second |
| 145 |
# arguments (after the command) if provided. There's no other bare arguments, so |
| 146 |
# we only need to look for whether the arguments start with "-". |
| 147 |
if extra and not extra[0].startswith("-"): |
| 148 |
name = extra.pop(0) |
| 149 |
# Normalize the platform name to the registered capitalization |
| 150 |
platform = {n.lower(): n for n in platforms}.get(name.lower(), name) |
| 151 |
else: |
| 152 |
platform = { |
| 153 |
"darwin": "macOS", |
| 154 |
"linux": "linux", |
| 155 |
"win32": "windows", |
| 156 |
}[sys.platform] |
| 157 |
|
| 158 |
# Import the platform module |
| 159 |
try: |
| 160 |
platform_module = platforms[platform] |
| 161 |
except KeyError: |
| 162 |
raise InvalidPlatformError(platform, platforms.keys()) from None |
| 163 |
|
| 164 |
# If the output format wasn't explicitly specified, check to see |
| 165 |
# Otherwise, extract and use the default output_format for the platform. |
| 166 |
if extra and not extra[0].startswith("-") and extra[0] != "--": |
| 167 |
output_format = extra.pop(0) |
| 168 |
else: |
| 169 |
output_format = platform_module.DEFAULT_OUTPUT_FORMAT |
| 170 |
|
| 171 |
output_formats = get_output_formats(platform) |
| 172 |
|
| 173 |
# Normalise casing of output_format to be more forgiving. |
| 174 |
output_format = {n.lower(): n for n in output_formats}.get( |
| 175 |
output_format.lower(), output_format |
| 176 |
) |
| 177 |
|
| 178 |
# We now know the command, platform, and format. |
| 179 |
# Get the command class that corresponds to that definition. |
| 180 |
try: |
| 181 |
format_module = output_formats[output_format] |
| 182 |
Command = getattr(format_module, options.command) |
| 183 |
except KeyError: |
| 184 |
raise InvalidFormatError( |
| 185 |
requested=output_format, |
| 186 |
choices=list(output_formats.keys()), |
| 187 |
) from None |
| 188 |
except AttributeError: |
| 189 |
raise UnsupportedCommandError( |
| 190 |
platform=platform, |
| 191 |
output_format=output_format, |
| 192 |
command=options.command, |
| 193 |
) from None |
| 194 |
return Command, extra |