---
title: "Read custom game command-line arguments in C#"
url: https://recursiveai.org/onegame/cookbook/read-game-command-line-options/
description: "Read custom command-line arguments in a C# game. Declare options in onegame, read a level and a flag from GameInstance.Args, and pass them with dotnet run."
version: "2.5.3"
---

# Read custom game command-line arguments in C#

**onegame** is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGame.Host) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/read-game-command-line-options/read-game-command-line-options.zip)

Declare `level` in `OptionNames`, then read `game.Args.GetInt("level", 1)`.

```shell
dotnet add package RecursiveAI.OneGame.Host
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneGame.Host;

abstract class GameWithLevel
    : GameDefinition
{
    public override IReadOnlyList<string>
        OptionNames => ["level"];

    public override void OnEachFrame(
        GameInstance game) =>
        Console.WriteLine(
            game.Args.GetInt("level", 1));
}
```

For `--level 3`, the frame callback prints `3`. With no level it prints `1`.

Unzip [the example](https://recursiveai.org/onegame/cookbook/read-game-command-line-options/read-game-command-line-options.zip), then `dotnet run`.

## The full program

The first snippet is a fragment of your `GameDefinition`. In your entry point, `GameApp.Run(new MyGame(), args)` passes the command-line arguments to your game. The full program below uses the same two overrides in a ready-to-run project. The download opens no window and runs one frame. It also reads `--show-grid` as a flag. The program prints the chosen options rather than loading a level or drawing a grid.

```csharp
using OneGame.Core;
using OneGame.Host;
using OneGame.UI;

return GameApp.Run(new Demo(),
    ["--headless", "--gpu", "off", "--frames", "1", .. args]);

sealed class Demo : GameDefinition
{
    public override string Name => "Launch options";
    public override IReadOnlyList<string> OptionNames => ["level", "show-grid"];
    public override void RegisterEntities(EntityRegistry registry) { }
    public override void Setup(World world) { }
    public override void DefineScreens(ScreenSet screens) =>
        screens.Add("main", _ => Ui.Label("Launch options"));

    public override void OnEachFrame(GameInstance game)
    {
        var level = game.Args.GetInt("level", 1);
        Console.WriteLine($"Level: {level}");
        Console.WriteLine($"Show grid: {game.Args.Has("show-grid")}");
    }
}
```

With no arguments, `dotnet run` prints:

```text
Level: 1
Show grid: False
Launch options: 1 frames, tick 1, hash 9eb3622df396ae9d
```

The engine prints the final `Launch options:` line when the run ends.

A misspelled option such as `--levle` stops the run before the first frame and the error lists the options you declared. A nonnumeric `--level` value throws `FormatException` in `GetInt`.

## Variations

**Pass a level and turn on the grid flag.**

```shell
dotnet run -- --level 3 --show-grid
```

```text
Level: 3
Show grid: True
Launch options: 1 frames, tick 1, hash 9eb3622df396ae9d
```

## Related recipes

[Hot reload C# in a game](https://recursiveai.org/onegame/cookbook/hot-reload/) · [Save game slots and settings](https://recursiveai.org/onegame/cookbook/save-slots-and-settings/) · [Game options guide](https://recursiveai.org/onegame/docs/game-options/)

---

## The short answer

List your custom names in `GameDefinition.OptionNames`, without the `--` prefix. Pass the command line to `GameApp.Run`, then read the options from `GameInstance.Args` in host or view code. Use `GetInt` for a non-negative whole number and `Has` to check for a flag.

## How do I pass command-line arguments to a C# game?

With [`dotnet run`](https://learn.microsoft.com/dotnet/core/tools/dotnet-run), put a separator `--` before your game’s arguments, as in the variation above. This tells the .NET CLI that `--level` and `--show-grid` belong to your program. When you start a built game executable directly, leave out the separator and pass the game arguments directly to the executable. Declared game options work in development and release builds.

For a named value, both `--level 4` and `--level=4` work. Use a bare `--show-grid` for a presence flag. If a bare flag is followed by a word that doesn’t begin with `--`, the parser treats that word as the flag’s value. Putting the flag last, as in the variation, avoids that when you also use positional arguments.

## How do I check and parse custom command-line arguments?

`Has("show-grid")` tests presence. It also returns `true` for `--show-grid=false`, so don’t use `Has` to read a true-or-false value. `Get` reads the option as text and returns `null` for an omitted option or a bare flag. `GetInt("level", 1)` returns the fallback for an omitted or bare `--level`; it rejects text and negative numbers. Choose a fallback that makes sense for your game, and check separately whether the level number actually exists.

`OnEachFrame` makes the result easy to see in the example. For a setting you only need once, read the option where you set up the screen or service that uses it. The command line isn’t saved in the game world, snapshot, or replay. Keep it out of simulation code so the same replay doesn’t change when you restart with a different command line.
