---
title: "The command-line tool"
description: "The commands of an application's command-line tool, with the sample application's tool as the example: settings, commands, options, output, exit codes and adapters."
audience: "Users"
kind: "Reference"
status: "Current"
version: "main · v3.2 (unreleased)"
slug: "command-line-tool"
url: "https://docs.allors.com/allors3/command-line-tool/"
---

# The command-line tool

> **Status: Current.** [Where this is implemented](#where-this-is-implemented) names the code.

An application has a command-line tool that creates, fills, saves, loads and upgrades its database. This page describes the tool of the [sample application](https://github.com/allors/allors3/tree/main/sample), `sample/Database/Commands`, which takes three of its commands from Core. Run it from the root of the platform’s repository, after `./build.sh SampleGenerate`:

Terminal window

```sh
dotnet run --project sample/Database/Commands -- <command> [options]
```

<a id="usage"></a>

## Usage

```text
Usage: Commands [command] [options]
```

| Option | Meaning |
| --- | --- |
| `-t` | The command timeout of the database, in seconds. It precedes the command: `-t 300 upgrade`. |
| `-?`, `-h`, `--help` | Show the help of the tool, or of the command that precedes it. |

Without a command the tool shows its help and exits with code 1.

<a id="settings"></a>

## Settings

The tool reads the file `custom/appsettings.json` below the [configuration root](https://docs.allors.com/allors3/configuration.md#the-configuration-root), and environment variables, which override the file.

| Setting | Meaning |
| --- | --- |
| `adapter` | Required: `snapshot`, `sqlclient` or `npgsql`. |
| `ConnectionStrings:DefaultConnection` | Required with `sqlclient` and `npgsql`. |
| `populationFile` | The file of `save`, `load` and `upgrade` when the option `-f` is absent. Without the setting the file is `population.xml`. |

A command stops with exit code 1 without a configuration root, and when a required setting has no value. Without `ALLORS_CONFIG_ROOT` the message is:

```text
ALLORS_CONFIG_ROOT is not set. Set it to the folder that holds 'custom/appsettings.json'.
```

`init` also uses the admin connection of the server; [Admin connections](https://docs.allors.com/allors3/configuration.md#admin-connections) describes it.

<a id="commands"></a>

## Commands

| Command | From | What it does | Option |
| --- | --- | --- | --- |
| `init` | The application | Drops and creates the database of the connection string, through the admin connection; [Admin connections](https://docs.allors.com/allors3/configuration.md#admin-connections) says what it does with the login of the connection string. With the Snapshot adapter it logs that there is nothing to drop or create. |  |
| `populate` | The application | Empties the database with `Init()` and runs the [setup](https://docs.allors.com/allors3/populations.md#a-new-population), which builds a new population. |  |
| `user` | The application | Creates a person who can sign in and adds that person to a user group. | `--name` (`-n`), either `--group` (`-g`) or `--group-id`, and optionally `--password-stdin`. |
| `save` | Core | Writes the population to a file. | `-f` the file |
| `load` | Core | Replaces the population with the one in a file. | `-f` the file |
| `upgrade` | The application | Loads a population that an earlier version saved and brings it up to date; [Upgrade a population](https://docs.allors.com/allors3/upgrade-a-population.md) describes its steps. | `-f` the file |
| `index` | Core | On SQL Server and PostgreSQL, creates missing indexes and tries to drop obsolete indexes of the adapter. If a drop fails, the index can remain after a successful run. Run `index` at deployment when the model’s indexes change and the population is to be kept. A database object checks for missing indexes before its first transaction. The SQL adapters also maintain the indexes during `Init()` and after a load. On the Snapshot adapter the command logs that there are no indexes to maintain. See [Indexes](https://docs.allors.com/allors3/database-schema.md#indexes). |  |

A relative file name is relative to the working directory, and the tool logs the full path of the file that it saves or loads.

<a id="create-a-user"></a>

### Create a user

After populating the database, run the command and enter the password at the masked prompt. Core’s password rule requires at least 12 characters with at least four distinct characters.

Terminal window

```sh
dotnet run --project sample/Database/Commands -- user --name worker@example.com --group Workers
```

Supply exactly one group selector. If several groups have the same name, the error lists their `UniqueId` values. Repeat the command with `--group-id <UniqueId>`. An existing user name (including a case variant), an invalid group selection or a rejected password leaves the population unchanged.

For automation, pipe the password into the command and add `--password-stdin`. It reads one line, preserving leading and trailing spaces. To sign in as the new user, follow [Run the server and sign in](https://docs.allors.com/allors3/create-an-application.md#run-the-server-and-sign-in) on Windows, or [Host a server](https://docs.allors.com/allors3/host-a-server.md) from bash.

<a id="output-and-exit-codes"></a>

## Output and exit codes

The tool logs to the console, from the level `Info` up, one line per message:

```text
13:19:57 INFO Begin
13:19:57 INFO Saving /home/user/allors3/population.xml
13:19:57 INFO End
```

| Exit code | Meaning |
| --- | --- |
| 0 | The command succeeded. |
| 1 | The command failed, its arguments were not recognized, or no command was given. The tool logs the message at the level `Error`, except when no command was given: then it shows its help. |

<a id="adapters"></a>

## Adapters

[The database adapters](https://docs.allors.com/allors3/database-adapters.md) compares the adapters. On the Snapshot adapter the population ends with the process, so each run starts with an empty database: `save` then writes a population without objects, and `load` or `upgrade` holds the loaded population only until the run ends. A sequence of commands, such as `populate` followed by `save`, needs SQL Server or PostgreSQL. The tests of the sample call the commands in one process instead, on the Snapshot adapter.

<a id="what-an-application-writes"></a>

## What an application writes

Core provides `save`, `load` and `index`, in `dotnet/Core/Database/Commands/Core`; the project of the tool includes them with a glob, as the [projects of an application](https://docs.allors.com/allors3/create-an-application.md) include Core. The application writes the rest:

-   **`Program`**, which lists the commands with `[Subcommand]`, reads the configuration of the application’s domain, `AddAllorsConfiguration("custom")` in the sample, and builds the database with the application’s rules. Core’s commands ask it for `Configuration` and `Database`.
-   **`ExitCode`**, with `Success` and `Error`, which Core’s commands return.
-   **`populate`, `user` and `upgrade`**, which depend on the application’s setup and model, and **`init`**, which Core keeps in its folder `Custom`; the sample’s is a copy of it.
-   **`nlog.config`**, which the build copies next to the tool.

<a id="where-this-is-implemented"></a>

## Where this is implemented

-   The sample’s tool is in `sample/Database/Commands`: `Program.cs`, `ExitCode.cs`, `nlog.config` and the commands in `Custom`. Its tests are in `sample/Database/Commands.Tests`.
-   Core’s commands are in `dotnet/Core/Database/Commands/Core`. `DatabaseBuilder` and `DatabaseProvisioning`, which build the database and serve `init`, are in `dotnet/System/Database/Adapters/Allors.Database.Builder`.
