Command Line Arguments
This page is under construction. Please come back later.
When running programs from the command line we have the option of supplying additional information in the form of arguments on the command line. Command line arguments take the form of strings separated by spaces. For example the following command line program invokation has three additional arguments.
$ ./a.out Fred 123 Flintstone
In our programs we can access each of these arguments using the variables
argv[0]
, argv[1]
, argv[2]
, etc.
The program name can be found in the variable with the 0 subscript. The user supplied arguments are in the variables with subscripts 1, 2, 3, etc.
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); wcout << L"Number of arguments: " << (argc - 1) << endl; wcout << L"Program Name: " << Utils::UTF8_to_wstring(argv[0]) << endl; wcout << L"Arg 1: " << Utils::UTF8_to_wstring(argv[1]) << endl; wcout << L"Arg 2: " << Utils::UTF8_to_wstring(argv[2]) << endl; wcout << L"Arg 3: " << Utils::UTF8_to_wstring(argv[3]) << endl; wcout << L"Arg 4: " << Utils::UTF8_to_wstring(argv[4]) << endl; return 0; }
Output
File not found: CmdLineArgs1u.cpp
Output
If we want to treat a command line argument as a number we first need to convert from the string representation passed on the command line to a numeric type. This is where conversion functions come in handy. The example below illustrates how to convert command line arguments to integers.
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <fmt/format.h> #include <fmt/xchar.h> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); int const a = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[1]), 0); int const b = Utils::stoiWithDefault(Utils::UTF8_to_wstring(argv[2]), 0); int const c = a + b; wcout << fmt::format(L"{0:d} + {1:d} = {2:d}", a, b, c) << endl; return 0; }
Output
The example below converts the command line arguments to floating point values.
#include "Utils.hpp" #include <clocale> #include <codecvt> #include <fmt/format.h> #include <fmt/xchar.h> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); double const a = Utils::stodWithDefault(Utils::UTF8_to_wstring(argv[1]), 0); double const b = Utils::stodWithDefault(Utils::UTF8_to_wstring(argv[2]), 0); double const c = a + b; wcout << fmt::format(L"{0:f} + {1:f} = {2:f}", a, b, c) << endl; return 0; }
Output
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Combined FICA Tax (Command Line Argument)
- Cubic Roots (Command Line)
- Dog Years (Command Line)
- Milky Way Stellar Density Estimate
- Quadratic Roots (Command Line)
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]