Dart CLI programming — Crash Course

Dart CLI programming — Crash Course

Today, we will take a look at how to create a great CLI application in Dart. For that, we will use the great package dcli, which is built for that specific use. Because dcli is full of features, this is part 1 of a whole series about this package.

· Installation
· Read user input
Ask for information
· Display output
print, printerr, and echo
Color coding
· Clearing and Cursor movement
· Rows
· column
· Further reading & Conclusion

What you will learn today

  1. Create CLI project

  2. Handle user input in a CLI application

  3. Format your output nicely with layouts, colors, and much more

What you will learn in further articles

Manage files and Directories, Environment variables, Calling apps, Command Line Arguments, Paths, + 5 more things!

If you don’t want to miss this, I recommend following me!

Okay, now that we know what we will learn, let’s get right into it!

Happy reading!

Note: You can find the whole source code provided in this tutorial here


The first thing we have to do after creating our app (dart create my-cli-app) is to add dcli. To do so, we will use the command dart pub add dcli. You should be familiar with those commands, otherwise, I recommend you to look up the basics of Dart again.

Note: There is a out-of-the-box support for CLI in Dart, however, it’s not as easy as it should be and there is a lot of boilerplate code. dcli simplifies this process.

Read user input

First, let’s discuss how we can read user input. There are several ways of doing so.

We can ask the user for something (Just a basic input), have a confirm message (User can select between Yes/No), and a menu (User can select between multiple options).

Ask for information

Let’s start by asking the user for some information. In our example the Username. To ask the user, simply type ask('Username:');. That’s it. But of course, you can pass on some other arguments. For example hidden (bool) to hide the input (good for passwords), defaultValue (String), required (bool) (if false, the user can input nothing and it is okay), and validator which can be Ask.integer for example to check if the input was an integer. If not, the user has to input it again.


Now let’s say you want to confirm something. To do that, you can use confirm('Is is input right?');. You can also set a default value to false or true.


Now let’s use menus. It’s a little bit more complex than the other ones, but still easy to understand. To use a menu, call menu(); and pass a title and the options. You can also set a default value with defaultOption. Let’s see it in action.

But what about passing a class to the options? What do we do now? Well, then we can use the formatter property. To explain it better, here is an example:

As you can see, to format it we pass one car to the function and show dcli how we want to create a string out of the arguments this class can have.

Display output

Now let’s finally display some output. DCli offers a ton of methods to format your output nicely. So let’s get right into it.

print, printerr, and echo

print is the standard function that dart provides out-of-the-box. It prints a line of text including a new line.

But the standard print prints to stdout (Standard output), whereas printerr prints to stderr (Standard error). As you can imagine, you should use printerr when printing error messages.

Now, what about echo? It’s also similar to print, but echo lets you decide whether a new line is output after the text or not. By default, it is set to false. An echo call looks like this: echo('hello', newline: false');.

Great, let’s dive deeper into displaying information!

Color coding

DCli also provides a way to color code your output. To do that, just wrap your text into a function named after the color. For example, you could write print(orange('hello world', background: AnsiColor.white, bold: false)). Okay, “hello world” is now in orange, but what does this background thing do? It’s optional and provides a way to also set the background color. And the bold attribute? As you may already imagine, you can specify if the text is displayed in bold or not.

These colors are supported for foreground and background colors:

  • red

  • black

  • green

  • blue

  • yellow

  • magenta

  • cyan

  • white

  • orange

  • grey

Clearing and Cursor movement

You can also control the cursor with DCli and clear the console.

Let’s start with clearing. With clearScreen();, you clear the console and with clearLine(); you clear the current line.

If you call startOfLine(); you can move the cursor to the start of the current line and with previousLine(); to the start of the previous line. showCursorhides or shows the cursor: showCursor(show: true); shows the cursor.


There is also a way to output rows with fixed columns. It is still experimental, so use it at your own risk. To display a row, call Format().row and specify the different columns within a list. To specify the width of each column, use the width argument and pass a list of integers there. The input defines, how many characters the column has. -1 is a special number and means, that the column expands as needed. Great, are there other properties? Yes, one, and it is called alignment. It also takes a list of TableAlignment and you can specify with it, how each column is aligned. Now, this was a lot of theory, let’s just see it in action:

This one display four columns of width 17, 9, 16, and infinite. The first and last columns are aligned left, the second one right, and the third in the middle.


We have discussed before how to move the cursor around. With column, you can move the cursor to the given column on the current line.

Further reading & Conclusion

In this article, you have learned the basics of the cli framework “DCli”. You have seen how easy it is to use and how much you can do with it (You have seen a tiny bit of the whole package today).

You can unfold the whole power of DCli if you use packages like Freezed, Isar, or Flutter Hooks. If you want to learn these additions, I have entire tutorials about them. Check them out here.

In the following few articles, I will introduce more somewhat complicated packages and explain them. If you don’t want to miss this, I recommend you follow me. I tried my best to write the most straightforward tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

Thanks for reading, have a nice day!

NOTICE: This article is based on the documentation of the DCli package. All the information provided here is from this site and a big part of the source code is taken from the documentation. DCli Documentation Source: https://dcli.onepub.dev/

Did you find this article valuable?

Support Tomic Riedel by becoming a sponsor. Any amount is appreciated!