Command-Line Program Patterns
Command Dispatch
Route a command word to a small action label.
command-dispatch
Dispatch turns a command word into the branch of work to run. The same pattern works for small tools and larger command groups.
Command Dispatch
command_dispatch.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $command = "run";
my $action = "show-usage";
if ($command eq "run") {
$action = "execute";
} elsif ($command eq "check") {
$action = "validate";
}
print "command=$command\n";
print "action=$action\n";
use strict;
use warnings;
my $command = "check";
my $action = "show-usage";
if ($command eq "run") {
$action = "execute";
} elsif ($command eq "check") {
$action = "validate";
}
print "command=$command\n";
print "action=$action\n";
use strict;
use warnings;
my $command = "unknown";
my $action = "show-usage";
if ($command eq "run") {
$action = "execute";
} elsif ($command eq "check") {
$action = "validate";
}
print "command=$command\n";
print "action=$action\n";
$command ← run, $action ← show-usage
4my $command→ run = "run"; #@command="check", "unknown"5my $action→ show-usage = "show-usage";$action ← execute
7if ($commandrun eq "run") {8 $action→ execute = "execute";9} elsif ($command eq "check") {print "command=$command ";
13print "command=$commandrun\n";14print "action=$actionexecute\n";outputcommand=run action=execute
$command ← check, $action ← show-usage
4my $command→ check = "check";5my $action→ show-usage = "show-usage";$action ← validate
8 $action = "execute";9} elsif ($commandcheck eq "check") {10 $action→ validate = "validate";11}print "command=$command ";
13print "command=$commandcheck\n";14print "action=$actionvalidate\n";outputcommand=check action=validate
$command ← unknown, $action ← show-usage
4my $command→ unknown = "unknown";5my $action→ show-usage = "show-usage";67if ($command eq "run") {8 $action = "execute";9} elsif ($command eq "check") {10 $action = "validate";11}1213print "command=$commandunknown\n";14print "action=$actionshow-usage\n";outputcommand=unknown action=show-usage