Build a Command Line Weather App in Deno

If you’ve been following along with our introductory articles on Deno, you’re probably interested in having a go at writing your first program. In this article, we’re going to walk…


This content originally appeared on SitePoint and was authored by Nilson Jacques

Build a Terminal Weather App in Deno

If you’ve been following along with our introductory articles on Deno, you’re probably interested in having a go at writing your first program. In this article, we’re going to walk through installing the Deno runtime, and creating a command-line weather program that will take a city name as an argument and return the weather forecast for the next 24 hours.

To write code for Deno, I’d highly recommend Visual Studio Code with the official Deno plugin. To make things a little more interesting, we’re going to be writing the app in TypeScript.

Installing Deno

Firstly, let’s get Deno installed locally so we can begin writing our script. The process is straightforward, as there are installer scripts for all three major operating systems.

Windows

On windows, you can install Deno from PowerShell:

iwr https://deno.land/x/install/install.ps1 -useb | iex

Linux

From the Linux terminal, you can use the following command:

curl -fsSL https://deno.land/x/install/install.sh |  sh

macOS

On a Mac, Deno can be installed with Brew:

brew install deno

After installing

Once the install process is finished, you can check that Deno has been correctly installed by running the following command:

deno --version

You should now see something similar to this:

deno 1.2.0
v8 8.5.216
typescript 3.9.2

Let’s create a folder for our new project (inside your home folder, or wherever you like to keep your coding projects) and add an index.ts file:

mkdir weather-app
cd weather-app
code index.ts

Note: as I mentioned above, I’m using VS Code for this tutorial. If you’re using a different editor, replace the last line above.

Getting User Input

Our program is going to retrieve the weather forecast for a given city, so we’ll need to accept the city name as an argument when the program is run. Arguments supplied to a Deno script are available as Deno.args. Let’s log this variable out to the console to see how it works:

console.log(Deno.args);

Now run the script, with the following command:

deno run index.ts --city London

You should see the following output:

[ "--city", "London" ]

Although we could parse this argument array ourselves, Deno’s standard library includes a module called flags that will take care of this for us. To use it, all we have to do is add an import statement to the top of our file:

import { parse } from  "https://deno.land/std@0.61.0/flags/mod.ts";

Note: the examples in the docs for standard library modules will give you an unversioned URL (such as https://deno.land/std/flags/mod.ts), which will always point to the latest version of the code. It’s good practice to specify a version in your imports, to ensure your program isn’t broken by future updates.*

Let’s use the imported function to parse the arguments array into something more useful:

const args = parse(Deno.args);

We’ll also change the script to log out our new args variable, to see what that looks like. So now your code should look like this:

import { parse } from  "https://deno.land/std@0.61.0/flags/mod.ts";

const args = parse(Deno.args);

console.log(args);

Now, if you run the script with the same argument as before, you should see the following output:

Download https://deno.land/std@0.61.0/flags/mod.ts
Download https://deno.land/std@0.61.0/_util/assert.ts
Check file:///home/njacques/code/weather-app/index.ts
{ _: [], city: "London" }

Whenever Deno runs a script, it checks for new import statements. Any remotely hosted imports are downloaded, compiled, and cached for future use. The parse function has provided us with an object, which has a city property containing our input.

Note: if you need to re-download the imports for a script for any reason, you can run deno cache --reload index.ts.

We should also add a check for the city argument, and quit the program with an error message if it’s not supplied:

if (args.city === undefined) {
    console.error("No city supplied");
    Deno.exit();
}

Continue reading Build a Command Line Weather App in Deno on SitePoint.


This content originally appeared on SitePoint and was authored by Nilson Jacques


Print Share Comment Cite Upload Translate Updates
APA

Nilson Jacques | Sciencx (2020-08-09T21:54:16+00:00) Build a Command Line Weather App in Deno. Retrieved from https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/

MLA
" » Build a Command Line Weather App in Deno." Nilson Jacques | Sciencx - Sunday August 9, 2020, https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/
HARVARD
Nilson Jacques | Sciencx Sunday August 9, 2020 » Build a Command Line Weather App in Deno., viewed ,<https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/>
VANCOUVER
Nilson Jacques | Sciencx - » Build a Command Line Weather App in Deno. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/
CHICAGO
" » Build a Command Line Weather App in Deno." Nilson Jacques | Sciencx - Accessed . https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/
IEEE
" » Build a Command Line Weather App in Deno." Nilson Jacques | Sciencx [Online]. Available: https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/. [Accessed: ]
rf:citation
» Build a Command Line Weather App in Deno | Nilson Jacques | Sciencx | https://www.scien.cx/2020/08/09/build-a-command-line-weather-app-in-deno/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.