Command-line arguments

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Dynamically allocating arrays
Anatomy of a program.
Switch statement.
Do-while loops.
Command-line arguments
Throwing exceptions.
Console input.
Dangling pointers.
Hello world!.
This.
Sorted arrays.
Floating-point primitive data types
Dynamically allocating arrays within structures
Break statements.
Wild pointers.
Console output.
The comma as a separator and as an operator
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Throwing exceptions.
Dynamically allocating structures
Sorting algorithms.
Passing pointers as parameters to and from functions
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Literal data.
Console output.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Recursive functions.
Presentation transcript:

Command-line arguments

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

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

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

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?

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

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;

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! 123 456 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"

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;

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

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

References [1] No references?

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 https://www.rbg.ca/ for more information.

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.