Command-line programs receive their extra words as an array of arguments.

argv `ARGV` is just an array of strings. Examples can model it with a local array before reading real process input.

ARGV Basics

args
argv_basics.rb
Replay: real traced execution (multi-file project)
args = ["build", "site"]
command = args.fetch(0, "help")
target = args.fetch(1, "default")
extra_count = [args.length - 2, 0].max

puts "command=#{command}"
puts "target=#{target}"
puts "arg_count=#{args.length}"
puts "extra_count=#{extra_count}"
args = ["test"]
command = args.fetch(0, "help")
target = args.fetch(1, "default")
extra_count = [args.length - 2, 0].max

puts "command=#{command}"
puts "target=#{target}"
puts "arg_count=#{args.length}"
puts "extra_count=#{extra_count}"
args = ["deploy", "prod", "--dry-run"]
command = args.fetch(0, "help")
target = args.fetch(1, "default")
extra_count = [args.length - 2, 0].max

puts "command=#{command}"
puts "target=#{target}"
puts "arg_count=#{args.length}"
puts "extra_count=#{extra_count}"
  1. args ← ["build", "site"], command ← build, target ← site, extra_count ← 0

    1args→ ["build", "site"] = ["build", "site"]  #@args=["test"], ["deploy", "prod", "--dry-run"]2command→ build = args.fetch(0, "help")build3target→ site = args.fetch(1, "default")site4extra_count→ 0 = [args.length - 2, 0].max056puts "command=#{commandbuild}"7puts "target=#{targetsite}"8puts "arg_count=#{args.length2}"9puts "extra_count=#{extra_count0}"
    outputcommand=build
    target=site
    arg_count=2
    extra_count=0
  1. args ← ["test"], command ← test, target ← default, extra_count ← 0

    1args→ ["test"] = ["test"]2command→ test = args.fetch(0, "help")test3target→ default = args.fetch(1, "default")default4extra_count→ 0 = [args.length - 2, 0].max056puts "command=#{commandtest}"7puts "target=#{targetdefault}"8puts "arg_count=#{args.length1}"9puts "extra_count=#{extra_count0}"
    outputcommand=test
    target=default
    arg_count=1
    extra_count=0
  1. args ← ["deploy", "prod", "--dry-run"], command ← deploy, target ← prod

    1args→ ["deploy", "prod", "--dry-run"] = ["deploy", "prod", "--dry-run"]2command→ deploy = args.fetch(0, "help")deploy3target→ prod = args.fetch(1, "default")prod4extra_count→ 1 = [args.length - 2, 0].max156puts "command=#{commanddeploy}"7puts "target=#{targetprod}"8puts "arg_count=#{args.length3}"9puts "extra_count=#{extra_count1}"
    outputcommand=deploy
    target=prod
    arg_count=3
    extra_count=1

Follow the Arguments

  1. args starts as ["build", "site"].
  2. args.fetch(0, "help") reads build as the command.
  3. args.fetch(1, "default") reads site as the target.
  4. args.length is 2.
  5. extra_count is 0 because there are no words after the first two. | args | command | target | arg_count | extra_count | | --- | --- | --- | ---: | ---: | | build site | build | site | 2 | 0 | | test | test | default | 1 | 0 | | deploy prod --dry-run | deploy | prod | 3 | 1 |

Exercise: argv_basics.rb

Reproduce command=build, target=site, arg_count=2, and extra_count=0, then use the pinned test and deploy arguments to predict the changed lines.