Getopt
is a little overkill to just get one command line argument. If your program requires more options and arguments later, you might consider something else similar to getopt
. For a more C++ oriented way to parse more complicated command lines you might want to take a look at the Boost Program Options library. Most languages (Java, Perl, Python, C, C++, Pascal, etc etc) have a library of functions or statements which return the command line arguments.
In C or C++ you can simply convert the first argument "10" or whatever it is, to an integer. All command line arguments are strings (char
arrays terminated with a zero byte).
Something like this:
Add the code to your main program to check for the command line argument, something like:
int pwlength(10); // or whatever the default
if (argc > 1) {
// put code here to convert the string in argv[0] to an integer
// and store in pwlength.
// If the string cannot be converted,
// Print an error message and exit the program with
// the statement "return 1;" or "exit(1);" which
// notifies the caller of the executable that an error occurred.
}
std::cout << "Your password length will be: " << pwlength << std::endl;
Add the directory where the file newpass
is stored to your PATH
variable, for example, assuming newpass
is located in your $HOME/bin
directory:
PATH="$PATH:$HOME/bin"
Change the file modes of the file newpass
with chmod
:
chmod +x newpass
rand()
then a competent adversary can easily figure out all your generated passwords. For questions in that area go to security.SX