PART I
C h a p t e r 8 :
A C l o s e r L o o k a t M e t h o d s a n d C l a s s e s
201
PART IPART I
string
array passed to
Main( )
. The length of the
args
array will be equal to the number of
command-line arguments, which might be zero.
For example, the following program displays all of the command-line arguments that it
is called with:
// Display all command-line information.
using System;
class CLDemo {
static void Main(string[] args) {
Console.WriteLine("There are " + args.Length +
" command-line arguments.");
Console.WriteLine("They are: ");
for(int i=0; i < args.Length; i++)
Console.WriteLine(args[i]);
}
}
If
CLDemo
is executed like this:
CLDemo one two three
you will see the following output:
There are 3 command-line arguments.
They are:
one
two
three
To understand the way that command-line arguments can be used, consider the next
program. It uses a simple substitution cipher to encode or decode messages. The message
to be encoded or decoded is specified on the command line. The cipher is very simple: To
encode a word, each letter is incremented by 1. Thus, A becomes B, and so on. To decode,
each letter is decremented. Of course, such a cipher is of no practical value, being trivially
easy to break. But it does provide an enjoyable pastime for children.
// Encode or decode a message using a simple substitution cipher.
using System;
class Cipher {
static int Main(string[] args) {
// See if arguments are present.
if(args.Length < 2) {
Console.WriteLine("Usage: encode/decode word1 [word2...wordN]");
return 1; // return failure code
}
// If args present, first arg must be encode or decode.
www.freepdf-books.com
202
P a r t I :
T h e C # L a n g u a g e
if(args[0] != "encode" & args[0] != "decode") {
Console.WriteLine("First arg must be encode or decode.");
return 1; // return failure code
}
// Encode or decode message.
for(int n=1; n < args.Length; n++) {
for(int i=0; i < args[n].Length; i++) {
if(args[0] == "encode")
Console.Write((char) (args[n][i] + 1) );
else
Console.Write((char) (args[n][i] - 1) );
}
Console.Write(" ");
}
Console.WriteLine();
return 0;
}
}
To use the program, specify either the “encode” or “decode” command followed by the
phrase that you want to encrypt or decrypt. Assuming the program is called Cipher, here
are two sample runs:
C:>Cipher encode one two
pof uxp
C:>Cipher decode pof uxp
one two
There are two interesting things in this program. First, notice how the program checks
that a command-line argument is present before it continues executing. This is very important
and can be generalized. When a program relies on there being one or more command-line
arguments, it must always confirm that the proper arguments have been supplied. Failure to
do this can lead to program malfunctions. Also, since the first command-line argument must
be either “encode” or “decode,” the program also checks this before proceeding.
Second, notice how the program returns a termination code. If the required command
line is not present, then 1 is returned, indicating abnormal termination. Otherwise, 0 is
returned when the program ends.
Do'stlaringiz bilan baham: |