0

I made a practice C++ program that generates a new password everytime I execute it, it asks me for the password length and then proceeds to generate a new pseudorandom password.

I will add its directory to the PATH environment variable so I can execute it from any directory.

I want to know how I can modify the executable to get the command line arguments, so I can invoke it from the terminal in the following manner:

newpass -10
or
newpass 10

Both of these would generate a password of 10 characters. I currently have my program as an object file that I have to execute as:

./newpass

5
  • 1
    If you want to pass arguments to your C++ program, you'll need to use a suitable library. This is really more of a programming question, and would be on topic on SO. But there should definitely be information available about this on the net already. Oct 20, 2017 at 21:36
  • Do a Google search for "C++ argument parsing library". It will give you some hits. tclap.sourceforge.net seems popular. Though to a first approximation, you can just use something primitive like argv[]. Oct 20, 2017 at 21:37
  • 2
    Thank you. I only just realized I was looking at it with the wrong approach. I found: Example of Parsing Arguments with getopt. gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
    – Randy
    Oct 20, 2017 at 21:42
  • BTW if these passwords are for actually securing anything (as opposed to learning how to program) make sure you are using a suitably strong and valid CSPRNG or TRNG. If for example you're just calling rand() then a competent adversary can easily figure out all your generated passwords. For questions in that area go to security.SX Oct 21, 2017 at 8:41
  • 4
    It's a programming question that has nothing to do with Unix/Linux Mar 3, 2021 at 15:43

2 Answers 2

1

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:

  1. 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;
    
  2. 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"

  3. Change the file modes of the file newpass with chmod:
    chmod +x newpass

3
  • Thank you very much I was looking into it and you are right. I always wondered how the GNU programs received their arguments and I thought it had more to do with the shell.
    – Randy
    Oct 20, 2017 at 23:33
  • Great. In Unix & Linux, the shell expands the command line for shell variables, file name matching patterns, etc, and then puts them in an array that gets copied into your program's argument vector (argv).
    – RobertL
    Oct 20, 2017 at 23:58
  • I'd make a distinction: those languages (except Pascal AFAIK) automatically receive the arguments; they often use libraries to parse options which, if used at all, are often a subset of the arguments. Also, in C and C++ argv[0] is the name of the program and argv[1] is the first argument (if it exists). Java args and perl ARGV start at 0; python starts at 1 but has no 0. Oct 21, 2017 at 8:34
-1

Shamelessly copied from here: How can I make a program executable from everywhere

"If you just type export PATH=$PATH:</path/to/file> at the command line it will only last for the length of the session.

If you want to change it permanently add export PATH=$PATH:</path/to/file> to your ~/.bashrc file (just at the end is fine)." -oadams

1
  • Thank you for your comment. I had already stumbled upon that question and that's why I stated I would add the add it's path. But I want to know how to make it executable with flags.
    – Randy
    Oct 20, 2017 at 20:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.