Command Line Arguments ECE2893 Lecture 20 ECE2893 Command Line - - PowerPoint PPT Presentation

command line arguments
SMART_READER_LITE
LIVE PREVIEW

Command Line Arguments ECE2893 Lecture 20 ECE2893 Command Line - - PowerPoint PPT Presentation

Command Line Arguments ECE2893 Lecture 20 ECE2893 Command Line Arguments Spring 2011 1 / 5 Command Line Arguments ECE2893 Command Line Arguments Spring 2011 2 / 5 Command Line Arguments All of our assignments do date have been command


slide-1
SLIDE 1

Command Line Arguments

ECE2893 Lecture 20

ECE2893 Command Line Arguments Spring 2011 1 / 5

slide-2
SLIDE 2

Command Line Arguments

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-3
SLIDE 3

Command Line Arguments

1

All of our assignments do date have been command line programs.

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-4
SLIDE 4

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-5
SLIDE 5

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-6
SLIDE 6

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

3

We have also used many other command line programs:

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-7
SLIDE 7

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

3

We have also used many other command line programs:

cp Copy

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-8
SLIDE 8

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

3

We have also used many other command line programs:

cp Copy cd Change Directory

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-9
SLIDE 9

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

3

We have also used many other command line programs:

cp Copy cd Change Directory make Build a program binary

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-10
SLIDE 10

Command Line Arguments

1

All of our assignments do date have been command line programs.

2

We run them from a terminal window, and enter the name of the program.

./drawing

3

We have also used many other command line programs:

cp Copy cd Change Directory make Build a program binary rsync Copy files from one computer to another

ECE2893 Command Line Arguments Spring 2011 2 / 5

slide-11
SLIDE 11

Command Line Arguments

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-12
SLIDE 12

Command Line Arguments

1

Almost all such programs read and process command line arguments.

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-13
SLIDE 13

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-14
SLIDE 14

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-15
SLIDE 15

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-16
SLIDE 16

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-17
SLIDE 17

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

mkdir "My New Directory"

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-18
SLIDE 18

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

mkdir "My New Directory"

4

You can also precede to space character with the escape character (\)

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-19
SLIDE 19

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

mkdir "My New Directory"

4

You can also precede to space character with the escape character (\)

mkdir My\ New\ Directory

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-20
SLIDE 20

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

mkdir "My New Directory"

4

You can also precede to space character with the escape character (\)

mkdir My\ New\ Directory

5

You can also use wildcards, such as the *:

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-21
SLIDE 21

Command Line Arguments

1

Almost all such programs read and process command line arguments.

cp drawing-skeleton.cc drawing.cc cd A8-LineDrawing

2

The command line arguments follow the name of the program, and are separated by spaces (whitespace).

3

If an argument contains spaces, you can enclose it in double quotes:

mkdir "My New Directory"

4

You can also precede to space character with the escape character (\)

mkdir My\ New\ Directory

5

You can also use wildcards, such as the *:

ls -la *.cc

ECE2893 Command Line Arguments Spring 2011 3 / 5

slide-22
SLIDE 22

Command Line Arguments

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-23
SLIDE 23

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-24
SLIDE 24

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-25
SLIDE 25

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-26
SLIDE 26

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-27
SLIDE 27

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

IMPORTANT: The program name itself is counted, so the value of argc is always at least one.

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-28
SLIDE 28

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

IMPORTANT: The program name itself is counted, so the value of argc is always at least one.

3

argv is a pointer to an array of char* values.

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-29
SLIDE 29

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

IMPORTANT: The program name itself is counted, so the value of argc is always at least one.

3

argv is a pointer to an array of char* values.

4

Each of the array value points to a zero-byte-terminated character string containing the value of the argument.

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-30
SLIDE 30

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

IMPORTANT: The program name itself is counted, so the value of argc is always at least one.

3

argv is a pointer to an array of char* values.

4

Each of the array value points to a zero-byte-terminated character string containing the value of the argument.

5

The value of argv[0] is always the name of the program.

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-31
SLIDE 31

Command Line Arguments

1

The complete list of arguments is passed to the C main function as two arguments:

int argc char** argv

2

argc is the count of arguments.

IMPORTANT: The program name itself is counted, so the value of argc is always at least one.

3

argv is a pointer to an array of char* values.

4

Each of the array value points to a zero-byte-terminated character string containing the value of the argument.

5

The value of argv[0] is always the name of the program.

6

The value of argv[argc] is always NULL

ECE2893 Command Line Arguments Spring 2011 4 / 5

slide-32
SLIDE 32

Command Line Arguments Example

/ / P r i n t the command l i n e arguments . / / George F . Riley , Georgia Tech , CS1372 F a l l 2008 #include <iostream > / / Some programmers use the f o l l o w i n g main function / / i n t main ( i n t argc , char∗ argv [ ] ) / / The above i s i d e n t i c a l to the below . int main ( int argc , char∗∗ argv ) { / / Loop over the command l i n e arguments and p r i n t them for ( int i = 0; i < argc ; ++ i ) { std : : cout << " Arg [ " << i << " ] i s " << argv [ i ] << } }

ECE2893 Command Line Arguments Spring 2011 5 / 5