Presentation is loading. Please wait.

Presentation is loading. Please wait.

Command-line arguments

Similar presentations


Presentation on theme: "Command-line arguments"— Presentation transcript:

1 Command-line arguments

2 Outline In this lesson, we will:
Understand what command-line arguments are See how to access command-line arguments Learn how to parse integers passed to programs

3 Command-line arguments
Functions can take arguments How about programs? In Linux, you can get a directory listing by entering % ls -al Here, ls is the name of a program that is to be executed The executable is located (on my computer) in /usr/bin/ls The -al is a command-line argument that is passed to the executing program

4 Command-line arguments
Command-line arguments are handled by operating systems as follows: Suppose that on the console, the user types % ls -al /usr/lib The shell (or command prompt) breaks this into three separate strings separated by white space: "ls" "-al" "/usr/lib" These are converted to null-character-terminated strings

5 Command-line arguments
If you want a white-space character to be ignored, you must prefix it with a '\' Suppose the user types % ls ~/ECE\ 150/Slides This command line is broken into two strings: "ls" "~/ECE 150/Slides" Recall escape characters?

6 Command-line arguments
How are these passed to main? After all, int main() has no parameters Actually, it does, only while the arguments are put on the stack, they are thus ignored by the executing program To access these arguments, you must give the appropriate parameters: int main( int argc, char *argv[] ); You can change the identifiers, but will annoy everyone else argc: argument count argv: argument vector

7 Printing the arguments
We can write a program that does nothing more than print out the command-line arguments: #include <iostream> int main( int argc, char *argv[] ); int main( int argc, char *argv[] ) { std::cout << "There are " << argc << " command-line arguments:" << std::endl; for ( int k{0}; k < argc; ++k ) { std::cout << " " << k << ": \"" << argv[k] << "\"" << std::endl; } return 0;

8 Printing the arguments
$ g++ example.cpp $ ls a.out example.cpp $ ./a.out Hello world! There are 3 command-line arguments: 0: "./a.out" 1: "Hello" 2: "world!" $ ./a.out Hello\ world! There are 4 command-line arguments: 1: "Hello world!" 2: "123" 3: "456" $ ./a.out -al ~/public_html 1: "-al" 2: "/home/ece050/public_html"

9 Integers? Suppose we want one of the arguments to be an integer
We need to convert a string to an integer so we can use it While sub-optimal, here is a start: #include <iostream> #include <string> int main( int argc, char *argv[] ); int main( int argc, char *argv[] ) { if ( argc != 2 ) { std::cout << "Expecting one (1) argument, but got " << (argc - 1) << std::endl; return -1; } int arg{ std::stoi( argv[1] ) }; // Use arg... return 0;

10 Integers? The function std::stoi converts a string to an int
In C, there is atoi, converting ascii to an integer If it is unable to convert the argument to an int, an exception is thrown

11 Summary Following this lesson, you now
Understand that command-line arguments are passed They use null-character-terminated strings Know you can access these and parse them We described a very simple std::stoi function

12 References [1] No references?

13 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

14 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "Command-line arguments"

Similar presentations


Ads by Google