Skip to main content

Parse argument aliases in Deno

You can parse command-line arguments in Deno with the standard flags module:

import { parse } from "";

deno run -a beep -b boop

{ _: [], a: 'beep', b: 'boop' }

The parse function has a second options argument where you can assign aliases. Before we do that, let’s use a more description example.

Example scenario

Say you have a script, log.ts, that takes a message to display in the terminal. We would use our script like this:

deno run log.ts --message "Hello world"

Hello world

Now let’s alias the --message flag to -M for ease of use.

Alias option

When we call parse in our script on Deno.args, we want to provide an additional options object with a key aliases.

That will be assigned to another object that will map flags to their aliases:

// log.ts
import { parse } from "";

const flagToAliasMap = {
    message: 'M'

const parsedArgs = parse(Deno.args, { aliases: flagToAliasMap })


Now we can call our script with those aliases:

deno run log.ts -M "That's better"

That's better

With that, the flags module does all of the hard lifting for you!

Wrap up

Two things to note about aliases:

  1. flags doesn’t care whether you use one or two hyphens for aliases, so -M and --M will both work.
  2. Aliases are case sensitive, so you’ll have to map m and M separately.

Happy coding!