So you want to write a Command line tool?

How can you do that with Rust?

Genie grants three wishes

  • LMGTFY 
  • StackOverFlow
  • IRC?

How about no Genie?

What are command line arguments?

  • -v short arg names
  • --version long arg names  
  • free arguments

Lets take a quick look at some examples of each kind.

Switch to terminal 

How can we abstract them?

What does the rustc command accept?

Switch to terminal

How does it do it?

Can we peek at the source?

Switch to source page.

https://github.com/rust-lang/rust/blob/e4efb47b9d23a96ff4684df80360bbed0ec68bc9/src/librustc_driver/lib.rs

Lets try to do it ourselves from the docs.

Switch to getopts crate doc page.

http://doc.rust-lang.org/getopts/getopts/index.html

Overview of the API

benefits of using Option and Result types

Simple example

build the example provided on the doc page.

Lets discuss this

explain each line

Can we improve this?

Transparently represent argument list as an instance of a struct?

Design choices

How abt a struct representing the args

Struct

with default values

Switch to Sublime to show example code.

Overview of Structs in Rust

  • Constructor
  • link to doc

Overview of Matches

Overview of Options

cmdline args -> Matches -> struct

What could possibly go wrong?

Using GetOpts in Rust

By performance

Using GetOpts in Rust

A Discussion about practical usage, API design and discoverability

  • 165