36
Command-Line Arguments

You know the arguments to main() that I’ve been carefully avoiding discussing?

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
.​.​.​

Now you are ready to learn about them. argv is an array of C strings. argc tells you how many strings are in the array. What do these string represent? Command-line arguments.

The command-line tools that you’ve been creating can be run from Terminal. The Terminal app is just a pretty interface to what is called a shell. There are a few different shells with catchy names like csh, sh, zsh, and ksh, but nearly all Mac users use bash. When you run a program from bash, after you type in the program name, you can supply any number of arguments separated by whitespace. Those arguments are packed into argv before main() is called.

Truthfully, Cocoa and iOS programmers seldom use argv and argc. However, if you ever write a handy command-line tool, you will almost certainly need to know how to utilize them.

In Xcode, create a new C Command Line Tool project called Affirmation. Affirmation will take two arguments, a person’s name and a number n. When you run it, that person will be declared cool n times.

$​ ​A​f​f​i​r​m​a​t​i​o​n​ ​M​i​k​e​y​ ​3​
M​i​k​e​y​ ​i​s​ ​c​o​o​l​.​
M​i​k​e​y​ ​i​s​ ​c​o​o​l​.​
M​i​k​e​y​ ​i​s​ ​c​o​o​l​.​

Before we do that, change main() to just print out each of the arguments in argv:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​a​r​g​c​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​a​r​g​ ​%​d​ ​=​ ​%​s​​n​"​,​ ​i​,​ ​a​r​g​v​[​i​]​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

If you are running this from bash, you could just type in the arguments on the command line.

$​ ​A​f​f​i​r​m​a​t​i​o​n​ ​A​a​r​o​n​ ​4​

However, to run a program with arguments in Xcode, you must first edit the scheme. Under the Product menu, choose Edit Scheme.... When the sheet appears, select Run Affirmation in the table view on the left. Then select the Arguments tab from the choices at the top of the sheet. Find the list entitled Arguments Passed On Launch and use the + button to add two items: a name and a number.

Figure 36.1  Adding arguments

Adding arguments

Click OK to dismiss the sheet.

When you run the program, you’ll get a list of the strings in argv. The one that surprises most people is argv[0]:

a​r​g​ ​0​ ​=​ ​/​U​s​e​r​s​/​a​a​r​o​n​/​L​i​b​r​a​r​y​/​D​e​v​e​l​o​p​e​r​/​X​c​o​d​e​/​D​e​r​i​v​e​d​D​a​t​a​/​
 ​ ​ ​ ​A​f​f​i​r​m​a​t​i​o​n​-​e​n​k​f​q​s​g​a​v​f​s​p​r​o​e​g​g​o​x​w​b​r​m​c​o​w​v​n​/​B​u​i​l​d​/​P​r​o​d​u​c​t​s​/​D​e​b​u​g​/​A​f​f​i​r​m​a​t​i​o​n​
a​r​g​ ​1​ ​=​ ​A​a​r​o​n​
a​r​g​ ​2​ ​=​ ​4​

argv[0] is the path to the executable file.

Figure 36.2  argv and argc in Affirmation

argv and argc in Affirmation

If your program takes arguments, the first thing you should do is make sure that the number of arguments is correct. Edit main.m:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​ ​/​/​ ​a​t​o​i​(​)​


i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​i​f​ ​(​a​r​g​c​ ​!​=​ ​3​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​f​p​r​i​n​t​f​(​s​t​d​e​r​r​,​ ​"​U​s​a​g​e​:​ ​A​f​f​i​r​m​a​t​i​o​n​ ​<​n​a​m​e​>​ ​<​n​u​m​b​e​r​>​​n​"​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​1​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​i​n​t​ ​c​o​u​n​t​ ​=​ ​a​t​o​i​(​a​r​g​v​[​2​]​)​;​
 ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​j​ ​=​ ​0​;​ ​j​ ​<​ ​c​o​u​n​t​;​ ​j​+​+​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​s​ ​i​s​ ​c​o​o​l​.​​n​"​,​ ​a​r​g​v​[​1​]​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

atoi() is a standard C function that reads a C string and tries to make an int out of it.

Build and run the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset