Adding an Optional GUI Flag to Getopts

Now that have getopts set, it's time to add an option to allow us to launch a GTK GUI instead of only providing command-line output. Before we begin though, we need to import the following crates for the upcoming GTK chapters: gtk, gdk and pango. However, we will need to use the latest git versions because the current stable packages conflict with some libs in hyper. This issue will probably be fixed the next time these packages are updated.

Cargo.toml

gtk = { git = "https://github.com/gtk-rs/gtk" }
gdk = { git = "https://github.com/gtk-rs/gdk" }
pango = { git = "https://github.com/gtk-rs/pango" }

And now we just need to add these crates to our main.rs file.

main.rs

extern crate gtk;
extern crate gdk;
extern crate pango;

Implementing the GTK3 GUI Flag

We will first add an extra flag argument for selecting to launch GUI.

opts.optflag("g", "gui", "display in a GTK3 GUI");

And then we will add an extra line to our help function to display the new flag option.

fn print_help() {
    println!("Prints the latest information from Phoronix.");
    println!("    -h, --help     : show this information");
    println!("    -g, --gui      : launches a GTK3 GUI instead of outputting to the terminal");
    println!("    -n, --no-color : prints to stdout without using colors");
}

Choosing Between CLI and GUI

And then we need to add a check in our code to allow the program to launch the GUI code instead of the CLI code when g is passed as an argument to the program. You should have your code look like this:

match matches.opt_present("g") {
    true => phoronix_gui::launch(),
    false => {
        match matches.opt_present("n") {
            true => phoronix_cli::print_homepage(),
            false => phoronix_cli::print_homepage_colored(),
        };
    },
};

This won't actually compile or do anything yet though because we have yet to create the phoronix_gui.rs file containing the launch() function.