commandline.md 2.83 KB
Newer Older
Carl Schwan's avatar
Carl Schwan committed
1
2
3
4
5
6
7
8
9
---
title: Command Line Interface
description: >
  Adds the ability to specify which file to open from the command line to our text editor.
weight: 6
---

## Introduction

10
We now have a working text editor that can open and save files. We might, however, want to extend its utility by enabling users to more quickly and efficiently use it to edit files. In this tutorial we will make the editor act more like a desktop application by enabling it to open files from command line arguments or even using Open with from within Dolphin.
Carl Schwan's avatar
Carl Schwan committed
11
12
13
14
15
16
17

![](result.png)

## Code and Explanation

### mainwindow.h

18
Here we have done nothing but add a new `openFileFromUrl` function which takes a `QUrl`. Again, we use a `QUrl` instead of a `QString` so that we can also work with remote files as if they were local.
Carl Schwan's avatar
Carl Schwan committed
19

20
{{< readfile file="/content/docs/getting-started/commandline/mainwindow.h" highlight="cpp" >}}
Carl Schwan's avatar
Carl Schwan committed
21
22
23

### mainwindow.cpp

24
There's no new code here, only rearranging. Everything from `void openFile()` has been moved into `void openFileFromUrl(const QUrl &inputFileName)` except the call to `QFileDialog::getOpenFileUrl()`.
Carl Schwan's avatar
Carl Schwan committed
25

26
This way, we can call `openFile()` if we want to display a dialog, or we can call `openFileFromUrl(const QUrl &)` if we know the name of the file already. Which will be the case when we feed the file name through the command line.
Carl Schwan's avatar
Carl Schwan committed
27

28
{{< readfile file="/content/docs/getting-started/commandline/mainwindow.cpp" highlight="cpp" >}}
Carl Schwan's avatar
Carl Schwan committed
29
30
31
32
33
34
35
36
37
38
39

### main.cpp

This is where all the [QCommandLineParser ](https://doc.qt.io/qt-5/qcommandlineparser.html) magic happens. In previous examples, we only used the class to feed QApplication the necessary data for using flags like `--version` or `--author`. Now we actually get to use it to process command line arguments.

First, we tell `QCommandLineParser` that we want to add a new positional arguments. In a nutshell, these are arguments that are not options. `-h` or `--version` are options, `file` is an argument.

```c++
parser.addPositionalArgument(QStringLiteral("file"), i18n("Document to open"));
```

40
Later on, we start processing positional arguments, but only if there is one. Otherwise, we proceed as usual. In our case we can only open one file at a time, so only the first file is of interest to us. We call the `openFileFromUrl()` function and feed it the URL of the file we want to open, whether it is a local file like "$HOME/foo" or a remote one like "ftp.mydomain.com/bar". We use the overloaded form of `QUrl::fromUserInput()` in order to set the current path. This is needed in order to work with relative paths like "../baz".
Carl Schwan's avatar
Carl Schwan committed
41
42
43

```c++
if (parser.positionalArguments().count() > 0) {
44
    window->openFileFromUrl(QUrl::fromUserInput(parser.positionalArguments().at(0), QDir::currentPath()));
Carl Schwan's avatar
Carl Schwan committed
45
46
47
48
49
}
```

These are the changes:

50
{{< readfile file="/content/docs/getting-started/commandline/main.cpp" highlight="cpp" start="40" lines="13" >}}