Argo

An argument-parsing library for Go.

Version 0.3.0

Quickstart Tutorial


Imagine we're building a utility for joining MP3 files, something like MP3cat. We want the user to supply the file names as a list of command line arguments. We also want to support an --out/-o option so the user can specify an output filename and a --quiet/-q flag for turning down the program's verbosity.

First we import the Argo package:

import "github.com/dmulholl/argo"

Next we create an ArgParser instance:

parser := argo.NewParser()
parser.Helptext = "Usage: mp3cat..."
parser.Version = "1.0"

Supplying a helptext string for the parser activates an automatic --help/-h flag; similarly, supplying a version string activates an automatic --version/-v flag.

Now we can register our options and flags:

parser.NewStringOption("out o", "default.mp3")
parser.NewFlag("quiet q")

That's it, we're done specifying our interface. Now we can parse the program's command line arguments:

parser.Parse()

This will exit with a suitable error message for the user if anything goes wrong. Now we can check if the --quiet flag was found:

if parser.Found("quiet") {
    doStuff()
}

And determine our output filepath:

outpath := parser.StringValue("out")

The input filenames will be collected by the parser into a list of positional arguments — a slice of strings which we can access via the parser's .Args field:

for _, filename := range parser.Args {
    doStuff()
}