Debian Codegraph @

docs/en/how-to/building/cli-apps.md

raw
1 # Building your Console App with Briefcase { #cli-apps }
2
3 ## Overview
4
5 This guide explains the key differences when creating a console application using Briefcase, as opposed to a GUI-based app. It assumes you are already familiar with the basics of Briefcase and have completed the [BeeWare Tutorial](https://tutorial.beeware.org/).
6
7 We'll cover:
8
9 1. How selecting "Console" changes your project configuration
10 2. The [`console_app`][] flag in `pyproject.toml`
11 3. How Briefcase handles command-line arguments
12 4. Operational differences in how console apps run and display output
13 5. Packaging differences specific to console apps (e.g., `.pkg` bundles on macOS)
14
15 ## Creating a Console App
16
17 Start by generating a new Briefcase project with `briefcase new`. When prompted to select a GUI framework, choose the option corresponding to **Console**.:
18
19 ```console
20 $ briefcase new
21 ...
22 -- GUI Framework -------------------------------------------------------------
23
24 What GUI toolkit do you want to use for this project?
25
26 Additional GUI bootstraps are available from the community.
27
28 Check them out at https://beeware.org/bee/briefcase-bootstraps
29
30 1) Toga
31 2) PySide6 (does not support iOS/Android/Web deployment)
32 3) Pygame (does not support iOS/Android/Web deployment)
33 4) Console (does not support iOS/Android/Web deployment)
34 5) None
35
36 GUI Framework [1]: 4
37 ```
38
39 This changes the code that Briefcase generates for the new app, and the configuration for the new project.
40
41 The most important change is that your `pyproject.toml` will include:
42
43 ```toml
44 console_app = true
45 ```
46
47 This flag informs Briefcase to treat the app as a terminal-based (non-GUI) application. Console apps print to the terminal directly, unlike GUI apps which log to the system console or display windows.
48
49 ### Inspecting the File Structure
50
51 Your generated project will have a layout similar to the following, assuming your app's formal name is `Hello CLI` and its app name is `hello-cli`:
52
53 ```text
54 hello-cli/
55 ├── pyproject.toml
56 ├── src/
57 │ └── hello_cli/
58 │ ├── __main__.py
59 │ └── app.py
60 └── ...
61 ```
62
63 Note the `app.py` file contains a simple `print("Hello, World.")` statement by default.
64
65 ## Handling Command-line Arguments
66
67 You can use any standard Python tool to parse CLI arguments. Here's a simple example using [`argparse`][]:
68
69 ```python
70 import argparse
71
72 def main():
73 parser = argparse.ArgumentParser(description="A simple CLI app.")
74 parser.add_argument("name", help="Your name")
75 args = parser.parse_args()
76
77 print(f"Hello, {args.name}!")
78 ```
79
80 Update your `app.py` file with this logic. You can now pass a name when running the app.
81
82 ## Running the App with Briefcase
83
84 Briefcase provides two ways to run your app: `dev` mode and `run` mode. You can pass command-line arguments to your app in both cases using `--` followed by your arguments.
85
86 For example, if your app uses the example `argparse` code shown above, running in dev mode:
87
88 ```console
89 $ briefcase dev -- John
90 ===========================================================================
91 Hello, John!
92 ```
93
94 And when you run the built app:
95
96 ```console
97 $ briefcase run -- John
98 ===========================================================================
99 Hello, John!
100 ```
101
102 This is a key difference from GUI apps, which are usually launched without passing arguments and display a graphical window.
103
104 ## Packaging and Distribution
105
106 Once you've tested your app, you can package it for distribution using:
107
108 ```console
109 $ briefcase create
110 $ briefcase build
111 $ briefcase package
112 ```
113
114 On Linux, you'll get a system package (`.deb`, `.rpm` etc) for your Linux distribution; on Windows, you'll get a `.msi` installer; and on macOS, you'll get a `.pkg` installer. Command-line apps can't be generated for Android, iOS or web, as those platforms don't have a console that is accessible by the user.
115
116 /// note | Note
117
118 On macOS, console apps *must* be packaged as `.pkg` files, rather than the `.app` or `.dmg` bundles used for GUI apps. A `.pkg` installer is required because a post-processing step must be used to ensure the binary is available on the user's path, allowing the app to be executed from the command line.
119
120 ///
121
122 When the package is installed by the user, it can run from the terminal:
123
124 ```console
125 $ hello-cli John
126 Hello, John!
127 ```
128
129 The executable name of your app will be the app name, not the formal name. For example, in this guide, the formal name is "Hello CLI", but the executable name is `hello-cli`. This is the name you will use to run your app from the terminal, as shown in the examples above. However, the installer will use the formal name.
130
131 ## Console App Behavior Summary
132
133 Key differences from GUI apps include:
134
135 - **Project structure** includes `console_app = true` in `pyproject.toml`;
136 - **Apps are executed in the terminal**, and print directly to `stdout`/`stderr`;
137 - **Arguments are passed using** `-- ARGS ...` when using `briefcase dev` and `briefcase run`;
138 - **No GUI framework or windowing system** is used or bundled; and
139 - **macOS packaging uses \`\`.pkg\`\` format**, rather than GUI `.app`/`.dmg` bundles.
140
141 ## What's next?
142
143 Now that you've built a simple console app with Briefcase, you can use these principles to build more complex CLI tools or integrate other libraries like [Click](https://click.palletsprojects.com/), [Typer](https://typer.tiangolo.com/), or any other Python CLI framework. For more advanced text-based interfaces, you might also explore libraries like [curses](https://docs.python.org/3/library/curses.html) or [Textual](https://textual.textualize.io/), which allow you to create "GUI-like" applications in the terminal.